欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

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)文章

  • Mybatis日志模塊的適配器模式詳解

    Mybatis日志模塊的適配器模式詳解

    這篇文章主要介紹了Mybatis日志模塊的適配器模式詳解,,mybatis用了適配器模式來兼容這些框架,適配器模式就是通過組合的方式,將需要適配的類轉(zhuǎn)為使用者能夠使用的接口
    2022-08-08
  • Java實現(xiàn)在正則表達式中控制大小寫的方法

    Java實現(xiàn)在正則表達式中控制大小寫的方法

    這篇文章主要介紹了Java實現(xiàn)在正則表達式中控制大小寫的方法,結(jié)合實例形式分析了java正則表達式中傳遞控制參數(shù)的功能與相關(guān)操作技巧,需要的朋友可以參考下
    2017-04-04
  • 關(guān)于解決雪花算法生成的ID傳輸前端后精度丟失問題

    關(guān)于解決雪花算法生成的ID傳輸前端后精度丟失問題

    這篇文章主要介紹了關(guān)于解決雪花算法生成的ID傳輸前端后精度丟失問題,雪花算法生成的ID傳輸?shù)角岸藭r,會出現(xiàn)后三位精度丟失,本文提供了解決思路,需要的朋友可以參考下
    2023-03-03
  • springmvc使用REST出現(xiàn):Request?method?'PUT'?not?supported問題

    springmvc使用REST出現(xiàn):Request?method?'PUT'?not?sup

    這篇文章主要介紹了springmvc使用REST出現(xiàn):Request?method?'PUT'?not?supported問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-02-02
  • 深入分析JAVA 多線程--interrupt()和線程終止方式

    深入分析JAVA 多線程--interrupt()和線程終止方式

    這篇文章主要介紹了JAVA 多線程--interrupt()和線程終止方式的的相關(guān)資料,文中代碼非常細致,幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下
    2020-06-06
  • MyBatis中動態(tài)SQL語句@Provider的用法

    MyBatis中動態(tài)SQL語句@Provider的用法

    本文主要介紹了MyBatis中動態(tài)SQL語句@Provider的用法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-06-06
  • java實現(xiàn)創(chuàng)建縮略圖、伸縮圖片比例生成的方法

    java實現(xiàn)創(chuàng)建縮略圖、伸縮圖片比例生成的方法

    這篇文章主要介紹了java實現(xiàn)創(chuàng)建縮略圖、伸縮圖片比例生成的方法,可實現(xiàn)針對圖片大小的縮放功能,是Java針對圖片操作的典型應(yīng)用,需要的朋友可以參考下
    2014-11-11
  • Spring Boot實現(xiàn)通用的接口參數(shù)校驗

    Spring Boot實現(xiàn)通用的接口參數(shù)校驗

    本文介紹基于 Spring Boot 和 JDK8 編寫一個 AOP ,結(jié)合自定義注解實現(xiàn)通用的接口參數(shù)校驗。具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-05-05
  • Java Executor 框架的實例詳解

    Java Executor 框架的實例詳解

    這篇文章主要介紹了Java Executor 框架的實例詳解的相關(guān)資料,這里提供實例來幫助大家學(xué)習(xí)理解這部分內(nèi)容,需要的朋友可以參考下
    2017-09-09
  • Java模擬實現(xiàn)斗地主發(fā)牌

    Java模擬實現(xiàn)斗地主發(fā)牌

    這篇文章主要為大家詳細介紹了Java實現(xiàn)模擬斗地主發(fā)牌,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-07-07

最新評論