SpringBoot2.0如何啟用https協(xié)議
SpringBoot2.0之后,啟用https協(xié)議的方式與1.*時有點兒不同,貼一下代碼。
我的代碼能夠根據(jù)配置參數(shù)中的condition.http2https,確定是否啟用https協(xié)議,如果啟用https協(xié)議時,會將所有http協(xié)議的訪問,自動轉(zhuǎn)到https協(xié)議上。
一、啟動程序
package com.wallimn.iteye.sp.asset; 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.beans.factory.annotation.Value; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory; import org.springframework.context.annotation.Bean; /** * SpringBoot2.0啟動程序 * @author wallimn,http://wallimn.iteye.com * */ @SpringBootApplication public class AssetApplication { public static void main(String[] args) { SpringApplication.run(AssetApplication.class, args); } //如果沒有使用默認值80 @Value("${http.port:80}") Integer httpPort; //正常啟用的https端口 如443 @Value("${server.port}") Integer httpsPort; // springboot2 寫法 @Bean @ConditionalOnProperty(name="condition.http2https",havingValue="true", matchIfMissing=false) public TomcatServletWebServerFactory servletContainer() { TomcatServletWebServerFactory tomcat = 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); } }; tomcat.addAdditionalTomcatConnectors(httpConnector()); return tomcat; } @Bean @ConditionalOnProperty(name="condition.http2https",havingValue="true", matchIfMissing=false) public Connector httpConnector() { System.out.println("啟用http轉(zhuǎn)https協(xié)議,http端口:"+this.httpPort+",https端口:"+this.httpsPort); Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol"); connector.setScheme("http"); //Connector監(jiān)聽的http的端口號 connector.setPort(httpPort); connector.setSecure(false); //監(jiān)聽到http的端口號后轉(zhuǎn)向到的https的端口號 connector.setRedirectPort(httpsPort); return connector; }}
二、配置文件
1.使用http協(xié)議時的配置
server.port=80
2.使用https及http協(xié)議時的配置
server.port=443 server.ssl.key-store=classpath:keystore.p12 server.ssl.key-store-password=your-password server.ssl.keyStoreType=PKCS12 server.ssl.keyAlias=your-cert-alias condition.http2https=true http.port=80
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
關(guān)于解決雪花算法生成的ID傳輸前端后精度丟失問題
這篇文章主要介紹了關(guān)于解決雪花算法生成的ID傳輸前端后精度丟失問題,雪花算法生成的ID傳輸?shù)角岸藭r,會出現(xiàn)后三位精度丟失,本文提供了解決思路,需要的朋友可以參考下2023-03-03springmvc使用REST出現(xiàn):Request?method?'PUT'?not?sup
這篇文章主要介紹了springmvc使用REST出現(xiàn):Request?method?'PUT'?not?supported問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-02-02深入分析JAVA 多線程--interrupt()和線程終止方式
這篇文章主要介紹了JAVA 多線程--interrupt()和線程終止方式的的相關(guān)資料,文中代碼非常細致,幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下2020-06-06MyBatis中動態(tài)SQL語句@Provider的用法
本文主要介紹了MyBatis中動態(tài)SQL語句@Provider的用法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06java實現(xiàn)創(chuàng)建縮略圖、伸縮圖片比例生成的方法
這篇文章主要介紹了java實現(xiàn)創(chuàng)建縮略圖、伸縮圖片比例生成的方法,可實現(xiàn)針對圖片大小的縮放功能,是Java針對圖片操作的典型應(yīng)用,需要的朋友可以參考下2014-11-11Spring Boot實現(xiàn)通用的接口參數(shù)校驗
本文介紹基于 Spring Boot 和 JDK8 編寫一個 AOP ,結(jié)合自定義注解實現(xiàn)通用的接口參數(shù)校驗。具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-05-05