spring boot 同时支持https 与http 两种协议

2018-12-18  文晶 

参考地址:https://www.cnblogs.com/lianggp/p/8136540.html

性能测试mockserver 支持http /hessian 协议mock 今天又提出某些业务在对接支付宝,微信时需要https协议。因此参考上述文章进行https 协议.

1、生成P12 文件

keytool -genkey -alias tomcat -storetype PKCS12 -keyalg RSA -keysize 2048 -keystore keystore.p12 -validity 3650

2、配置springboot yml文件

     minSpareThreads: 10
      maxSpareThreads: 100
      maxThreads: 1000
      maxConnections: 10000
      acceptCount: 1000
      maxHttpHeaderSize: 81920
  https:
    port: 8443
    ssl:
      key-store: /apps/ssl/keystore.p12
      key-store-password: 123456
      keyStoreType: PKCS12
      keyAlias: tomcat
      key_password: 123456


3、调整启动tomcat配置


@Bean
    public ServletWebServerFactory servletContainer() {
        TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
        tomcat.addAdditionalTomcatConnectors(createSslConnector()); // 添加http
        return tomcat;
    }


    // 配置https
    private Connector createSslConnector() {
        Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
        Http11NioProtocol protocol = (Http11NioProtocol) connector.getProtocolHandler();
        try {
//            File keystore = new ClassPathResource(key_store).getFile();
            File keystore = new File(key_store);
            connector.setScheme("https");
            connector.setSecure(true);
            connector.setPort(port);
            protocol.setSSLEnabled(true);
            protocol.setKeystoreFile(keystore.getAbsolutePath());
            protocol.setKeystorePass(key_store_password);
            protocol.setKeyPass(key_password);
            connector.setAttribute("acceptCount",acceptCount);
            connector.setAttribute("maxConnections",maxConnections);
            connector.setAttribute("maxThreads",maxThreads);
            connector.setAttribute("minSpareThreads",minSpareThreads);
            connector.setAttribute("maxSpareThreads",maxSpareThreads);
            connector.setAttribute("maxHttpHeaderSize",maxHttpHeaderSize);
            return connector;
        } catch (Exception ex) {
            throw new IllegalStateException("can't access keystore: [" + "keystore"
                    + "] or truststore: [" + "keystore" + "]", ex);
        }
    }

测试完成 虚拟机部署时遇到的问题:

1、部分服务器可正常启动,部分服务器不能启动查看日志中有错误信息:invalid keystore format

解决思路:1)考虑在拷贝过程中P12文件损坏,本机生成P12 重新启动仍是次错。

                2)参考其它信息说jre版本与tomcat jre版本不一致,所有出错机器,jdk版本均为1.8.0.51然而打包环境的jdk 版本为1.8.0.65 调整启动JDK版本启动正常(更新部署机器JDK)




510°/5107 人阅读/0 条评论 发表评论

登录 后发表评论