国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > Java > 正文

SpringBoot2.0如何啟用https協議

2024-07-14 08:41:23
字體:
來源:轉載
供稿:網友

SpringBoot2.0之后,啟用https協議的方式與1.*時有點兒不同,貼一下代碼。 

我的代碼能夠根據配置參數中的condition.http2https,確定是否啟用https協議,如果啟用https協議時,會將所有http協議的訪問,自動轉到https協議上。

一、啟動程序 

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轉https協議,http端口:"+this.httpPort+",https端口:"+this.httpsPort);     Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");     connector.setScheme("http");     //Connector監聽的http的端口號     connector.setPort(httpPort);     connector.setSecure(false);     //監聽到http的端口號后轉向到的https的端口號     connector.setRedirectPort(httpsPort);     return connector;   }} 

二、配置文件

1.使用http協議時的配置 

server.port=80 

2.使用https及http協議時的配置 

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 

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VeVb武林網。


注:相關教程知識閱讀請移步到JAVA教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 平远县| 康乐县| 三门县| 临西县| 茂名市| 阿鲁科尔沁旗| 乐亭县| 翼城县| 塘沽区| 江都市| 罗田县| 中江县| 井陉县| 新丰县| 铁岭县| 清流县| 深泽县| 曲阜市| 扶风县| 洛南县| 北京市| 莆田市| 古蔺县| 拜城县| 福贡县| 兴山县| 宁都县| 绥宁县| 鲁山县| 丽水市| 炎陵县| 博乐市| 象山县| 双鸭山市| 长子县| 古浪县| 汉沽区| 西畴县| 始兴县| 长宁县| 定边县|