Registry<ConnectionSocketFactory> reg =RegistryBuilder<ConnectionSocketFactory>create().register("https", new
SSLConnectionSocketFactory(ctx)).register("http", new PlainConnectionSocketFactory())
.build();
PoolingHttpClientConnectionManager cm = newPoolingHttpClientConnectionManager(reg);
client = HttpClients.custom()
.setConnectionManager(cm)
.setDefaultRequestConfig(config)
.setSSLHostnameVerifier(new NoopHostnameVerifier())
.build();
我使用NoopHostnameVerifier因为我不想验证SSL,但是在我的ConnectionManager中存在必要的逻辑并且没有机会忘记ConnectionManager。
问题是如果我有ConnectionManager和SSLHostnameVerifier(NoopHostnameVerifier),我会收到以下错误:
javax.net.ssl.SSLPeerUnverifiedException:peer not authenticated
可能导致此问题的原因是什么?
答案 0 :(得分:1)
这对我有用。我找到了here。
PoolingHttpClientConnectionManager cm;
try {
SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustSelfSignedStrategy()).build();
clientBuilder.setSslcontext(sslContext);
SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(sslContext, new AllowAllHostnameVerifier());
Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
.register("http", PlainConnectionSocketFactory.getSocketFactory())
.register("https", sslSocketFactory)
.build();
cm = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
} catch (NoSuchAlgorithmException | KeyManagementException | KeyStoreException e) {
cm = new PoolingHttpClientConnectionManager();
}
答案 1 :(得分:0)
您需要使用NoopHostnameVerifier
所以将new NoopHostnameVerifier()
更改为NoopHostnameVerifier.INSTANCE