Springboot中怎么配置ssl,針對這個問題,這篇文章詳細介紹了相對應的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
SSL(Secure Sockets Layer 安全套接層)是為網絡通信提供安全及數據完整性的一種安全協議,SSL在網絡傳輸層對網絡連接進行加密,SSL協議位于TCP/IP協議與各種應用層協議之間,為數據通信提供安全支持。SSL協議分為兩層,SSL記錄協議建立在TCP之上,為高層協議提供數據封裝、壓縮、加密等基本功能支持。SSL握手協議建立在SSL記錄協議之上,用戶實際數據傳輸開始前進行身份驗證、協商加密算法、交換加密秘鑰。
JDK中keytool是一個證書管理工具,可以生成自簽名證書。
本人這里用的系統是deepin,然后生成命令如下(找不到keytoo命令的先去配置java環境)
我指定的名字叫tomcat.keystore 別名叫tomcat,密碼自己設置,我這里用的tomcat,最后那個直接按得回車
keytool -genkey -alias tomcat -keyalg RSA -keystore /home/gzr/tomcat.keystore
我的命令執行記錄如下,(前提:在本地要創建目錄,否則會失?。?/p>

結果是生成了對應的文件,如下:

#端口號 server.port: 8443 #你生成的證書名字 server.ssl.key-store: E:\work\rave\tomcat.keystore #密鑰庫密碼 server.ssl.key-store-password: duan123 server.ssl.keyStoreType: JKS server.ssl.keyAlias: tomcat

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.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory;
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
public class SpringbootmyApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootmyApplication.class, args);
}
/**
* it's for set http url auto change to https
*/
@Bean
public EmbeddedServletContainerFactory servletContainer(){
TomcatEmbeddedServletContainerFactory tomcat=new TomcatEmbeddedServletContainerFactory(){
@Override
protected void postProcessContext(Context context) {
SecurityConstraint securityConstraint=new SecurityConstraint();
securityConstraint.setUserConstraint("CONFIDENTIAL");//confidential
SecurityCollection collection=new SecurityCollection();
collection.addPattern("/*");
securityConstraint.addCollection(collection);
context.addConstraint(securityConstraint);
}
};
tomcat.addAdditionalTomcatConnectors(httpConnector());
return tomcat;
}
@Bean
public Connector httpConnector(){
Connector connector=new Connector("org.apache.coyote.http11.Http11NioProtocol");
connector.setScheme("http");
connector.setPort(8080);
connector.setSecure(false);
connector.setRedirectPort(8443);
return connector;
}
}關于Springboot中怎么配置ssl問題的解答就分享到這里了,希望以上內容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關注億速云行業資訊頻道了解更多相關知識。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。