1、从阿里云或者腾讯云下载证书文件(java主要使用tomcat证书)
2、将证书压缩文件解压,解压后文件夹内有两部分内容:
其中后缀为pfx的文件为证书文件,password.txt文件内容为一个密码字符串
3、将后缀为pfx的证书文件放入项目的根目录下
4、在yml配置文件中添加ssl配置,配置内容如下:
server:
ssl:
key-store: 证书文件名.pfx
key-store-password: 密码文本文件内内容
5、添加配置类
@Configuration
public class SSLConfig {
@Bean
public TomcatServletWebServerFactory tomcatServletWebServerFactory() {
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.Http11NioProtocol");
connector.setScheme("http");
connector.setSecure(false);
connector.setRedirectPort(8098);
return connector;
}
}