java中的Http调用未发送客户端证书
我正在使用java中的Http调用未发送客户端证书,java,java-8,apache-httpcomponents,Java,Java 8,Apache Httpcomponents,我正在使用Java8中的ApacheHTTP客户端(版本4.5.13)执行POST调用,该调用要求客户端使用存储在.PFX文件中的证书进行身份验证 这是我正在使用的代码: public static void performClientRequest() throws Exception { //Trust Strategy to accept any server certificate TrustStrategy trustStrategy = new TrustStrate
Java8
中的ApacheHTTP客户端
(版本4.5.13
)执行POST调用,该调用要求客户端使用存储在.PFX文件中的证书进行身份验证
这是我正在使用的代码:
public static void performClientRequest() throws Exception {
//Trust Strategy to accept any server certificate
TrustStrategy trustStrategy = new TrustStrategy() {
public boolean isTrusted(X509Certificate[] chain, String authType)
throws CertificateException {
return true;
}
};
//Load PFX client certificate
KeyStore clientStore = KeyStore.getInstance("PKCS12");
InputStream instream = new FileInputStream("C:\\client.pfx");
try {
clientStore.load(instream, null);
} finally {
instream.close();
}
//Create ssl context with key store and trust strategy
SSLContext sslContext = SSLContexts.custom()
.loadKeyMaterial(clientStore, null)
.loadTrustMaterial(trustStrategy)
.build();
//Create ssl socket factory from context
SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE);
//Create HTTP client
HttpClient httpClient = HttpClients.custom()
.setSSLSocketFactory(sslSocketFactory)
.build();
//Perform call
URI url = new URI("https://mysite.foo");
HttpPost request = new HttpPost(url);
request.setHeader("Content-Type","application/json");
request.setHeader("Accept", "application/json");
String body = "...";
StringEntity bodyEntity = new StringEntity(body);
request.setEntity(bodyEntity);
HttpResponse response = httpClient.execute(request);
HttpEntity entity = response.getEntity();
System.out.println("----------------------------------------");
System.out.println(response.getStatusLine());
EntityUtils.consume(entity);
}
我在过去使用过这段代码,当时它工作得很好,但现在我尝试重新使用它,它只是不发送证书,服务器回复为:
HTTP/1.1 403 No client certificate supplied
我如何调试它并发现证书未被发送的原因
注意:我在
C#
和使用Postman
两种情况下都实现了类似的调用,在这两种情况下都工作得很好,因此服务器的客户端证书身份验证工作正常,只是在我的Java实现中不工作。因此,我不知道这是一个bug还是预期行为(如果是,为什么?),但显然,PFX文件必须有密码保护,然后才能正确发送。我无法使用不受保护的PFX文件,并像我在问题中发布的代码中所做的那样传递null
所以问题解决了,但我想知道是否有人可以评论为什么会发生这种情况