This is the 17th day of my participation in Gwen Challenge

Original: Telami’s Blog, welcome to share, reprint please reserve source.

Not long ago, I bought a new domain name, dapidEng.com, and recently I started a small project using SpringBoot. Most of the time, nginx was put first and the request was forwarded to the corresponding service. This time, we have a new server and we have not configured Nginx, so we just start java-JAR.

And when it’s deployed, huh? I haven’t done HTTPS yet.

In fact, in the previous blog, I have also mentioned the configuration of certificates, but this time it is springBoot, which has a built-in Tomcat container, which is not quite the same as packaging projects under Tomcat.

Certificate use ali cloud free certificate can, here is not repeated.

After the application, you can click download, here we must choose Tomcat.

You can also check out the corresponding help documentation

Once you have downloaded the certificates, place them in the Resource directory as a file ending in PFX.

Add the following configuration to application.properties:

server.port=443
server.ssl.enabled=true
server.ssl.key-store=classpath:2046023_www.dapideng.com.pfx
server.ssl.key-store-password=c6503VGY
server.ssl.keyStoreType=PKCS12
Copy the code

Inject the following two beans into the Application, which have changed since SpringBoot was updated to version 2.0. Most of the configuration from Baidu, or Google, is based on version 1.5.

@Bean
public Connector connector(a) {
    Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
    connector.setScheme("http");
    connector.setPort(80);
    connector.setSecure(false);
    connector.setRedirectPort(443);
    return connector;
}

@Bean
public TomcatServletWebServerFactory tomcatServletWebServerFactory(Connector connector) {
    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(connector);return tomcat;
}
Copy the code

Run the maven command to package.

maven clean package
Copy the code

Upload it to the server and run the command to start the service in the background.

nohup java  -jar demo.jar &
Copy the code

Done, perfect ~ 👏 👏