Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot: Enable HTTPS for embedded tomcat

Created keystore with following command:
keytool -genkey -alias tomcat -storetype PKCS12 -keyalg RSA -keysize 2048 -keystore keystore.p12 -validity 3650

Added following setting in the application.properties file:

server.ssl.key-store=keystore.p12
server.ssl.key-store-password=######
server.ssl.keyStoreType=PKCS12
server.ssl.keyAlias=tomcat
#server port config
server.port=8080
server.http.port=8081

Wrote following code:

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(initiateHttpConnector());
        return tomcat;
    }

Application starts fine without any error. And I can see the following message in the logs: s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (https) 8081 (http)

But when I send a request https://localhost:8081/hello, response sent back from server and there is no activity on server logs. Not sure what is going on.

like image 399
Himanshu Yadav Avatar asked Dec 19 '25 12:12

Himanshu Yadav


1 Answers

The startup message of the embedded Tomcat clearly states that your ssl/tls connection is running on the port you specified with server.port:

Tomcat started on port(s): 8080 (https)

So you are just having a wrong port/protocol combination. https://localhost:8080 should work.

But normally your browser should complain with a message stating this. Just out of curiosity you could check what happens in your browser when you call https://www.google.com:80

like image 195
Marged Avatar answered Dec 21 '25 07:12

Marged



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!