【发布时间】:2017-09-14 02:15:03
【问题描述】:
我正在使用来自第三方的 SSL 证书 - 我使用以下命令创建了一个 .p12 密钥库
openssl pkcs12 -export -CAfile Geotrust_EV_Intermediate_Bundle.crt -in www_domainName_in.crt -inkey domainName.in.key -out wtkeystore1.p12 -name CompanyName -passout pass:SomePassWord
我参考了 Akka HTTPS Support Docs 并编码如下
public HttpsConnectionContext useHttps(ActorSystem system) {
HttpsConnectionContext https = null;
try {
final char[] password = properties.keystorePassword().toCharArray();
final KeyStore ks = KeyStore.getInstance("PKCS12");
final InputStream keystore = WDService.class.getClassLoader().getResourceAsStream("wtkeystore.p12");
if (keystore == null) {
throw new RuntimeException("Keystore required!");
}
ks.load(keystore, password);
final KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance("SunX509");
keyManagerFactory.init(ks, password);
final TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
tmf.init(ks);
final SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(keyManagerFactory.getKeyManagers(), tmf.getTrustManagers(), new SecureRandom());
final AkkaSSLConfig sslConfig = AkkaSSLConfig.get(system);
https = ConnectionContext.https(sslContext);
} catch (NoSuchAlgorithmException | KeyManagementException e) {
system.log().error(e.getCause() + " while configuring HTTPS.", e);
} catch (CertificateException | KeyStoreException | UnrecoverableKeyException | IOException e) {
system.log().error(e.getCause() + " while ", e);
}
return https;
}
我的主文件代码如下
final Http http = Http.get(system);
log.info("Starting on " + properties.url() + ":" + properties.port());
final ConnectHttp host = ConnectHttp.toHost(properties.url(), properties.port());
Http.get(system).bindAndHandle(appRoute().flow(system, materializer), host, materializer);
log.info("Started on " + properties.url() + ":" + properties.port());
if (properties.useSSL()) {
HttpsConnectionContext https = useHttps(system);
http.setDefaultServerHttpContext(https);
Http.get(system).bindAndHandle(appRoute().flow(system, materializer),
ConnectHttp.toHost(properties.urlSSL(), properties.portSSL()), materializer);
log.info("Started on " + properties.urlSSL() + ":" + properties.portSSL());
}
现在我可以绑定到 Akka Http,并且根本不报告错误,但是我的 https 请求在服务器上被拒绝(它甚至没有到达 akka/http - 所以在 akka 系统中没有错误日志)和@ 987654321@ 工作正常。
问题:
我错过了上面的任何步骤吗?
我只使用 SSLContext - 可以吗,或者我也应该使用 SSLConfig 吗?如果是 - 那么我应该如何使用 SSLConfig,因为似乎没有提供适当的文档
- 是否需要通过 keytool 使用 Java 默认密钥库?因为我相信使用 openssl 生成的 wtkeystore.p12 文件也是一个密钥库,并且足够好用。
更新代码 1: 按照建议:
if (properties.useSSL()) {
HttpsConnectionContext https = useHttps(system);
ConnectHttp connect = ConnectHttp.toHostHttps(properties.urlSSL(), properties.portSSL())
.withCustomHttpsContext(https);
Http.get(system).bindAndHandle(appRoute().flow(system, materializer), connect, materializer);
log.info("Started on " + properties.urlSSL() + ":" + properties.portSSL());
}
并确保防火墙/网络对端口 443 开放,但 netstat 仍然显示状态为 'ESTABLISHED',我 telnet 到它,然后关闭此端口连接
【问题讨论】:
-
您需要使用
Connect.toHostHttps创建HTTPS服务器。