【问题标题】:How to run Spring Boot HTTPS server on multiple HTTPS ports如何在多个 HTTPS 端口上运行 Spring Boot HTTPS 服务器
【发布时间】:2021-03-01 10:49:28
【问题描述】:

我需要使用 HTTPS 在 2 个或更多端口上运行同一服务器。在早期生产中,我们为服务配置了 10500 端口。目前,我们需要在启用 SSL 的 443 和 10500 上运行它。

我找到了许多资源来为 Spring Boot 启用 HTTP 和 HTTPS。但我找不到任何允许在 2 个或更多端口上启用 HTTPS 服务的方法。

我已尝试配置端口重定向。但这也没有用。它在使用 HTTP 手动连接到端口 443 时有效。但是每当使用 HTTPS 时,程序都会抛出异常。

java.lang.IllegalArgumentException异常:无效字符在方法名实测值[0x160x030x010x020x000x010x000x010xfc0x030x030x810x00aC0x1b0x10`0xb80x8d0xae0x9e0xe40xc7V0xf60x08:e0xcc0x8f

@Bean
public ServletWebServerFactory 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(oldPortRedirectConnector());
  return tomcat;
}

private Connector oldPortRedirectConnector() {
  Connector connector = new Connector(TomcatServletWebServerFactory.DEFAULT_PROTOCOL);
  connector.setScheme("https");
  connector.setPort(443);
  connector.setSecure(true);
  connector.setRedirectPort(10500);
  return connector;
}

application.properties

server.port=10500

非常感谢任何帮助。在 2 个端口上运行相同的服务或从一个 HTTPS 端口转发到另一个端口对我们来说真的很棒。

【问题讨论】:

标签: java spring spring-boot


【解决方案1】:

最后,我得到了它的工作。问题是,即使我使用 HTTPS 方案添加了额外的连接器,也没有为该额外的连接器设置 SSL 配置。

通过设置 SSLHostConfig,我们可以拥有尽可能多的额外 https 端口。

@Bean
public ServletWebServerFactory 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(extraHttpsConnector());
  return tomcat;
}

private Connector extraHttpsConnector() {
  Connector connector = new Connector();
  connector.setScheme("https");
  connector.setPort(443);
  connector.setSecure(true);
  connector.setProperty("SSLEnabled", "true");

  //Add SSL configuration to your extra connector
  SSLHostConfig sslConfig = new SSLHostConfig();
  SSLHostConfigCertificate certConfig = new SSLHostConfigCertificate(sslConfig, Type.RSA);
  certConfig.setCertificateKeystoreFile("YOUR_KEYSTORE");
  certConfig.setCertificateKeystorePassword("YOUR_KEYSTORE_PASSWORD");
  certConfig.setCertificateKeyAlias("YOUR_KEYSTORE_ALIAS");
  sslConfig.addCertificate(certConfig);

  //Link the configuration to the connector
  connector.addSslHostConfig(sslConfig);
  return connector;
}

【讨论】:

    猜你喜欢
    • 2015-09-02
    • 2016-01-17
    • 1970-01-01
    • 2020-09-15
    • 2021-05-10
    • 2020-04-21
    • 1970-01-01
    • 2021-02-17
    • 2021-01-31
    相关资源
    最近更新 更多