我发现自己很难为我们的 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?
我可能错过了一些东西,但我不知道是什么