忽略Apache HttpClient 4.3中的SSL证书
如何忽略Apache HttpClient 4.3的SSL证书(全部信任)?
我在SO上找到的所有答案都对待以前的版本,并且API更改了。
编辑:
仅用于测试目的。孩子们,不要在家(或生产中)尝试
回答:
以下代码可用于信任自签名证书。创建客户端时,必须使用TrustSelfSignedStrategy:
SSLContextBuilder builder = new SSLContextBuilder();builder.loadTrustMaterial(null, new TrustSelfSignedStrategy());
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
builder.build());
CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(
sslsf).build();
HttpGet httpGet = new HttpGet("https://some-server");
CloseableHttpResponse response = httpclient.execute(httpGet);
try {
System.out.println(response.getStatusLine());
HttpEntity entity = response.getEntity();
EntityUtils.consume(entity);
} finally {
response.close();
}
我没有SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER
故意包括:重点是允许使用自签名证书进行测试,因此您不必从证书颁发机构获取适当的证书。您可以轻松创建具有正确主机名的自签名证书,而不要添加SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER
标志。
以上是 忽略Apache HttpClient 4.3中的SSL证书 的全部内容, 来源链接: utcz.com/qa/410789.html