Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How set up Spring Boot to run HTTPS / HTTP ports

Spring boot have some properties to config web port and SSL settings, but once a SSL certificate is set the http port turns into https port.

So, how can I keep both ports running on it, for example: 80 an 443 at the same time?

As you can see, there only properties for one port, in this case "server.ssl" is enabled, what makes http port be disabled automatically.

############## ### Server ### ############## server.port=9043 server.session-timeout=1800 server.ssl.key-store=file:///C:/Temp/config/localhost.jks server.ssl.key-store-password=localhost server.ssl.key-password=localhost server.ssl.trust-store=file:///C:/Temp/config/localhost.jks server.ssl.trust-store-password=localhost 

I am trying to use even Tomcat or Undertow. I'd appreciate any help!

like image 547
Carlos Alberto Avatar asked Jun 17 '15 15:06

Carlos Alberto


People also ask

How do I enable HTTP Security in spring?

The first thing you need to do is add Spring Security to the classpath. The WebSecurityConfig class is annotated with @EnableWebSecurity to enable Spring Security's web security support and provide the Spring MVC integration.


2 Answers

Spring Boot configuration using properties, allows configuring only one connector. What you need is multiple connectors and for this, you have to write a Configuration class. Follow instructions in

https://docs.spring.io/spring-boot/docs/1.2.3.RELEASE/reference/html/howto-embedded-servlet-containers.html

You can find a working example of configuring HTTPS through properties and then HTTP though EmbeddedServletContainerCustomizer below

http://izeye.blogspot.com/2015/01/configure-http-and-https-in-spring-boot.html?showComment=1461632100718#c4988529876932015554

server:   port: 8080   ssl:     enabled: true     keyStoreType: PKCS12     key-store: /path/to/keystore.p12     key-store-password: password   http:     port: 8079 

@Configuration public class TomcatConfig {  @Value("${server.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);             }         }     }; } } 
like image 174
Harish Gokavarapu Avatar answered Sep 20 '22 08:09

Harish Gokavarapu


The currently accepted answer works perfectly but needs some adaption if you want it to work with Spring Boot 2.0.0 and onwards:

@Component public class HttpServer {   @Bean   public ServletWebServerFactory servletContainer(@Value("${server.http.port}") int httpPort) {       Connector connector = new Connector(TomcatServletWebServerFactory.DEFAULT_PROTOCOL);       connector.setPort(httpPort);        TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();       tomcat.addAdditionalTomcatConnectors(connector);       return tomcat;   } } 

or the Kotlin version:

@Component class HttpServer {   @Bean   fun servletContainer(@Value("\${server.http.port}") httpPort: Int): ServletWebServerFactory {     val connector = Connector(TomcatServletWebServerFactory.DEFAULT_PROTOCOL)     connector.setPort(httpPort)      val tomcat = TomcatServletWebServerFactory()     tomcat.addAdditionalTomcatConnectors(connector)     return tomcat   } } 
like image 34
NickDK Avatar answered Sep 20 '22 08:09

NickDK