欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

SpringBoot2.0如何启用https协议

程序员文章站 2024-02-13 23:34:34
springboot2.0之后,启用https协议的方式与1.*时有点儿不同,贴一下代码。 我的代码能够根据配置参数中的condition.http2https,确...

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 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。