1、yml
server:
port: 8181
https:
port: 8282
ssl:
key-store: classpath:sample.jks
key-store-password: secret
key-password: password
2、啓動類添加
@Value("${https.port}")
private Integer port;
@Value("${https.ssl.key-store-password}")
private String key_store_password;
@Value("${https.ssl.key-password}")
private String key_password;
@Value("${https.ssl.key-store}")
private String key;
@Bean
public EmbeddedServletContainerFactory servletContainer() {
TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory();
tomcat.addAdditionalTomcatConnectors(createSslConnector());
return tomcat;
}
private Connector createSslConnector() {
Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
Http11NioProtocol protocol = (Http11NioProtocol) connector.getProtocolHandler();
try {
connector.setScheme("https");
connector.setSecure(true);
connector.setPort(port);
protocol.setSSLEnabled(true);
protocol.setKeystoreFile(key);
protocol.setKeystorePass(key_store_password);
protocol.setKeyPass(key_password);
return connector;
}
catch (IOException ex) {
throw new IllegalStateException("can't access keystore: [" + "keystore"
+ "] or truststore: [" + "keystore" + "]", ex);
}
}
springboot 2.x
@Bean
public ServletWebServerFactory servletContainer() {
TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
tomcat.addAdditionalTomcatConnectors(createSslConnector()); // 添加http
return tomcat;
}