日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring Boot 配置SSL 实现HTTPS

發布時間:2024/9/27 javascript 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring Boot 配置SSL 实现HTTPS 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文章目錄

            • 1. 簡介
            • 2. 證書生成
            • 3. 證書引入
            • 4. 創建測試index
            • 5. 配置
            • 6. 創建配置類
            • 7. 創建控制器測試
            • 8. 瀏覽器驗證

1. 簡介
傳輸層安全性協議(英語:Transport Layer Security,縮寫作 TLS),及其前身安全套接層 (Secure Sockets Layer,縮寫作 SSL)是一種安全協議,目的是為互聯網通信,提供安全及數 據完整性保障。網景公司(Netscape)在1994年推出首版網頁瀏覽器,網景導航者時,推出HTTPS 協議,以SSL進行加密,這是SSL的起源。IETF將SSL進行標準化,1999年公布第一版TLS標準文 件。隨后又公布RFC 524620088月)與 RFC 617620113月)。在瀏覽器、電子郵件、 即時通信、VoIP、網絡傳真等應用程序中,廣泛支持這個協議。主要的網站,如Google、 Facebook等也以這個協議來創建安全連接,發送數據。目前已成為互聯網上保密通信的工業標準。SSL包含記錄層(Record Layer)和傳輸層,記錄層協議確定傳輸層數據的封裝格式。傳輸層安全 協議使用X.509認證,之后利用非對稱加密演算來對通信方做身份認證,之后交換對稱密鑰作為會談 密鑰(Session key)。這個會談密鑰是用來將通信兩方交換的數據做加密,保證兩個應用間通信的 保密性和可靠性,使客戶與服務器應用之間的通信不被攻擊者竊聽。

在配置TLS/SSL之前我們需要拿到相應簽名的證書,測試實例可以使用Java 下面的 Keytool 來生成證書:

2. 證書生成

打開控制臺輸入:

keytool -genkey -alias tomcat -storetype PKCS12 -keyalg RSA -keysize 2048 -keystore keystore.p12 -validity 3650


這里的別名是 keystore.p12,密碼什么的直接設置就好,然后回車

3. 證書引入

然后根據路徑找到生成好的證書,把證書復制到項目里,我是放到了這里

4. 創建測試index

放好證書后,建立一個index.html放到resources/templates文件夾下,一會用于測試。

5. 配置

再配置application.properties

server.port=8888 server.tomcat.uri-encoding=utf-8 server.servlet.context-path=/demoserver.ssl.key-store=keystore.p12 server.ssl.key-store-password=123456 server.ssl.key-store-type=PKCS12 server.ssl.key-alias=tomcatspring.thymeleaf.prefix=classpath:/templates/
6. 創建配置類
@Configuration public class HttpsConfig {/*** spring boot 1.0*//* @Beanpublic EmbeddedServletContainerFactory servletContainer() {TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory() {@Overrideprotected void postProcessContext(Context context) {SecurityConstraint constraint = new SecurityConstraint();constraint.setUserConstraint("CONFIDENTIAL");SecurityCollection collection = new SecurityCollection();collection.addPattern("/*");constraint.addCollection(collection);context.addConstraint(constraint);}};tomcat.addAdditionalTomcatConnectors(httpConnector());return tomcat;}*//*** spring boot 2.0* @return*/@Beanpublic TomcatServletWebServerFactory servletContainer() {TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory() {@Overrideprotected void postProcessContext(Context context) {SecurityConstraint constraint = new SecurityConstraint();constraint.setUserConstraint("CONFIDENTIAL");SecurityCollection collection = new SecurityCollection();collection.addPattern("/*");constraint.addCollection(collection);context.addConstraint(constraint);}};tomcat.addAdditionalTomcatConnectors(httpConnector());return tomcat;}@Beanpublic Connector httpConnector() {Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");connector.setScheme("http");//Connector監聽的http的端口號connector.setPort(8080);connector.setSecure(false);//監聽到http的端口號后轉向到的https的端口號connector.setRedirectPort(8888);return connector;}}
7. 創建控制器測試
@Controller @RequestMapping public class ViewControlller {@GetMapping("index")public String index(){return "index";} }

值得注意的是加入的springboot jar的版本不同代碼有一定的改變,我這里用的是2.0的版本,還有就是要想跳轉到html頁面的時候一定注意的就是千萬不要在Controller中用@RestController而是要用Controller,如果用RestController的話就會直接把你的index解析顯示在頁面當中,就不會跳轉了,還有就是想要跳轉的話一定要加入下面的兩個jar包

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId></dependency>

準備完畢后啟動項目,打印臺顯示

8. 瀏覽器驗證

從控制臺可以看出:
http地址為:http://127.0.0.1:8080/demo/index
https地址為:https://127.0.0.1:8888/demo/index
再輸入:
127.0.0.1:8080/demo/index就會自動跳轉

總結

以上是生活随笔為你收集整理的Spring Boot 配置SSL 实现HTTPS的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。