0

I need to run the same server on 2 or more ports with HTTPS. In the early production, we had configured port 10500 for services. Currently, we need to run it both on 443 and 10500 with SSL enabled.

I have found many resources for enabling HTTP and HTTPS for spring boot. But I couldn't find any method that allows enabling HTTPS services on 2 or more ports.

I have tried configuring port redirection. But that also didn't work. It works when manually connecting to port 443 with HTTP. But whenever HTTPS is used, the program throws an exception.

java.lang.IllegalArgumentException: Invalid character found in method name [0x160x030x010x020x000x010x000x010xfc0x030x030x810x00aC0x1b0x10`0xb80x8d0xae0x9e0xe40xc7V0xf60x08:e0xcc0x8f<0xf70x8b0xc2y0xa40xfe0xa3(0xc7-0xe6]. HTTP method names must be tokens

@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

Any help is greatly appreciated. Either running the same service on 2 ports or forwarding from one HTTPS port to another will be really great to have for us.

  • 2
    Have a look at this https://stackoverflow.com/questions/36357135/configure-spring-boot-with-two-ports – Jabir Nov 18 '20 at 07:07
  • 1
    Does this answer your question? [Configure Spring Boot with two ports](https://stackoverflow.com/questions/36357135/configure-spring-boot-with-two-ports) – Gaurav Jeswani Nov 18 '20 at 07:09
  • Thanks for the suggestions. But these answers only provide the option to configure extra HTTP ports. I would like to have additional HTTPS ports. – Muhammed Afsal Villan Nov 18 '20 at 07:17

1 Answers1

1

Finally, I got it working. The issue was that, even though I was adding the extra connector with the HTTPS scheme, the SSL configuration was not set for that extra connector.

By setting SSLHostConfig, we can have as many extra https ports as we need.

@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;
}