SpringBoot配置Https入門實(shí)踐
一、生成一個(gè)https證書
我們使用Java自帶的JDK管理工具keytool來生成一個(gè)免費(fèi)的https證書,在我們的Java安裝目錄下,在bin目錄下我們使用cmd啟動(dòng)命令行窗口,執(zhí)行如下命令生成一個(gè)https證書。
keytool -genkey -alias myhttps -keyalg RSA -keysize 2048 -keystore E:\test.p12 -validity 365
- genkey表示要?jiǎng)?chuàng)建一個(gè)新的密鑰
- alias表示keystore的別名
- keyalg表示使用的加密算法是RSA
- keysize表示密鑰的長度
- keystore表示生成密鑰的存放位置
- validity表示密鑰的有效天數(shù)
我們設(shè)置的密鑰的名稱是myhttps,口令是123456,先保存好后續(xù)集成到SpringBoot會(huì)使用到。
我們在E盤中發(fā)現(xiàn)生成了這個(gè)https證書。
二、集成到SpringBoot
1.把生成的https證書復(fù)制到項(xiàng)目的resources目錄下
2.在application.yml中添加https相關(guān)配置
server: ssl: # 證書文件名 key-store: classpath:test.p12 # 證書密鑰別名 key-alias: myhttps # 密鑰口令 key-store-password: 123456
3.啟動(dòng)項(xiàng)目測試
示例代碼如下:
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; /** * @author qinxun * @date 2023-06-15 * @Descripion: */ @RestController public class IndexController { @GetMapping("/index") public String toIndex() { return "hello https"; } }
我們先使用常規(guī)的http訪問,會(huì)提示請(qǐng)求錯(cuò)誤。
我們修改為使用https訪問,可以正常訪問了。
三、請(qǐng)求轉(zhuǎn)發(fā)配置
SpringBoot不支持同時(shí)啟用http和https,為了解決這個(gè)問題,我們可以新增一個(gè)配置,當(dāng)用戶發(fā)起http訪問的時(shí)候,自動(dòng)重定向到https上。
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.boot.web.embedded.tomcat.TomcatServletWebServerFactory; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * @author qinxun * @date 2023-06-16 * @Descripion: 請(qǐng)求轉(zhuǎn)發(fā)配置 */ @Configuration public class HttpsConfig { @Bean TomcatServletWebServerFactory tomcatServletWebServerFactory() { TomcatServletWebServerFactory factory = 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); } }; factory.addAdditionalTomcatConnectors(createHttpsConnector()); return factory; } private Connector createHttpsConnector() { // 設(shè)置http請(qǐng)求端口為8081的都自動(dòng)重定向到https端口 Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol"); connector.setScheme("http"); connector.setPort(8081); connector.setSecure(false); connector.setRedirectPort(8080); return connector; } }
我們請(qǐng)求http://localhost:8081/index會(huì)重定向到了https://localhost:8080/index這個(gè)訪問地址,成功實(shí)現(xiàn)了http重定向到https的配置。
到此這篇關(guān)于SpringBoot配置Https入門實(shí)踐的文章就介紹到這了,更多相關(guān)SpringBoot配置Https內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- springboot如何配置ssl支持https
- SpringBoot配置HTTPS及開發(fā)調(diào)試的操作方法
- springboot實(shí)現(xiàn)的https單向認(rèn)證和雙向認(rèn)證(java生成證書)
- SpringBoot配置Https訪問的詳細(xì)步驟
- springboot項(xiàng)目開啟https協(xié)議的項(xiàng)目實(shí)現(xiàn)
- SpringBoot的HTTPS配置實(shí)現(xiàn)
- springboot配置http跳轉(zhuǎn)https的過程
- springboot如何將http轉(zhuǎn)https
- springboot支持https請(qǐng)求的實(shí)現(xiàn)
- SpringBoot中支持Https協(xié)議的實(shí)現(xiàn)
- SpringBoot整合HTTPS的項(xiàng)目實(shí)踐
相關(guān)文章
Spring5源碼解析之Spring中的異步和計(jì)劃任務(wù)
本篇文章主要介紹了Spring5源碼解析之Spring中的異步和計(jì)劃任務(wù),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-10-10SpringBoot啟動(dòng)過程的實(shí)現(xiàn)
這篇文章主要介紹了SpringBoot啟動(dòng)過程的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09基于SpringBoot和Vue寫一個(gè)2048小游戲
創(chuàng)建一個(gè)基于 Java Spring Boot 后端和 Vue 前端的 2048 游戲,可以按照以下步驟進(jìn)行,這個(gè)項(xiàng)目將包括后端(用來處理游戲邏輯)和前端(用來顯示游戲界面和與用戶交互),感興趣的小伙伴可以參考本文自己動(dòng)手嘗試一下2024-08-08selenium+java+chrome環(huán)境搭建的方法步驟
這篇文章主要介紹了selenium+java+chrome環(huán)境搭建的方法步驟,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07