第一步 添加配置类
/**
* HTTPS域名访问配置
*/
@Configuration
public class TomcatConfig {
@Bean
public TomcatServletWebServerFactory servletContainer() {
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(httpConnector());
return tomcat;
}
@Bean
public Connector httpConnector() {
Connector c = new Connector("org.apache.coyote.http11.Http11NioProtocol");
//http重定向https
c.setScheme("http");
c.setPort(); //http访问端口
c.setSecure(false);
c.setRedirectPort(); //服务端口
return c;
}
}
第二步 application.yml文件配置ssl
前提条件:把 jks 文件放到resources目录下
#ssl
ssl:
key-store: classpath:***.jks
key-store-password: ***
key-store-type: JKS