How to upgrade a SpringBoot website to HTTPS
In this issue, Xiaobian will bring you about how to upgrade SpringBoot website to HTTPS. The article is rich in content and analyzes and narrates from a professional perspective. After reading this article, I hope you can gain something.
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
The meaning of each parameter of the command:
-genkey: generate key;
-alias: alias of key;
-dname: Specifies certificate owner information
-storetype: Keystore type is JCEKS. JKS(default),JCEKS(recommended),PKCS12,BKS,UBER. Each keystore can be only one of these types.
-keyalg: DSA or RSA algorithm (when using-genkeypair parameter), DES or DESede or AES algorithm (when using-genseckey parameter);
-keysize: The key length is between 512 and 1024 (multiples of 64)
-keystore: Name of the certificate repository
-validity: specifies how many days the certificate created is valid
Details of dname:
CN(Common Name)
OU(Organization Unit Name)
O(Organization name)
L(Locality city or region name)
ST(State or Province)
C(Country name)
Under directory C:\Users\current user you will see a file keystore.p12
Enable HTTPS in Spring Boot
Configure HTTPS in application.properties.
#https port number
server.port=81
#http port number
http-port=82
#Path to certificate.
server.ssl.key-store=classpath:keystore.p12
#Certificate password, please change it to your own certificate password
server.ssl.key-store-password=123456
#Keystore Type
server.ssl.keyStoreType=PKCS12
#Certificate alias
server.ssl.keyAlias=tomcat
Redirect HTTP requests to HTTPS
/**
* @ClassName : TomcatHttpConfig
* @Description :
* @Author : YWR
* @Date: 2021-01-12 22:56
*/
@Configuration
public class TomcatHttpConfig {
@Value("${http-port}")
private int port;
@Value("${server.port}")
private int sslPort;
@Bean
public ServletWebServerFactory servletContainer() {
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(redirectConnector());
return tomcat;
}
private Connector redirectConnector() {
Connector connector = new Connector(TomcatServletWebServerFactory.DEFAULT_PROTOCOL);
connector.setScheme("http");
connector.setPort(port);
connector.setSecure(false);
connector.setRedirectPort(sslPort);
return connector;
}
}
The above is how to upgrade SpringBoot website to HTTPS shared by Xiaobian. If there is a similar doubt, please refer to the above analysis for understanding. If you want to know more about it, please pay attention to the industry information channel.