1、生成keystore:参考https://blog.csdn.net/Dopamy_BusyMonkey/article/details/81368620
2、添加配置:
#server
server.port=8081
server.ssl.key-store=code.keystore
server.ssl.key-store-password=123456
server.ssl.keyStoreType=JKS
server.ssl.enabled=true
server.ssl.keyAlias:www.busy.com
3、添加代理方法:
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.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@SpringBootApplication
public class WecBootApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(WecBootApplication.class, args);
}
@RequestMapping("/")
public String hello() {
return "Hello World!";
}
@Bean
public Connector connector(){
Connector connector=new Connector("org.apache.coyote.http11.Http11NioProtocol");
connector.setScheme("http");
connector.setPort(8082);
connector.setSecure(false);
connector.setRedirectPort(8081);
return connector;
}
@Bean
public TomcatServletWebServerFactory tomcatServletWebServerFactory(Connector connector){
TomcatServletWebServerFactory tomcat=new TomcatServletWebServerFactory(){
@Override
protected void postProcessContext(Context context) {
SecurityConstraint securityConstraint=new SecurityConstraint();
securityConstraint.setUserConstraint("CONFIDENTIAL");
SecurityCollection collection=new SecurityCollection();
collection.addPattern("/*");
securityConstraint.addCollection(collection);
context.addConstraint(securityConstraint);
}
};
tomcat.addAdditionalTomcatConnectors(connector);
return tomcat;
}
}
4、结果:访问http://127.0.0.1:8082会自动跳转到https://127.0.0.1:8081