spring boot 集成ssl
https 默認配置端口443,http 80
SSL 獲取方式
阿里云,騰訊云申請免費單域名證書,解壓后會出現(xiàn)兩個文件,一個是密碼,一個SSL證書
jdk 對源證書進行加密處理
密碼最好和源密碼一樣
操作步驟
打開cmd命令,切到jdk安裝的bin包下,如下圖,輸入命令后會提示輸入三次密碼,前兩次自定義密碼,最后一次源密碼,也是下載阿里證書文件里面的密碼
keytool -importkeystore -srckeystore 2832703_gshp.top.pfx(阿里證書源文件) -destkeystore gshp.jks(生成的文件) -srcstoretype PKCS12(加密方式) -deststoretype JKS

image.png
spring boot 集成SSL
application.yml 添加配置
配置含義參考官網(wǎng)地址: https://docs.spring.io/spring-boot/docs/2.2.2.RELEASE/reference/html/appendix-application-properties.html#common-application-properties
注意不要寫錯哦,博主配置引用錯誤,報錯WebServerException: Could not load key store 'null'
應用配置

image
spring boot 配置監(jiān)聽,http訪問自動跳轉(zhuǎn)https
package com.gshp.config;
import io.undertow.Undertow;
import io.undertow.UndertowOptions;
import io.undertow.servlet.api.SecurityConstraint;
import io.undertow.servlet.api.SecurityInfo;
import io.undertow.servlet.api.TransportGuaranteeType;
import io.undertow.servlet.api.WebResourceCollection;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.embedded.undertow.UndertowServletWebServerFactory;
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* 采用Undertow作為服務器,支持https服務配置和HTTP2.0協(xié)議
*
*/
@Configurationpublic
class WebServerConfiguration {
/**
* http服務接口
*/
@Value("${gshp.server.http.port}")
private Integer httpPort;
@Value("${server.port}")
private Integer httpsPort;
@Bean
public ServletWebServerFactory undertowFactory(){
UndertowServletWebServerFactory undertowFactory = new UndertowServletWebServerFactory();
undertowFactory.addBuilderCustomizers((Undertow.Builder builder) -> {
builder.addHttpListener(httpPort, "0.0.0.0");
// 開啟HTTP2
builder.setServerOption(UndertowOptions.ENABLE_HTTP2, true);
});
undertowFactory.addDeploymentInfoCustomizers(deploymentInfo -> {
// 開啟HTTP自動跳轉(zhuǎn)至HTTPS
deploymentInfo.addSecurityConstraint(
new SecurityConstraint().addWebResourceCollection(
new WebResourceCollection().addUrlPattern("/*"))
.setTransportGuaranteeType(TransportGuaranteeType.CONFIDENTIAL)
.setEmptyRoleSemantic(SecurityInfo.EmptyRoleSemantic.PERMIT))
.setConfidentialPortManager(exchange -> httpsPort);
});
return undertowFactory;
}
}
——有遇到什么問題,歡迎評論區(qū)討論

image.png