【问题标题】:SSL-Config or SSLContext to be used ? in Akka Http SSL Java要使用 SSL-Config 或 SSLContext 吗?在 Akka Http SSL Java 中
【发布时间】: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@ 工作正常。

问题:

  1. 我错过了上面的任何步骤吗?

  2. 我只使用 SSLContext - 可以吗,或者我也应该使用 SSLConfig 吗?如果是 - 那么我应该如何使用 SSLConfig,因为似乎没有提供适当的文档

  3. 是否需要通过 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 到它,然后关闭此端口连接

当我调试时,我得到 SSLConfig 和其他对象为无,除了 SSLContext 对象。这是正常的吗??

【问题讨论】:

  • 您需要使用Connect.toHostHttps创建HTTPS服务器。

标签: java ssl akka-http


【解决方案1】:

试试这样的:

if (properties.useSSL()) {
  ConnectHttp connect =
    ConnectHttp.toHostHttps(properties.urlSSL(), properties.portSSL())
      .withCustomHttpsContext(useHttps(system));

  Http.get(system).bindAndHandle(appRoute().flow(system, materializer),
      connect, materializer);
  log.info("Started on " + properties.urlSSL() + ":" + properties.portSSL());
}

【讨论】:

  • Thanx jrudolph ...现在,当我调试它时,它会显示 ConnectHttp 中用于 https 的端口和主机名 - 但 SSL 在我的终端仍然无法工作...
【解决方案2】:

终于!解决了……

我正在制作 2 个新的 Http.get(system) 对象而不是单个对象 所以我更新的代码如下

final Http http = Http.get(system); // Created Once Only

log.info("Starting on " + properties.url() + ":" + properties.port());
final ConnectHttp host = ConnectHttp.toHost(properties.url(), properties.port());

http.bindAndHandle(appRoute().flow(system, materializer), host, materializer);
log.info("Started on " + properties.url() + ":" + properties.port());

if (properties.useSSL()) {

  HttpsConnectionContext https = useHttps(system);
  ConnectHttp connect = ConnectHttp.toHostHttps(properties.urlSSL(), properties.portSSL())
      .withCustomHttpsContext(https);

  http.bindAndHandle(appRoute().flow(system, materializer), connect, materializer);
  log.info("Started on " + properties.urlSSL() + ":" + properties.portSSL());
}

也感谢 jrudolph 帮助代码...我还必须打开防火墙端口 443 并使域指向服务器的 IP 地址

按照 Akka Http Docs 使用 SSLContext 是可以的……如果您按照文档所示使用 SSLContext,则无需使用 SSLConfig - 我目前使用的是 Akka v2.4.7。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-18
    • 2023-03-14
    • 2017-01-10
    • 1970-01-01
    • 2017-08-12
    • 1970-01-01
    • 2010-12-22
    • 1970-01-01
    相关资源
    最近更新 更多