在 akka http 客户端中禁用 SSL 安全性

Disable SSL security in akka http client

我必须对似乎没有经过验证的 SSL 证书的 api 进行 https 调用。我仍然想使用 akka-http 的 Http().singleRequest 方法调用 api。

当我打电话时,我收到以下错误:

javax.net.ssl.SSLHandshakeException: General SSLEngine problem

当我用 curl 打电话时,我得到

curl failed to verify the legitimacy of the server and therefore could not
establish a secure connection to it.

然而,如果我有标志 --insecure.

,则使用 curl 的调用会起作用

在 akka-http 中,我尝试了以下操作:

val badSslConfig = AkkaSSLConfig().mapSettings(
      s => s.withLoose(
        s.loose
          .withAcceptAnyCertificate(true)
          .withAllowLegacyHelloMessages(Some(true))
          .withAllowUnsafeRenegotiation(Some(true))
          .withAllowWeakCiphers(true)
          .withAllowWeakProtocols(true)
          .withDisableHostnameVerification(true)
          .withDisableSNI(true)
      )
    )
    val badCtx = Http().createClientHttpsContext(badSslConfig)


    Http()
      .singleRequest(
        HttpRequest(
          HttpMethods.POST,
          uri = Uri("https://..."),
          protocol = HttpProtocols.`HTTP/1.1`
        ),
        connectionContext = badCtx
      )

但我仍然得到同样的错误。

我应该怎么做才能解决这个问题?

PS:我明白(考虑到 akka-http 文档中的许多警告)这是我不应该在生产中做的事情,但我希望这个解决方法暂时有效......

我前段时间遇到过类似的问题,据我所知它与 this issue 有关。该问题的解决方法是拥有自己的 SSLContext 实现,它将接受任何东西。实现非常简单,示例可以在上面链接的问题的最后评论中找到。