現在我正在尋找有關任務如何通過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