无法使用 Spring Rest 模板访问 HTTPS 服务 - TrustStore 问题?

Unable to access HTTPS service using Spring Rest Template - TrustStore issue?

访问 HTTPS 服务时遇到以下问题:

错误: ..Certification authentication failed</TITLE> ... An attempt to authenticate with a client certificate failed. A valid client certificate is required to make this connection.

我正在使用 Spring RestTemplate excahnge API :

restTemplate.exchange(reqInfo.getUrl(),HttpMethod.GET,requestEntity,String.class);

我尝试了 2 种方法来提供 trustStore,但错误仍然存​​在:

1.) 作为参数传递: java -cp -Djavax.net.ssl.trustStore="trustStore.jks" -Djavax.net.ssl.trustStorePassword="pwd" Test

2.) 设置 属性

System.setProperty("javax.net.ssl.trustStore","path to truststore"); System.setProperty("javax.net.ssl.trustStorePassword","pwd");

我也尝试使用简单的 Java 代码使用 HTTPclient 然后它工作正常但是 SPring RestTemplate none 选项正在工作,我在这里遗漏了什么吗?

注意:如果我对 URL 进行卷曲,我会得到与未提供信任库相同的错误。因此我假设这个问题是由于 TrustStore。

终于解决了上面的问题

在构建 SSL 上下文时,我没有加载密钥库(尽管我通过参数传递它),因此我得到 认证身份验证失败,因为密钥库是不可用。

下面的代码解决了这个问题:(添加了 loadKeyMaterial

sslcontext = SSLContexts.custom().loadTrustMaterial(trustStore, new TrustSelfSignedStrategy())
                            .loadKeyMaterial(keyStore, keyStorePwd).build();