【问题标题】:Spring Boot SSL Configured, Server up but not able to connect to PortSpring Boot SSL 已配置,服务器启动但无法连接到端口
【发布时间】:2019-05-21 14:40:18
【问题描述】:

我已尝试使用最新版本启动应用程序,想要确保其余 api ssl 安全,我已在下面完成 创建keystore并放入项目classpath,服务器启动,启动没有问题,但无法发送请求8080或8443,下面是配置,

server.ssl.key-store=KeyStore.p12 server.ssl.key-store-password=shashank server.ssl.key-alias=mydomain server.ssl.key-password=shashank

@Bean
   public TomcatServletWebServerFactory servletContainer() {
      TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory() {
   @Override
   protected void postProcessContext(Context context) {
   SecurityConstraint securityConstraint = new SecurityConstraint();
   securityConstraint.setUserConstraint("CONFIDENTIAL");
   SecurityCollection collection = new SecurityCollection();
   collection.addPattern("/*");
   securityConstraint.addCollection(collection);
   context.addConstraint(securityConstraint);
   }
   };
   tomcat.addAdditionalTomcatConnectors(getHttpConnector());
   return tomcat;
   }

  private Connector getHttpConnector() {
        Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
        connector.setScheme("https");
        connector.setPort(8080);
        connector.setSecure(true);
        connector.setRedirectPort(8443);

}

信息 84898 --- [主要] o.s.b.w.embedded.tomcat.TomcatWebServer :Tomcat 在端口上启动:enter image description here 8443 (https) 8080 (https) 带有上下文路径'/event-processing'

由于这是自签名证书,它显示“此证书未经第三方验证”

这里的目的是让所有rest api的https enter image description here

【问题讨论】:

    标签: java spring-boot ssl tomcat


    【解决方案1】:

    试试这些改变:

    修改application.properties,将server.ssl.key-store参数值从KeyStore.p12修改为keystore.p12

    server.ssl.key-store: keystore.p12
    

    将 TomcatEmbeddedServletContainerFactory bean 添加到 @Configuration 类(任何一个)。

     @Bean
      public EmbeddedServletContainerFactory servletContainer() {
        TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory() {
            @Override
            protected void postProcessContext(Context context) {
              SecurityConstraint securityConstraint = new SecurityConstraint();
              securityConstraint.setUserConstraint("CONFIDENTIAL");
              SecurityCollection collection = new SecurityCollection();
              collection.addPattern("/*");
              securityConstraint.addCollection(collection);
              context.addConstraint(securityConstraint);
            }
          };
    
        tomcat.addAdditionalTomcatConnectors(initiateHttpConnector());
        return tomcat;
      }
    
      private Connector initiateHttpConnector() {
        Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
        connector.setScheme("http");
        connector.setPort(8080);
        connector.setSecure(false);
        connector.setRedirectPort(8443);
    
        return connector;
      }
    

    【讨论】:

      【解决方案2】:

      我在使用自签名证书时遇到了这个问题,并通过在服务器机器上而不是在我的本地机器上制作证书来解决这个问题,因此您应该运行在服务器机器上制作证书的 keytool 命令并使用该 .p12在您的项目中生成文件,一切都会按预期工作。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-10-23
        • 2018-05-12
        • 1970-01-01
        • 1970-01-01
        • 2019-01-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多