zoukankan      html  css  js  c++  java
  • pringBoot2.0启用https协议

    SpringBoot2.0之后,启用https协议的方式与1.*时有点儿不同,贴一下代码。 

      我的代码能够根据配置参数中的condition.http2https,确定是否启用https协议,如果启用https协议时,会将所有http协议的访问,自动转到https协议上。 

    一、启动程序 

    Java代码  收藏代码
    1. package com.wallimn.iteye.sp.asset;  
    2.   
    3. import org.apache.catalina.Context;  
    4. import org.apache.catalina.connector.Connector;  
    5. import org.apache.tomcat.util.descriptor.web.SecurityCollection;  
    6. import org.apache.tomcat.util.descriptor.web.SecurityConstraint;  
    7. import org.springframework.beans.factory.annotation.Value;  
    8. import org.springframework.boot.SpringApplication;  
    9. import org.springframework.boot.autoconfigure.SpringBootApplication;  
    10. import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;  
    11. import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;  
    12. import org.springframework.context.annotation.Bean;  
    13.   
    14. /** 
    15.  * SpringBoot2.0启动程序 
    16.  * @author wallimn,http://wallimn.iteye.com 
    17.  * 
    18.  */  
    19. @SpringBootApplication  
    20. public class AssetApplication {  
    21.   
    22.     public static void main(String[] args) {  
    23.         SpringApplication.run(AssetApplication.class, args);  
    24.     }  
    25.     //如果没有使用默认值80  
    26.     @Value("${http.port:80}")  
    27.     Integer httpPort;  
    28.   
    29.     //正常启用的https端口 如443  
    30.     @Value("${server.port}")  
    31.     Integer httpsPort;  
    32.   
    33.     // springboot2 写法  
    34.     @Bean  
    35.     @ConditionalOnProperty(name="condition.http2https",havingValue="true", matchIfMissing=false)  
    36.     public TomcatServletWebServerFactory servletContainer() {  
    37.         TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory() {  
    38.             @Override  
    39.             protected void postProcessContext(Context context) {  
    40.                 SecurityConstraint constraint = new SecurityConstraint();  
    41.                 constraint.setUserConstraint("CONFIDENTIAL");  
    42.                 SecurityCollection collection = new SecurityCollection();  
    43.                 collection.addPattern("/*");  
    44.                 constraint.addCollection(collection);  
    45.                 context.addConstraint(constraint);  
    46.             }  
    47.         };  
    48.         tomcat.addAdditionalTomcatConnectors(httpConnector());  
    49.         return tomcat;  
    50.     }  
    51.   
    52.     @Bean  
    53.     @ConditionalOnProperty(name="condition.http2https",havingValue="true", matchIfMissing=false)  
    54.     public Connector httpConnector() {  
    55.         System.out.println("启用http转https协议,http端口:"+this.httpPort+",https端口:"+this.httpsPort);  
    56.         Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");  
    57.         connector.setScheme("http");  
    58.         //Connector监听的http的端口号  
    59.         connector.setPort(httpPort);  
    60.         connector.setSecure(false);  
    61.         //监听到http的端口号后转向到的https的端口号  
    62.         connector.setRedirectPort(httpsPort);  
    63.         return connector;  
    64.     }}  



    二、配置文件 

    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 

  • 相关阅读:
    利用MsChart控件绘制多曲线图表
    Silverlight与WCF通信(三) :Silverlight与IIS宿主的WCF间的双工通信
    Silverlight与WCF通信(二) :Silverlight通过netTcpBinding访问IIS宿主WCF
    Silverlight与WCF通信(一) :Silverlight通过httpBinding访问IIS宿主WCF
    Silverlight与WCF通信(四) :Silverlight访问控制台宿主WCF
    Highcharts配合Jquery ajax 请求本页面 陈
    WCF netTcpBinding寄宿到IIS7 陈
    修改ATI显卡全屏 陈
    鼠标画出矩形
    检查进程是否存在
  • 原文地址:https://www.cnblogs.com/exmyth/p/11347928.html
Copyright © 2011-2022 走看看