现在我正在寻找有关任务如何通过HttpComponentsMessageSender(不相关)为客户端x509证书身份验证重写不推荐使用的解决方案的解决方案。Apache HttpClient 4.3和x509客户端证书进行身份验证
例如,过时的解决方案是:
SSLSocketFactory lSchemeSocketFactory = new SSLSocketFactory(this.keyStore, this.keyStorePassword);
Scheme sch = new Scheme("https", 443, lSchemeSocketFactory);
DefaultHttpClient httpClient = (DefaultHttpClient)getHttpClient();
httpClient.getConnectionManager().getSchemeRegistry().register(sch);
与CloseableHttpClient新的解决方案,我使用:
SSLContextBuilder sslContextBuilder = SSLContexts.custom()
// this key store must contain the key/cert of the client
.loadKeyMaterial(keyStore, keyStorePassword.toCharArray());
if (trustStore != null) {
// this key store must contain the certs needed and trusted to verify the servers cert
sslContextBuilder.loadTrustMaterial(trustStore);
}
SSLContext sslContext = sslContextBuilder.build();
LayeredConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext);
// Create a registry of custom connection socket factories for supported
// protocol schemes/https
Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
.register("https", sslsf)
.register("http", new PlainConnectionSocketFactory())
.build();
PoolingHttpClientConnectionManager connPoolControl =
new PoolingHttpClientConnectionManager(socketFactoryRegistry);
setConnPoolControl(connPoolControl);
getClientBuilder().setSSLSocketFactory(sslsf);
我还是从服务器获取禁止403。但是,当我使用解决方案的“已弃用”版本时,它效果很好。 SSL证书已签署Thawte。
有什么想法? 感谢
'不推荐使用SSLContexts类型'和'构造函数SSLConnectionSocketFactory(SSLContext,X509HostnameVerifier)'弃用' – KCD
'useTLS()'是无用的,因为TLS已经是默认协议。 – herau
@KCD谢谢你,我根据你的意见做了修改。 – Daniyar