【问题标题】:How to get Remote server untrusted SSL certificate using Apache HTTP Client API如何使用 Apache HTTP Client API 获取远程服务器不受信任的 SSL 证书
【发布时间】:2018-02-04 03:34:46
【问题描述】:
我有一个远程服务器,它可能使用有效的 SSL 证书(使用自签名 SSL 证书)运行,也可能不运行。
我们正在连接远程服务器,如果远程服务器使用自签名 SSL 证书,连接可能会失败。因此,如果我们的 SSL 握手失败,我们希望能够下载/查看远程服务器证书。
如果我使用 Apache HTTP 客户端,那么我找不到可以让我查看远程服务器证书的方法(您可以使用 HttpsURLConnection 来完成,但我们试图避免使用它see this example)。
我还查看了 Spring RestTemplate,但它也没有提供任何选项 - 我在 Google 上搜索并没有找到 Spring 或 Apache HTTP 客户端的任何内容。
【问题讨论】:
标签:
ssl
https
apache-httpclient-4.x
apache-commons-httpclient
【解决方案1】:
这应该让您几乎可以完全控制信任验证过程。
SSLContext sslContext = SSLContextBuilder.create()
.loadTrustMaterial((chain, authType) -> {
for (X509Certificate cert: chain) {
System.out.println(cert.getSubjectDN());
}
// Let the standard trust managers decide
// whether or not the cert chain is trusted
return false;
})
.build();
CloseableHttpClient client = HttpClientBuilder.create()
.setSSLContext(sslContext)
.build();