[英]How to configure apache httpclient 4.5+ SSLContext to use mutual TLS authentication with a self signed certificate?
我正在嘗試使用相互 TLS 身份驗證從 httpclient 4.5 配置CloseableHttpClient
。 服務器證書是自簽名的。 這是我使用的代碼(受各種 StackOverflow 帖子的啟發):
KeyStore trustStore = KeyStore.getInstance("pkcs12");
try (InputStream fis = new FileInputStream(ssl_cert_file)) {
trustStore.load(fis, password.toCharArray());
}
SSLContext sslContext = SSLContexts.custom()
.loadKeyMaterial(trustStore, password.toCharArray(), (map, socket) -> "client")
.loadTrustMaterial(trustStore, new TrustSelfSignedStrategy())
.build();
SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(sslContext, new DefaultHostnameVerifier());
Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
.register("https", socketFactory).build();
connectionManager = new PoolingHttpClientConnectionManager(registry);
我有
sun.security.provider.certpath.SunCertPathBuilderException:
unable to find valid certification path to requested target
我認為我提供的 CA 證書似乎沒有被使用。 知道什么可能是錯的嗎?
CA 的證書、客戶端的證書和客戶端的私鑰都在一個 pkcs12 文件中
openssl pkcs12 -export -in client-cert.pem -inkey private/client-key.pem -certfile cacert.pem -name "client" -out client-cert.p12
我嘗試openssl s_client
來檢查證書返回預期的輸出(確實如此)。 我懷疑問題來自我的代碼而不是client-cert.p12
文件。
openssl s_client -connect host:port -tls1 -cert client-cert.pem -key private/client-key.pem -CAfile cacert.pem
正如@Gimby 所指出的,問題是我的 CA 證書在我的密鑰庫中未被識別為 TrustCertEntry。
我的解決方法是為 CA 的證書生成一個 jks 文件:
keytool -import -alias client -file cacert.pem -storetype JKS -keystore cacert.jks
從中構建一個 KeyStore 對象並將其用於SSLContext.loadTrustMaterial
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.