Spring Boot配置支持https

一般来说都是在Nginx上配证书的

也有的情况是需要直接在Spring Boot上直接搞起

# https
server.port: 443
server.ssl:
  key-store: "classpath:server.pfx"
  key-store-password: q0yDlrAB
  keyStoreType: PKCS12

证书放在server/resource根目录下的server.pfx

如果想同时开启http支持:

@Configuration
public class HttpTomcatConfiguration {

    private static final int HTTP_PORT = 8080;

    @Bean
    public EmbeddedServletContainerCustomizer customizeTomcatConnector() {
        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(HTTP_PORT);
                    containerFactory.addAdditionalTomcatConnectors(connector);
                }
            }
        };
    }


}

 

Leave a Reply

Your email address will not be published.