一、首先在 application.properties中添加代码
https.port=443
https.ssl.key-store=classpath:***.jks //域名证书 -我是在腾讯云申请的一年免费证书
https.ssl.key-alias=www.***.com //网站域名
https.ssl.enabled=true
https.ssl.key-store-password= *** //证书秘钥,
https.ssl.key-store-type=JKS
二、在启动页面添加如下代码:
@SpringBootApplication
public class AssociationApiApplication extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(AssociationApiApplication.class);
}
public static void main(String[] args) {
SpringApplication.run(AssociationApiApplication.class, args);
}
/**
* 配置http / https 协议
* @author juekf
* @data
*/
@Value("${https.port}")
private Integer port;
@Value("${https.ssl.key-store-password}")
private String key_store_password;
@Bean
public ServletWebServerFactory servletContainer() {
TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
tomcat.addAdditionalTomcatConnectors(createSslConnector()); // 添加http
return tomcat;
}
/**
*
* @title: createSslConnector
* @Descripyion: TODO (//配置https)
* @author juekf
* @data
* @return: Connector
* @return
*/
private Connector createSslConnector(){
Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
Http11NioProtocol protocol = (Http11NioProtocol) connector.getProtocolHandler();
try {
File keystore = new ClassPathResource("***.jks").getFile();
connector.setScheme("https");
connector.setSecure(true);
connector.setPort(port);
protocol.setSSLEnabled(true);
protocol.setKeystoreFile(keystore.getAbsolutePath());
protocol.setKeystorePass(key_store_password);
return connector;
} catch (IOException ex) {
throw new IllegalStateException(
"can't access keystore: [" + "keystore" + "] or truststore: [" + "keystore" + "]", ex);
}
}
}
三、在resources 文件夹下添加证书文件就OK了,