springboot 配置http和https同时访问
- 使用jdk自带keytool工具生成jks证书
keytool -genkeypair -alias zhan -keypass zhan123 -keyalg RSA -keysize 1024 -validity 365 -keystore E:/idea_work/springsecurity20210902/demo04/src/main/resources/zhan.jks -storepass nana123
- yml配置
server:
port: 8443
servlet:
context-path: /security
#ssl
ssl:
# 密钥库的路径
key-store: classpath:zhan.jks
# 密钥库类型
key-store-type: JKS
# 密钥库中密钥的别名
key-alias: zhan
# 用于访问密钥库中密钥的密码
key-password: zhan123
# 用于访问密钥库的密码
key-store-password: nana123
- 由于配置文件中增加了https协议,因此http协议增加一个配置类即可
@Bean
public ServletWebServerFactory servletContainer() {
TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
// tomcat.addConnectorCustomizers(new GwsTomcatConnectionCustomizer());
tomcat.addAdditionalTomcatConnectors(newHttpConnector());
return tomcat;
}
private Connector newHttpConnector() {
Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
connector.setScheme("http");
connector.setPort(8080);
connector.setSecure(true);
return connector;
}
- 启动结果如下图: