Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to configure in Spring Boot 2 (w/ WebFlux) two ports for HTTP and HTTPS?

Can anybody tell me how 2 ports (for HTTP and HTTPS) can be configured when using Spring Boot 2 and WebFlux? Any hint is appreciated!

like image 346
Juergen Zimmermann Avatar asked Jan 12 '18 07:01

Juergen Zimmermann


People also ask

How do I enable HTTP and HTTPS in Spring boot?

To see both HTTP and HTTPS in action, create a simple REST controller. Build and deploy your Spring boot application. Once you application is up and running, try to open these URL's. You will get a reply from both URL's since we have enabled both HTTP and HTTPS in our Spring Boot application.

How do I enable HTTP support in Spring boot?

To enable support for HTTP and HTTPS in Spring Boot 2, we need to register an additional connector with Spring Boot application. First, enable SSL/HTTPS for Spring Boot, for example by following the HTTPS using Self-Signed Certificate in Spring Boot tutorial. Now, add server.

How do I turn off HTTP in Spring boot?

Spring boot documentation claims that setting server. port=-1 disables http endpoint, but for me it behaves the same as if I used port=0.


1 Answers

This isn't directly supported by Spring Boot 2 yet.

But, you may be able to get it to work in a couple of ways.

By default, Spring Boot WebFlux uses Netty. If you are already configured for ssl, then Spring Boot will start up and open port 8443 (or whatever you have configured).

Then, to add 8080, you can do:

@Autowired
HttpHandler httpHandler;

WebServer http;

@PostConstruct
public void start() {
    ReactiveWebServerFactory factory = new NettyReactiveWebServerFactory(8080);
    this.http = factory.getWebServer(this.httpHandler);
    this.http.start();
}

@PreDestroy
public void stop() {
    this.http.stop();
}

Which is a bit clunky since your https configuration is in one spot (application.yml) and your http configuration is in Java config, but I have tested this myself with a toy application. Not sure how robust of a solution it is, though.

Another option that may work is to try the equivalent of other suggestions, but use the reactive version of the class, which is TomcatReactiveWebServerFactory. I'm not aware of any way to provide more than one connector for it, but you could possibly override its getWebServer method:

@Bean
TomcatReactiveWebServerFactory twoPorts() {
    return new TomcatReactiveWebServerFactory(8443) {
        @Override
        public WebServer getWebServer(HttpHandler httpHandler) {
           // .. copy lines from parent class
           // .. and add your own Connector, similar to how tutorials describe for servlet-based support
        }
    }
}

Also, a bit messy, and I have not tried that approach myself.

Of course, keep track of the ticket so you know when Spring Boot 2 provides official support.

like image 185
jzheaux Avatar answered Oct 07 '22 17:10

jzheaux