We have re-written our webservices with Vert.x 4 and we're more than satisfied. Before putting them in production we want to secure them and we're trying to enable https. This is the main verticle:
public class MainVerticle extends AbstractVerticle {
@Override
public void start() throws Exception {
//Deploy the HTTP server
vertx.deployVerticle(
"com.albertomiola.equations.http.HttpServerVerticle",
new DeploymentOptions().setInstances(3)
);
}
// I use this only in IntelliJ Idea because when I hit "Run" the build starts
public static void main(String[] args) {
Launcher.executeCommand("run", MainVerticle.class.getName());
}
}
And this is the most relevant part of the code of the HTTP server verticle:
public class HttpServerVerticle extends AbstractVerticle {
@Override
public void start(Promise<Void> startPromise) throws Exception {
var options = new HttpServerOptions();
options.setPort(443)
.setSSl(true)
.setPemTrustOptions(...)
.setPemKeyCertOptions(...);
var server = vertx.createHttpServer(options);
var router = Router.router(vertx);
//Start
server
.requestHandler(router)
.listen(portNumber, ar -> {
if (ar.succeeded()) {
startPromise.complete();
} else {
startPromise.fail(ar.cause());
}
});
}
}
The above code works fine because my website is reachable at https://website.it and https://www.website.it (with letsencrypt certs).
The problem is that when I try to access http://website.it or http://www.website.it it doesn't work (meaning that I can't see the home because the server is unreachable).
How can I redirect http://website.it to https//website.it?
I have googled a lot and what I've managed to find is:
- this vertx example that setups HTTPS as I do but it does not mention the redirect
- this SO question that seems to tell me what to do but I cannot understand what to do and I am not sure if that setup has to go in the HttpServerOption object
Maybe is my https configuration wrong? I am using Java 11 on IntelliJ IDEA (Maven build) and Vert.x 4 latest version. Thank you