Akka-HTTP server HTTPS Support

I found myself struggling to enable https support for our akka-http java application. The documentation page (https://doc.akka.io/docs/akka-http/current/server-side/server-https-support.html) should be helping, but for some reason in my case it does not.

From what I got from that page - the idea is to obtain an HttpsConnectionContext instance and use it as a parameter for the http.setDefaultClientHttpsContext(...) method call before actually doing some route binding.

What I have so far
Code that creates an SSLContext from the configuration:

public static SSLContext getSslContext(String path, ActorSystem system) {
    final AkkaSSLConfig sslConfig = AkkaSSLConfig.get(system);

    final Config overrides = system.settings().config().getConfig(path);
    final Config defaults = system.settings().config().getConfig("ssl-config");

    final SSLConfigSettings config = SSLConfigFactory.parse(overrides.withFallback(defaults));

    return new ConfigSSLContextBuilder(
        new AkkaLoggerFactory(system),
        config,
        sslConfig.buildKeyManagerFactory(config),
        sslConfig.buildTrustManagerFactory(config)
    ).build();
}

Actual peace of configuration:

akka.ssl-config {
  trustManager = {
    stores = [
      { type: "JKS" , path: "xxx-truststore.jks", password = "xxx"}
    ]
  }
  keyManager = {
    stores = [
      { type: "JKS" , path: "xxx-keystore.jks", password = "xxx"}
    ]
  }
}

Using them to enable https:

final SSLContext sslContext = getSslContext("akka.ssl-config", system);
final HttpsConnectionContext httpsContext = ConnectionContext.https(sslContext);
http.setDefaultClientHttpsContext(httpsContext);

final CompletionStage<ServerBinding> binding =
    http.bindAndHandle(routeFlow, ConnectHttp.toHost("localhost",443), materializer);

But that actually does nothing. If I try to make some https call to the server I get the following exception:

Illegal request, responding with status '400 Bad Request': Unsupported HTTP method: The HTTP method started with 0x16 rather than any known HTTP method. Perhaps this was an HTTPS request sent to an HTTP endpoint?

I’m probably missing something, but I have no idea what

looks like my fault after all)
the method to use is obviously http.setDefaultServerHttpContext(...) not http.setDefaultClientHttpsContext(...)

1 Like

Did you try to hit few requests with postman?

I have created similar application but when I am using Postman to send request, It’s not failing it and just letting it in.

I tried POST request as well as WSS request.

Please let me know your outcome.

seem fixed by using the following

engine.setNeedClientAuth(true);