Springboot built-in tomcat configuration HTTPS certificate
1. Generate SSL certificate
two。 Put the certificate under the root of the project, for example, when I installed the certificate for Anxin SSL website, it was placed in:
3. Put the certificate in the project resources directory, the same directory as application.properties, and then modify the application.properties file to add HTTPS support. Add the following code to application.properties:
Server:
Port:443 specifies the https port number
Server:
Key-store Certificate name
Key-alias alias
Key-store-type certificate type
Key-store-password certificate password
Enabled: true allows requests through https
4. Add the following code to the configuration class:
Package com.bootdo.common.config
Import org.apache.catalina.Context
Import org.apache.catalina.connector.Connector
Import org.apache.tomcat.util.descriptor.web.SecurityCollection
Import org.apache.tomcat.util.descriptor.web.SecurityConstraint
Import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer
Import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory
Import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory
Import org.springframework.context.annotation.Bean
Import org.springframework.context.annotation.Configuration
@ Configuration
Public class HttpsConfig {
@ Bean
Public EmbeddedServletContainerFactory servletContainer () {
TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory () {
@ 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 (getHttpConnector ())
Return tomcat
}
Private Connector getHttpConnector () {
Connector connector = new Connector ("org.apache.coyote.http11.Http11NioProtocol")
Connector.setScheme ("http")
Connector.setPort (80)
Connector.setSecure (false)
Connector.setRedirectPort (443)
Return connector
}
Public void customize (ConfigurableEmbeddedServletContainer container) {
Container.setPort (443)
}
}
5. If you upload to the server, press the certificate file in the place where the server puts the jar package:
Article source: http://baijiahao.baidu.com/builder/preview/s?id=1627774493795275221