spring boot配置ssl實現(xiàn)HTTPS的方法
傳輸層安全性協(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 來生成證書:
打開控制臺輸入:
keytool -genkey -alias tomcat -storetype PKCS12 -keyalg RSA -keysize 2048 -keystore keystore.p12 -validity 3650
這里的別名是 keystore.p12,密碼什么的直接設置就好,然后回車
然后根據(jù)路徑找到生成好的證書,把證書復制到項目里,我是放到了這里
放好證書后,建立一個index.html放到resources/templates文件夾下,一會用于測試。
再配置properties
server.port=8888 server.tomcat.uri-encoding=utf-8 server.servlet.context-path=/demo server.ssl.key-store=keystore.p12 server.ssl.key-store-password=123456 server.ssl.key-store-type=PKCS12 server.ssl.key-alias=tomcat spring.thymeleaf.prefix=classpath:/templates/
配置好properties再加入下面的代碼
@Configuration public class HttpsConfig { /** * spring boot 1.0 */ /* @Bean public EmbeddedServletContainerFactory servletContainer() { TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory() { @Override protected 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 */ @Bean public TomcatServletWebServerFactory servletContainer() { TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory() { @Override protected 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; } @Bean public 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的端口號后轉向到的https的端口號 connector.setRedirectPort(8888); return connector; } }
@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> <version>2.0.1.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
準備完畢后啟動項目,打印臺顯示
再輸入:
127.0.0.1:8080/demo/index就會自動跳轉
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
SpringCloud Zuul過濾器和谷歌Gauva實現(xiàn)限流
這篇文章主要介紹了SpringCloud Zuul過濾器和谷歌Gauva實現(xiàn)限流,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-03-03Java BeanUtils.copyProperties的詳解
這篇文章主要介紹了Java BeanUtils.copyProperties的詳解,本篇文章通過簡要的案例,講解了該項技術的了解與使用,以下就是詳細內容,需要的朋友可以參考下2021-08-08