Springboot2.x配置ssl证书同时支持http和https两种协议

1.准备一份xxx.jks的证书一份,放置resources目录下
2.在application.yml配置证书的一些参数,以及项目的http端口和https接口 配置如下:
server:
  port: 8080
  custom:
    httpPort: 9090
  ssl:
    key-store: classpath:ssl.jks
    key-password: 123456
    key-store-type: JKS
    key-alias: huluwa
3.新建一个配置类HttpsConfig
@Configuration
public class HttpsConfig {

    @Value("${server.custom.httpPort}")
    private Integer httpPort;

    @Bean
    public ServletWebServerFactory serverFactory() {
        TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
        tomcat.addAdditionalTomcatConnectors(createStandardConnector());
        return tomcat;
    }
    /**
     * 配置http
     * @return
     */
    private Connector createStandardConnector() {
        Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
        connector.setPort(httpPort);
        return connector;
    }

}

此时就大功告成啦,试试用https 和http来访问下你的项目 看看是不是支持双协议啦

你可能感兴趣的:(java,springboot)