如何在新的 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/