Tuesday, January 27, 2015

Configure HTTP and HTTPS in Spring Boot

To configure HTTP and HTTPS in Spring Boot,

add the following properties in application.properties:

http.port=80

server.port=443
server.ssl.key-store=/home/izeye/keystore
server.ssl.key-store-password=keystore
server.ssl.key-password=privatekey

and add an embedded servlet container customer as follows:

@Configuration
public class TomcatConfig {

    @Value("${http.port}")
    private int httpPort;

    @Bean
    public EmbeddedServletContainerCustomizer containerCustomizer() {
        return new EmbeddedServletContainerCustomizer() {
            @Override
            public void customize(ConfigurableEmbeddedServletContainer container) {
                if (container instanceof TomcatEmbeddedServletContainerFactory) {
                    TomcatEmbeddedServletContainerFactory containerFactory =
                            (TomcatEmbeddedServletContainerFactory) container;

                    Connector connector = new Connector(TomcatEmbeddedServletContainerFactory.DEFAULT_PROTOCOL);
                    connector.setPort(httpPort);
                    containerFactory.addAdditionalTomcatConnectors(connector);
                }
            }
        };
    }

}

Reference:
http://izeye.blogspot.kr/2015/01/configure-https-with-self-signed.html

3 comments: