Enable both http and https on Spring Boot
Building on How to Enable HTTP and HTTPS in Spring Boot which is a bit outdated nowadays.
To enable support for HTTP and HTTPS in Spring Boot 2, we need to register an additional connector with Spring Boot application.
First, enable SSL/HTTPS for Spring Boot, for example by following the HTTPS using Self-Signed Certificate in Spring Boot tutorial.
Now, add server.http.port=8080
to application.properties
.
To add another Tomcat connector, create the following configuration class:
import org.apache.catalina.connector.Connector;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class AppConfiguration {
@Bean
public WebServerFactoryCustomizer<TomcatServletWebServerFactory> cookieProcessorCustomizer() {
return (TomcatServletWebServerFactory factory) -> {
// also listen on http
final Connector connector = new Connector();
connector.setPort(httpPort);
factory.addAdditionalTomcatConnectors(connector);
};
}
@Value("${server.http.port}")
private int httpPort;
}
Written on August 25, 2021