How set up Spring Boot to run HTTPS / HTTP ports

后端 未结 6 668
半阙折子戏
半阙折子戏 2020-12-12 23:58

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

6条回答
  •  佛祖请我去吃肉
    2020-12-13 00:48

    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);
                }
            }
        };
    }
    }
    

提交回复
热议问题