首页 > 解决方案 > 使用没有密码的全局密钥库 - java HttpClient

问题描述

尝试使用 HttpClient 的证书进行 https 调用,将 pem 证书导入密钥库,并将根 CA 证书导入信任库。

但我能让它工作的唯一方法是使用 openSSL 和代码实现创建 p12:

private CloseableHttpClient getHttpClient() throws Exception{

           KeyStore clientStore = KeyStore.getInstance("PKCS12");
           clientStore.load(new FileInputStream("C:\\Users\\myUSer\\client.p12"), "passValue".toCharArray());
           SSLContext sslContext = SSLContexts.custom()
                   .loadKeyMaterial(clientStore, certificateProperties.getCertPassword().toCharArray())
                   .loadTrustMaterial(null, (x509CertChain, authType) -> true)
                   .build();                      

 

           HttpClientBuilder httpBuilder = HttpClients.custom()
                   .setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE)
                   .setSSLContext(sslContext);          
 

           RequestConfig requestConfig = RequestConfig.custom()
                   .setRedirectsEnabled(false)
                   .build();
 
           return httpBuilder.setDefaultRequestConfig(requestConfig).build();
       }

想要摆脱证书的路径并只传递和使用全局密钥库,但对我来说不清楚如何在没有传递的情况下使用全局密钥库并告诉 httpClient 使用来自密钥库的所需证书。

检查时存在密钥库文件:

System.getProperty("javax.net.ssl.trustStore")

标签: javaspringsslhttpclientkeystore

解决方案


推荐阅读