springboot項目開啟https協(xié)議的項目實現
1、在windows以管理員身份運行cmd,輸入如下命令生成證書
keytool -genkey -alias myhttps -keyalg RSA -keysize 2048 -validity 36500 -keystore "D:/tmp/ssl/myhttps.keystore"
注釋
命令:keytool -genkey -alias testhttps -keyalg RSA -keysize 2048 -validity 36500 -keystore "D:/tmp/ssl/testhttps.keystore"
命令解釋:
- -genkey 表示要創(chuàng)建一個新的密鑰。
- -alias 表示 keystore 的別名。
- -keyalg 表示使用的加密算法是 RSA。
- -keysize 表示密鑰的長度.。
- -keystore 表示生成的密鑰存放位直。
- -validity 表示密鑰的有效時間,單位為天。
2、將目錄下的myhttps.keystore文件移動到resource下面

3、配置文件
server: ? port: 9987 ? non-ssl-port: 8089 # 用于 非ssl請求 強制轉成 ssl 請求 # 當使用 訪問地址:http://127.0.0.1:8089/hello 訪問時 后臺會 將請求 轉換成 https://127.0.0.1:9987/hello # ?servlet: # ? ?context-path: /ssl-service ? ssl: ? ? key-store: classpath:myhttps.keystore ?#類路徑下的自簽證書 ? ? key-alias: myhttps # 證書別名 ? ? key-store-password: 123456 #證書密碼 ? ? key-store-type: JKS # 證書類型 ? ? enabled: true ?# 開啟證書驗證
4、配置http強制跳轉https配置類
package com.example.springboot3.config;
import org.apache.catalina.Context;
import org.apache.catalina.connector.Connector;
import org.apache.tomcat.util.descriptor.web.SecurityCollection;
import org.apache.tomcat.util.descriptor.web.SecurityConstraint;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
?* @author oukele
?* http 請求 強制跳轉成 https
?*/
@Configuration
public class HttpToHttpsConfig {
? ? /**
? ? ?* 項目指定的端口號
? ? ?*/
? ? @Value("${server.port}")
? ? private int serverPort;
? ? /**
? ? ?* 用于 非ssl請求 強制轉成 ssl 請求 的端口號
? ? ?*/
? ? @Value("${server.non-ssl-port}")
? ? private int port;
? ? @Bean
? ? public TomcatServletWebServerFactory servletContainerFactory() {
? ? ? ? TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory() {
? ? ? ? ? ? @Override
? ? ? ? ? ? protected void postProcessContext(Context context) {
? ? ? ? ? ? ? ? //設置安全性約束
? ? ? ? ? ? ? ? SecurityConstraint securityConstraint = new SecurityConstraint();
? ? ? ? ? ? ? ? securityConstraint.setUserConstraint("CONFIDENTIAL");
? ? ? ? ? ? ? ? //設置約束條件
? ? ? ? ? ? ? ? SecurityCollection collection = new SecurityCollection();
? ? ? ? ? ? ? ? //攔截所有請求
? ? ? ? ? ? ? ? collection.addPattern("/*");
? ? ? ? ? ? ? ? securityConstraint.addCollection(collection);
? ? ? ? ? ? ? ? context.addConstraint(securityConstraint);
? ? ? ? ? ? }
? ? ? ? };
? ? ? ? Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
? ? ? ? //設置將分配給通過此連接器接收到的請求的方案
? ? ? ? connector.setScheme("http");
? ? ? ? //true: http使用http, https使用https;
? ? ? ? //false: http重定向到https;
? ? ? ? connector.setSecure(false);
? ? ? ? //設置監(jiān)聽請求的端口號,這個端口不能其他已經在使用的端口重復,否則會報錯
? ? ? ? connector.setPort(port);
? ? ? ? //重定向端口號(非SSL到SSL)
? ? ? ? connector.setRedirectPort(serverPort);
? ? ? ? tomcat.addAdditionalTomcatConnectors(connector);
? ? ? ? return tomcat;
? ? }
}5、在瀏覽器中測試

到此這篇關于springboot項目開啟https協(xié)議的項目實現的文章就介紹到這了,更多相關springboot開啟https內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Java畢業(yè)設計實戰(zhàn)之線上水果超市商城的實現
這是一個使用了java+SSM+springboot+redis開發(fā)的網上水果超市商城,是一個畢業(yè)設計的實戰(zhàn)練習,具有水果超市商城該有的所有功能,感興趣的朋友快來看看吧2022-01-01
一次"java:程序包org.aspectj.lang不存在"問題解決實戰(zhàn)記錄
這篇文章主要給大家介紹了一次"java:程序包org.aspectj.lang不存在"問題解決的實戰(zhàn)過程,這個錯誤提示意味著你的Java程序中引用了org.aspectj.lang這個包,但是該包并不存在,文章通過圖文介紹的非常詳細,需要的朋友可以參考下2023-06-06

