springboot2配置https
程序员文章站
2022-04-27 18:43:49
...
- 添加配置类HttpsConfig.java
```java
/**
* https SSL配置
*/
@Configuration
public class HttpsConfig {
@Value("${server.http-port}")
Integer httpPort;
@Value("${server.port}")
Integer httpsPort;
@Bean
public ServletWebServerFactory 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(createHTTPConnector());
return tomcat;
}
private Connector createHTTPConnector() {
Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
connector.setProperty("relaxedQueryChars", "[]{}");
connector.setScheme("http");
connector.setSecure(false);
// http 端口 8080
connector.setPort(httpPort);
// https端口 server.port 必须一致 8443 否则 会报 端口被占用异常
connector.setRedirectPort(httpsPort);
return connector;
}
}
- application.yml添加ssl配置
如果证书别名与密码不正确也会报错
#启动访问端口
server:
port: 8443
ssl:
key-store: E:\ssl\cert\certhttps.pfx # 也可以是其他文件格式
key-store-password: 123456
key-alias: certssl
http-port: 8080
-
如没有证书密码可利用jdk自带工具生成测试:
cd jdk/bin 目录执行
keytool -genkey -alias certssl -keyalg RSA -keysize 2048 -keystore E:\ssl\cert\certhttps.p12 -validity 3650
推荐阅读
-
mysql 5.7以上版本安装配置方法图文教程(mysql 5.7.12mysql 5.7.13mysql 5.7.14)
-
Eclipse怎么在线配置Hibernate Tools?
-
eclipse怎么删除配置好的多余的工作空间?
-
Mysql5.7.17 winx64.zip解压缩版安装配置图文教程
-
MySQL 5.6下table_open_cache参数优化合理配置详解
-
Centos7安装和配置Mysql5.7
-
eclipse怎么创建servlet中class并配置servlet教程
-
Android studio怎么配置代码检查规则?
-
Visual Studio怎么重新配置开发环境?
-
vb每次打开都让配置cad2007怎么办?