How to Enable HTTP/HTTPS on Spring Boot [Snippet]

DZone 's Guide to

How to Enable HTTP/HTTPS on Spring Boot [Snippet]

Learn how to enable HTTP and HTTPS on your Spring Boot applications with this tutorial on using the SSL layer for application configuration.

· Java Zone ·
Free Resource

You can configure Spring Boot services to be accessed over the SSL layer. And, the configuration can be done in the application configuration YML or property file. If there is ever a need to support the services so that they can be accessed in both the HTTP and HTTPS layer, you will have to plug in an additional connector.

By default, it allows only one connector to use the properties. The key to supporting both connectors would require some customization.

Let's take a look at the following interface:


This is the strategy interface for customizing auto-configured embedded servlet containers.
Any beans of this type will get a callback with the container factory before the container itself is started, so you can set the port, address, error pages, etc.

  • Create a bean that returns an instance of EmbeddedServletContainerCustomizer, overriding the customize method. Inside the customize method, we need to add additional support for the connectors. 

public class EmbeddedTomcatConfig {

    private int httpPort;

    public EmbeddedServletContainerCustomizer customizeTomcatConnector() {
        return new EmbeddedServletContainerCustomizer() {
            public void customize(ConfigurableEmbeddedServletContainer container) {
                if (container instanceof TomcatEmbeddedServletContainerFactory) {
                    TomcatEmbeddedServletContainerFactory containerFactory =
                            (TomcatEmbeddedServletContainerFactory) container;

                    Connector connector = new Connector(TomcatEmbeddedServletContainerFactory.DEFAULT_PROTOCOL);


  • Next, we need to establish the application.properties:


Now, your application should support both HTTP (8081) and HTTPS (8082).

The JKS should be placed in the application classpath, for example, the /src/resources  folder.

You can create self-signed certificates using the key-tool for testing purposes.

Please refer to how to convert SSL certificates to JKS here

I have shared the configuration for a Tomcat-embedded container. This code can always be tweaked for Jetty. 

Happy coding!

classpath ,configuration ,https ,java ,spring boot ,ssl ,ssl layer

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}