javascript
Spring Boot 配置SSL 实现HTTPS
文章目錄
- 1. 簡介
- 2. 證書生成
- 3. 證書引入
- 4. 創(chuàng)建測試index
- 5. 配置
- 6. 創(chuàng)建配置類
- 7. 創(chuàng)建控制器測試
- 8. 瀏覽器驗證
1. 簡介
傳輸層安全性協(xié)議(英語:Transport Layer Security,縮寫作 TLS),及其前身安全套接層 (Secure Sockets Layer,縮寫作 SSL)是一種安全協(xié)議,目的是為互聯(lián)網(wǎng)通信,提供安全及數(shù) 據(jù)完整性保障。網(wǎng)景公司(Netscape)在1994年推出首版網(wǎng)頁瀏覽器,網(wǎng)景導航者時,推出HTTPS 協(xié)議,以SSL進行加密,這是SSL的起源。IETF將SSL進行標準化,1999年公布第一版TLS標準文 件。隨后又公布RFC 5246 (2008年8月)與 RFC 6176 (2011年3月)。在瀏覽器、電子郵件、 即時通信、VoIP、網(wǎng)絡傳真等應用程序中,廣泛支持這個協(xié)議。主要的網(wǎng)站,如Google、 Facebook等也以這個協(xié)議來創(chuàng)建安全連接,發(fā)送數(shù)據(jù)。目前已成為互聯(lián)網(wǎng)上保密通信的工業(yè)標準。SSL包含記錄層(Record Layer)和傳輸層,記錄層協(xié)議確定傳輸層數(shù)據(jù)的封裝格式。傳輸層安全 協(xié)議使用X.509認證,之后利用非對稱加密演算來對通信方做身份認證,之后交換對稱密鑰作為會談 密鑰(Session key)。這個會談密鑰是用來將通信兩方交換的數(shù)據(jù)做加密,保證兩個應用間通信的 保密性和可靠性,使客戶與服務器應用之間的通信不被攻擊者竊聽。在配置TLS/SSL之前我們需要拿到相應簽名的證書,測試實例可以使用Java 下面的 Keytool 來生成證書:
2. 證書生成
打開控制臺輸入:
keytool -genkey -alias tomcat -storetype PKCS12 -keyalg RSA -keysize 2048 -keystore keystore.p12 -validity 3650
這里的別名是 keystore.p12,密碼什么的直接設置就好,然后回車
3. 證書引入
然后根據(jù)路徑找到生成好的證書,把證書復制到項目里,我是放到了這里
4. 創(chuàng)建測試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. 創(chuàng)建配置類
@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監(jiān)聽的http的端口號connector.setPort(8080);connector.setSecure(false);//監(jiān)聽到http的端口號后轉(zhuǎn)向到的https的端口號connector.setRedirectPort(8888);return connector;}}7. 創(chuàng)建控制器測試
@Controller @RequestMapping public class ViewControlller {@GetMapping("index")public String index(){return "index";} }值得注意的是加入的springboot jar的版本不同代碼有一定的改變,我這里用的是2.0的版本,還有就是要想跳轉(zhuǎn)到html頁面的時候一定注意的就是千萬不要在Controller中用@RestController而是要用Controller,如果用RestController的話就會直接把你的index解析顯示在頁面當中,就不會跳轉(zhuǎn)了,還有就是想要跳轉(zhuǎn)的話一定要加入下面的兩個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就會自動跳轉(zhuǎn)
總結
以上是生活随笔為你收集整理的Spring Boot 配置SSL 实现HTTPS的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: centos7以普通用户开机启动某个服务
- 下一篇: oracle11g linux 日期格式