我正在尝试 接受所有证书,和/或接受 使用 Apache HTTPClient 4.5 版 的自签名证书( 此处 为教程链接)
我一直在通过关于 SO 的一堆帖子来解决这个问题。到目前为止,他们都没有工作。
我不断收到此错误: Error while trying to execute request. javax.net.ssl.SSLHandshakeException: Remote host closed connection during handshake
阿帕奇文档:
- Apache v4.5 教程
- SSL/TLS 定制
- Apache 有版本 3 的指南,但没有版本 4。
相关 StackOverflow 问题- 以下是我尝试过的解决方案的一些链接:
- 忽略 Apache HttpClient 4.3 中的 SSL 证书
- 如何忽略 Apache HttpClient 4.0 中的 SSL 证书错误
- 使用 Java 忽略 SSL 证书错误
- 使用Spring开发时需要信任所有证书
- 如何使用 Apache HttpClient 处理无效的 SSL 证书?
请注意,在所有这些示例中,我还传递了一个 cookie 存储和一个我之前定义的代理凭据提供程序。这些都在工作,我只是想添加 SSL 支持。
尝试#1
使用 --- 创建我自己的 ssl 上下文,并使用 TrustSelfSignedStrategy
SSLContextBuilder
信任所有自签名策略。
SSLContextBuilder sshbuilder = new SSLContextBuilder();
sshbuilder.loadTrustMaterial(null, new TrustSelfSignedStrategy());
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sshbuilder.build());
CloseableHttpClient httpclient = HttpClients.custom()
.setDefaultCredentialsProvider(credsProvider)
.setDefaultCookieStore(cookieStore)
.setSSLSocketFactory(sslsf)
.build();
结果:没有用。得到 Error while trying to execute request. javax.net.ssl.SSLHandshakeException: Remote host closed connection during handshake
尝试 #2
同上,但添加一个 PoolingHttpClientConnectionManager
SSLContextBuilder builder = new SSLContextBuilder();
builder.loadTrustMaterial(null, new TrustSelfSignedStrategy());
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(builder.build(),SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
.register("http", new PlainConnectionSocketFactory())
.register("https", sslsf)
.build();
PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(registry);
cm.setMaxTotal(2000);//max connection
CloseableHttpClient httpclient = HttpClients.custom()
.setDefaultCredentialsProvider(credsProvider)
.setDefaultCookieStore(cookieStore)
.setSSLSocketFactory(sslsf)
.setConnectionManager(cm)
.build();
结果:没有用。得到 Error while trying to execute request. javax.net.ssl.SSLHandshakeException: Remote host closed connection during handshake
尝试#3
只需覆盖 TrustStrategy
即可接受所有证书(不推荐)
SSLContextBuilder builder = new SSLContextBuilder();
builder.loadTrustMaterial(null, new TrustStrategy() {
@Override
public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
return true;
}
});
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(builder.build(),
SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
CloseableHttpClient httpclient = HttpClients.custom()
.setDefaultCredentialsProvider(credsProvider)
.setDefaultCookieStore(cookieStore)
.setSSLSocketFactory(sslsf)
.build();
结果:没有用。得到 Error while trying to execute request. javax.net.ssl.SSLHandshakeException: Remote host closed connection during handshake
尝试#4
我从 这个答案 中发现了一些有用的东西:
从 4.5 版开始,HttpClient 默认禁用 SSLv3 协议版本
这是他给出的解决方案:
SSLContext sslcontext = SSLContexts.createSystemDefault();
SSLConnectionSocketFactory sslConnectionSocketFactory = new SSLConnectionSocketFactory(
sslcontext, new String[] { "TLSv1", "SSLv3" }, null,
SSLConnectionSocketFactory.getDefaultHostnameVerifier());
Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
.register("http", PlainConnectionSocketFactory.INSTANCE)
.register("https", sslConnectionSocketFactory)
.build();
PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
httpclient = HttpClients.custom()
.setDefaultCredentialsProvider(credsProvider)
.setDefaultCookieStore(cookieStore)
.setSSLSocketFactory(sslConnectionSocketFactory)
.setConnectionManager(cm)
.build();
结果:没有用。得到 Error while trying to execute request. javax.net.ssl.SSLHandshakeException: Remote host closed connection during handshake
原文由 Katie 发布,翻译遵循 CC BY-SA 4.0 许可协议
上述方法之一应该适用于自签名证书,但奇怪的是您在所有方法中都遇到相同的异常。
我觉得在 SSL 会话建立期间或握手协议没有被客户端或服务器接受。
这里最好的解决方案是调试应用程序。
如果是 tomcat,请在 setenv.sh 或 setenv.bat 文件中添加 -Djavax.net.debug=all ,然后重新启动服务器。
或者您可以按照 本教程 进行操作。
OP 只需要在连接到 SSL 时更改端口: