ssl - 如何在新的 Apache Http Client 4.3 中创建 SSL 套接字工厂?

标签 ssl apache-commons-httpclient

如何在新的 Apache Http Client 4.3 中创建 SSL 套接字工厂?

这是我在 4.3 之前创建它的方式

val ts = new TrustStrategy() {
  def isTrusted(chain: Array[X509Certificate], authType: String): Boolean = true
}

new SSLSocketFactory(ts, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER)

现在 SSLSocketFactory 标记为已弃用。定义自定义 TrustStrategy 的新方法是什么?我想不通。

最佳答案

嗯,我想通了。

像这样初始化你的ConnectionSocketFactory

val sslContext = SSLContexts.custom().loadTrustMaterial(null, new TrustSelfSignedStrategy).useTLS().build()
new SSLConnectionSocketFactory(sslContext, new AllowAllHostnameVerifier())

如果您查看 TrustSelfSignedStrategy 的来源,它们将自签名证书与真实证书区分开来的方式是检查链的长度。

public boolean isTrusted(
        final X509Certificate[] chain, final String authType) throws CertificateException {
    return chain.length == 1;
}

我不确定这是非常可靠的方法,但请记住它。也许值得检查 isTrusted 中给定的 X509Certificate

关于ssl - 如何在新的 Apache Http Client 4.3 中创建 SSL 套接字工厂?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19061931/

相关文章:

google-chrome - 当握手中没有发送 "Client Request"时,Chrome 如何知道提供客户端证书?

java - 如何让 Apache Http 客户端在超时后恢复等待响应?

Java - 在 Web 浏览器中显示 HttpResponse 的内容

java - 使用 BrowserLauncher 2 打开 URl 时,Acegi 抛出 AuthenticationCredentialsNotFoundException

java - Java 中使用 commons HttpClient 的curl 等效项

node.js - 使用 Node 的 HTTPS/TLS 与 OpenSSL s_client 时不同的证书链顺序

asp.net - SSL 不适用于 iPhone 上的 PassBook

通过代理使用 FTP 的 RCurl getURL

Azure 证书分配给虚拟机

java - HttpAsyncClient 4 如何工作?