Spring Boot配置支持https

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

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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# https
server.port: 443
server.ssl:
key-store: "classpath:server.pfx"
key-store-password: q0yDlrAB
keyStoreType: PKCS12
# https server.port: 443 server.ssl: key-store: "classpath:server.pfx" key-store-password: q0yDlrAB keyStoreType: PKCS12
# https
server.port: 443
server.ssl:
  key-store: "classpath:server.pfx"
  key-store-password: q0yDlrAB
  keyStoreType: PKCS12

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

如果想同时开启http支持:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
@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);
}
}
};
}
}
@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); } } }; } }
@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);
                }
            }
        };
    }


}

另:https证书转化(key + pem 转 pfx,可以用这个网站 https://www.sslshopper.com/ssl-converter.html)

Leave a Reply

Your email address will not be published. Required fields are marked *