一、只能https访问,http访问不了。
1、JDK生成ssl证书,别名tomcathttps type:JKS
jdk1.8.0_231\bin下cmd运行: keytool -genkey -alias tomcathttps -keyalg RSA -keysize 2048 -keystore D:\javaboy.p13 -validity 3650
2、把生成的javaboy.p13文件复制到项目的 resources 目录下。
3、application.properties 文件中添加如下配置:
server.ssl.key-store=classpath:javaboy.p13
server.ssl.key-password=123456
server.ssl.key-store-type=JKS
server.ssl.key-alias=tomcathttps
二、http、https可不同端口同时访问。(暂时未测试)
1、配置http→https转向,在启动类中配置:
@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(connector());
return tomcat;
}
@Bean
public Connector connector() {
Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocal");
connector.setScheme("http");
connector.setPort(443);
connector.setSecure(false);
connector.setRedirectPort(7998);
return connector;
}