2

I'm not quite sure if this is possible - all examples I find online are deprecates as e.g. the property security.http.port does not seem to exist today in Spring Boot 2.

What I need is a way to run Tomcat on two ports e.g. 8443 for HTTPS and 8080 for HTTP.

Also I'd want everything under /api to require HTTPS except /api/webhooks (or at least /webhooks as I can still change the endpoint if I wanted).

I was imagining something like this:

@Override
public void configure(HttpSecurity http) throws Exception {

    http
            .requiresChannel()
                .antMatchers("/api/*/webhooks/**")
                    .requiresInsecure()
         .and()
            .requiresChannel()
                .antMatchers("/**")
                    .requiresSecure() 
                .and()
                    .exceptionHandling()
                .and()
                    .authorizeRequests()
                        .antMatchers("/api/*/public/**", "oauth/**")
                            .permitAll()
                        .antMatchers(HttpMethod.GET, "/*", "/assets/*")
                            .permitAll()
                        .antMatchers("/api/**")
                            .authenticated();
}

Can this be done? This question has a background - I am trying to accept webhook calls which are coming in as HTTP POST requests.

2

1 Answer 1

3

It is the way to open http and https at same time.

Under your JAVA environment, generate a SSL key

keytool -genkey -alias tomcat -dname "CN=Andy" -storetype PKCS12 -keyalg RSA -keysize 2048 -keystore keystore.p12 -validity 365

And copy the file to your resource directory

your properties should be like this

# https port
server.port=8443
# http port
server.http.port=8080
# key path
server.ssl.key-store=classpath:keystore.p12
# your key password
server.ssl.key-store-password=mytest
server.ssl.keyStoreType=PKCS12
server.ssl.keyAlias=tomcat

Configuration for additional http port

@Configuration
public class HttpConfig {
    @Value("${server.http.port}")
    private int httpPort;

    @Bean // (it only works for springboot 2.x)
    public ServletWebServerFactory servletContainer(){
        TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory();
        factory.addAdditionalTomcatConnectors(createStanderConnecter());
        return factory;
    }

    private Connector createStanderConnecter(){
        Connector connector = 
        //new Connector("org.apache.coyote.http11.Http11NioProtocol");
        new Connector(TomcatServletWebServerFactory.DEFAULT_PROTOCOL);
        connector.setPort(httpPort);
        return connector;
    }
}
2
  • Indeed that worked. Although you can replace that nasty hardcoded String with TomcatServletWebServerFactory.DEFAULT_PROTOCOL. Commented Dec 29, 2018 at 10:42
  • This does not work for me. When hitting localhost:8080 it does not response. It responds OK to localhost:8443 though. Also, the startup message says Tomcat initialized with port(s): 8443 (https). I would expect to list both http and https ports. What I'm I missing?
    – hypercube
    Commented Jul 2 at 17:35

Not the answer you're looking for? Browse other questions tagged or ask your own question.