Akka-HTTP服务器HTTPS支持

ni65a41a  于 2022-11-06  发布在  其他
关注(0)|答案(1)|浏览(215)

我发现自己很难为akka-http java应用程序启用https支持,文档页面(https://doc.akka.io/docs/akka-http/current/server-side/server-https-support.html)应该会有所帮助,但由于某种原因,我的情况下它没有。
根据我从该页面获得的信息,我们的想法是获取一个HttpsConnectionContext示例,并在实际进行一些路由绑定之前将其用作http.setDefaultClientHttpsContext(...)方法调用的参数。
目前为止我所拥有的:
从配置创建SSLContext的代码:

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();
}

实际平安配置:

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

使用它们来启用https:

final SSLContext sslContext = Utils.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);

如果我尝试对服务器进行一些https调用,我会得到以下异常:
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?
我可能错过了什么,但我不知道是什么

zdwk9cvp

zdwk9cvp1#

毕竟看起来是我的错)要使用的方法显然是http.setDefaultServerHttpContext(...)而不是http.setDefaultClientHttpsContext(...)

相关问题