Configure https access
Generate a certificate
If you have configured a JAVA development environment, you can use the keytool command to generate a certificate. We open the console and enter:
keytool -genkey -alias tomcat -dname "CN=Andy,OU=kfit,O=kfit,L=HaiDian,ST=BeiJing,C=CN" -storetype PKCS12 -keyalg RSA -keysize 2048 -keystore keystore.p12 -validity 365
After entering, you will be prompted to enter a password, which is useful in the configuration file below.
After generation, find the certificate file in the home directory and copy it to src/main/resources of the SpringBoot application.
2. Add ssl configuration in application.properties of SpringBoot application:
#https端口号.
server.port=443
#证书的路径.
server.ssl.key-store=classpath:keystore.p12
#证书密码,请修改为您自己证书的密码.
server.ssl.key-store-password=123456(改为之前设置的密码)
#秘钥库类型
server.ssl.keyStoreType=PKCS12
#证书别名
server.ssl.keyAlias=tomcat
Start the SpringBoot application at this time and find that it can be accessed via https.
1.png
Configure http access
Since https was previously configured in the configuration file, http must be configured in the form of code. This configuration is relatively simple, just add a configuration class.
@Configuration
public class TomcatConfig {
@Value("${server.http.port}")
private int httpPort;
@Bean
public EmbeddedServletContainerCustomizer containerCustomizer() {
return new EmbeddedServletContainerCustomizer() {
@Override
public void customize(ConfigurableEmbeddedServletContainer container) {
if (container instanceof TomcatEmbeddedServletContainerFactory) {
TomcatEmbeddedServletContainerFactory containerFactory =
(TomcatEmbeddedServletContainerFactory) container;
Connector connector = new Connector(TomcatEmbeddedServletContainerFactory.DEFAULT_PROTOCOL);
connector.setPort(httpPort);
containerFactory.addAdditionalTomcatConnectors(connector);
}
}
};
}
}
Start the SpringBoot application at this time and find that it can also be accessed through http.
Author: slow travel the world
link: https: //www.jianshu.com/p/49bdcaf74513
Source: Jane books
are copyrighted by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.