1冬阳、生成證書
如果大家對HTTPS不太了解可自行檢索資料倘核,這里就不在贅述HTTPS.
我們要想使用https協(xié)議谷浅,我們需要生成一個證書好爬,這個證書我們可以自己生成局雄,也可以從SSL證書中心獲取(阿里云)自己生成的不被客戶端認可抵拘,從授權(quán)中心獲取的證書客戶端才認可哎榴。有money的富豪可以去服務商哪里去獲刃突怼(筆者是個窮鬼)我們這里使用的是自行生成的證書。
自己生成證書的方式很簡單尚蝌,直接使用java自帶的keytool來生成迎变,生成的命令如下:
keytool -genkeypair -alias "test1" -keyalg "RSA" -keysize 2048?-keystore "?E:\tomcat.key" ?-validity 3650
這里涉及到幾個參數(shù)的含義我簡單說一下:
1.-genkeypair?生成一對非對稱密鑰;
2.-keyalg 生證書的算法名稱,RSA是一種非對稱加密算法?
3.-keysize 證書大小?
4.-keystore 生成的證書文件的存儲路徑?
5.-validity 證書的有效期
執(zhí)行完成后上面的命令后飘言,就會在相對應的路徑下生成對應的文件衣形。獲取到文件怎么使用進Spring boot項目呢?
2姿鸿、Spring boot 2.x 配置SSL 使用 https
? ? 2.1 首先谆吴,將剛剛獲取的證書,放在心目的根目錄下
? ? 2.2 配置 application.yml 文件
3苛预、也上步驟就將https配置完成句狼。但是這樣還不夠,因為用戶并不知道是否使用HTTPS 热某,大多數(shù)的用戶是直接使用HTTP協(xié)議來訪問我們的網(wǎng)站的腻菇。這時候我們需要添加HTTP自定轉(zhuǎn)項到HTTPS的功能,當用戶使用HTTP來進行訪問的時候自動轉(zhuǎn)為HTTPS的方式昔馋。這個配置很簡單筹吐,在入口類中添加相應的轉(zhuǎn)向Bean就行了。
????在springboot1.x這樣配置
????@Bean
public Connector connector(){
????????Connector connector=new Connector("org.apache.coyote.http11.Http11NioProtocol");
? ? ? ? connector.setScheme("http");
????????connector.setPort(8080);
????????connector.setSecure(false);
????????connector.setRedirectPort(1111);
????????return connector;
}
@Bean
?public EmbeddedServletContainerFactory servletContainer() {
?TomcatEmbeddedServletContainerFactory tomca t= 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( connector ());
? ? ? ? return tomcat;
? ? }
這個時候當我們訪問http://localhost:8080的時候系統(tǒng)會自動重定向到https://localhost:1111這個地址上秘遏。
? ? ? ?首先 這里需要使用 EmbeddedServletContainerFactory?這個類,但是在springboot2.x版本已經(jīng)找不到這個類了丘薛。但是在網(wǎng)上大部分還都是根據(jù)1.x來實現(xiàn)的,這也是我為什么寫這篇文章的初衷邦危,所以需要下邊代碼實現(xiàn)springboot2.x版本HTTP自動轉(zhuǎn)向HTTPS洋侨。
在springboot2.x這樣配置
@Bean
public Connector connector(){
Connector connector=new Connector("org.apache.coyote.http11.Http11NioProtocol");
connector.setScheme("http");
connector.setPort(8080);
connector.setSecure(false);
connector.setRedirectPort(8033);
return connector;
}
@Bean
public TomcatServletWebServerFactory tomcatServletWebServerFactory(){
TomcatServletWebServerFactory tomcat =new TomcatServletWebServerFactory(){
????????@Override
? ? ? ? protected void postProcessContext(Context context) {
????????SecurityConstraint securityConstraint=new SecurityConstraint();
????????securityConstraint.setUserConstraint("CONFIDENTIAL");
????????SecurityCollection collection=new SecurityCollection();
????????collection.addPattern("/*");
????????securityConstraint.addCollection(collection);
????????context.addConstraint(securityConstraint);
}
};
tomcat.addAdditionalTomcatConnectors(connector());
return tomcat;
}
到這,我們在springboot2.x項目中铡俐,當我們訪問http://localhost:8080的時候系統(tǒng)會自動重定向到https://localhost:1111這個地址上凰兑。
區(qū)別就是EmbeddedServletContainerFactory 換成了TomcatServletWebServerFactory;