1.在application启动类文件中新增(直接复制不需要修改)
/**
* http重定向到https
* @return
*/
@Bean
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
public Connector httpConnector() {
Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
connector.setScheme("http");
//Connector监听的http的端口号
connector.setPort(8080);
connector.setSecure(false);
//监听到http的端口号后转向到的https的端口号
connector.setRedirectPort(443);
return connector;
}
2.在application.yml配置文件中新增(需要修改value值)
server:
port: 443
ssl:
key-store: classpath:www.*****.cn.jks #该value是下载下来tomcat文件夹中的文件名称
key-store-type: JKS # 文件格式
key-store-password: kx2123m4520 #(密码在keystorePass.txt中)
3.把jks文件放在项目的resource根目录下