我有一个用于测试 api 的 java cucumber 测试自动化框架。
以前我使用的是一个名为 Karate 的工具,它有一个简单的标志 ( karate.configure('ssl', { trustAll: true });),它允许您信任所有证书。
我希望有一个类似的标志可用于 Apache HTTP 客户端......但我所有的谷歌搜索都会导致冗长而复杂的代码。
这是我到目前为止编写的将 .pfx 文件发送到 api 的代码
String keyPassphrase = "";
KeyStore keyStore = KeyStore.getInstance("PKCS12");
keyStore.load(new FileInputStream("src/main/resources/sslCertificates/certificate.pfx"), keyPassphrase.toCharArray());
SSLContext sslContext = SSLContexts.custom()
.loadKeyMaterial(keyStore, null)
.build();
//This is the httpClient that you will use to send your http request
CloseableHttpClient httpClient = HttpClients.custom().setSSLContext(sslContext).build();
//Send the request
CloseableHttpResponse response = httpClient.execute(request);
但它在发送之前被拒绝说
"javax.net.ssl.SSLHandshakeException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target"
如何轻松接受所有证书以解决此问题?如前所述,我只是在测试,所以这样做没有问题。
但是,我确实有 .pfx 和 .crt 格式的证书文件以及可能使用的 client.key 文件 - 但我不知道如何使用。