【问题标题】:Unable to access HTTPS service using Spring Rest Template - TrustStore issue?无法使用 Spring Rest 模板访问 HTTPS 服务 - TrustStore 问题?
【发布时间】:2018-02-01 11:43:32
【问题描述】:

在访问 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");

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

注意:如果我对该 URL 进行卷曲,则会收到与未提供 truststore 相同的错误。因此,我假设这个问题是由于 TrustStore 造成的。

【问题讨论】:

    标签: spring ssl https resttemplate truststore


    【解决方案1】:

    我终于能够解决上述问题了。

    在构建 SSL 上下文时,我没有加载密钥库(尽管我通过参数传递它),因此我得到 Certification authentication failed 因为密钥库不可用。 p>

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

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

    【讨论】:

      猜你喜欢
      • 2013-07-11
      • 2019-01-26
      • 1970-01-01
      • 2017-08-11
      • 2014-07-15
      • 2018-05-12
      • 2021-07-03
      • 2017-12-01
      • 1970-01-01
      相关资源
      最近更新 更多