SpringBoot配置Https入門實踐
一、生成一個https證書
我們使用Java自帶的JDK管理工具keytool來生成一個免費的https證書,在我們的Java安裝目錄下,在bin目錄下我們使用cmd啟動命令行窗口,執(zhí)行如下命令生成一個https證書。
keytool -genkey -alias myhttps -keyalg RSA -keysize 2048 -keystore E:\test.p12 -validity 365
- genkey表示要創(chuàng)建一個新的密鑰
- alias表示keystore的別名
- keyalg表示使用的加密算法是RSA
- keysize表示密鑰的長度
- keystore表示生成密鑰的存放位置
- validity表示密鑰的有效天數(shù)

我們設置的密鑰的名稱是myhttps,口令是123456,先保存好后續(xù)集成到SpringBoot會使用到。

我們在E盤中發(fā)現(xiàn)生成了這個https證書。
二、集成到SpringBoot
1.把生成的https證書復制到項目的resources目錄下

2.在application.yml中添加https相關配置
server:
ssl:
# 證書文件名
key-store: classpath:test.p12
# 證書密鑰別名
key-alias: myhttps
# 密鑰口令
key-store-password: 1234563.啟動項目測試
示例代碼如下:
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訪問,會提示請求錯誤。

我們修改為使用https訪問,可以正常訪問了。

三、請求轉發(fā)配置
SpringBoot不支持同時啟用http和https,為了解決這個問題,我們可以新增一個配置,當用戶發(fā)起http訪問的時候,自動重定向到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: 請求轉發(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() {
// 設置http請求端口為8081的都自動重定向到https端口
Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
connector.setScheme("http");
connector.setPort(8081);
connector.setSecure(false);
connector.setRedirectPort(8080);
return connector;
}
}我們請求http://localhost:8081/index會重定向到了https://localhost:8080/index這個訪問地址,成功實現(xiàn)了http重定向到https的配置。
到此這篇關于SpringBoot配置Https入門實踐的文章就介紹到這了,更多相關SpringBoot配置Https內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
- springboot如何配置ssl支持https
- SpringBoot配置HTTPS及開發(fā)調(diào)試的操作方法
- springboot實現(xiàn)的https單向認證和雙向認證(java生成證書)
- SpringBoot配置Https訪問的詳細步驟
- springboot項目開啟https協(xié)議的項目實現(xiàn)
- SpringBoot的HTTPS配置實現(xiàn)
- springboot配置http跳轉https的過程
- springboot如何將http轉https
- springboot支持https請求的實現(xiàn)
- SpringBoot中支持Https協(xié)議的實現(xiàn)
- SpringBoot整合HTTPS的項目實踐
相關文章
selenium+java+chrome環(huán)境搭建的方法步驟
這篇文章主要介紹了selenium+java+chrome環(huán)境搭建的方法步驟,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-07-07

