After SpringBoot 2.0, the way to enable the https protocol is a bit different from when 1.*, so post the code.
My code is able to determine whether to enable the https protocol based on the condition.http2https in the configuration parameters. If the https protocol is enabled, all accesses to the https protocol will be automatically transferred to the https protocol.
1. Start the program
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 starter* @author wallimn, http://wallimn.iteye.com * */ @SpringBootApplication public class AssetApplication { public static void main(String[] args) { SpringApplication.run(AssetApplication.class, args); } //If the default value is not used 80 @Value("${http.port:80}") Integer httpPort; //The normally enabled https port is such as 443 @Value("${server.port}") Integer httpsPort; // springboot2 writing method @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("Enable http to https protocol, http port: "+this.httpPort+", https port: "+this.httpsPort); Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol"); connector.setScheme("http"); //The port number of http listened to by Connector.setPort(httpPort); connector.setSecure(false); //The port number of https that you turn to after listening to the port number of https connected.setRedirectPort(httpsPort); return connector; }}2. Configuration file
1. Configuration when using http protocol
server.port=80
2. Configuration when using https and http protocols
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
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.