最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringBoot2.0如何啟用https協(xié)議

 更新時(shí)間:2018年06月29日 11:40:56   作者:wallimn  
這篇文章主要介紹了SpringBoot2.0如何啟用https協(xié)議,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧

SpringBoot2.0之后,啟用https協(xié)議的方式與1.*時(shí)有點(diǎn)兒不同,貼一下代碼。

我的代碼能夠根據(jù)配置參數(shù)中的condition.http2https,確定是否啟用https協(xié)議,如果啟用https協(xié)議時(shí),會(huì)將所有http協(xié)議的訪問,自動(dòng)轉(zhuǎn)到https協(xié)議上。

一、啟動(dòng)程序 

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啟動(dòng)程序 
 * @author wallimn,http://wallimn.iteye.com 
 * 
 */ 
@SpringBootApplication 
public class AssetApplication { 
 
  public static void main(String[] args) { 
    SpringApplication.run(AssetApplication.class, args); 
  } 
  //如果沒有使用默認(rèn)值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的端口號(hào) 
    connector.setPort(httpPort); 
    connector.setSecure(false); 
    //監(jiān)聽到http的端口號(hào)后轉(zhuǎn)向到的https的端口號(hào) 
    connector.setRedirectPort(httpsPort); 
    return connector; 
  }} 

二、配置文件

1.使用http協(xié)議時(shí)的配置

server.port=80 

2.使用https及http協(xié)議時(shí)的配置

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 

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

木兰县| 德阳市| 天门市| 逊克县| 色达县| 宝鸡市| 诸暨市| 即墨市| 乌什县| 巴马| 长丰县| 大英县| 皋兰县| 克拉玛依市| 晋宁县| 游戏| 孟连| 自贡市| 大新县| 靖西县| 菏泽市| 板桥市| 巴南区| 安岳县| 灵石县| 开江县| 孙吴县| 澄江县| 苏州市| 吉首市| 威宁| 老河口市| 霍山县| 麻阳| 开化县| 奈曼旗| 泸溪县| 长宁县| 涿鹿县| 滦南县| 大洼县|