【问题标题】:Spring Boot mutual authentication failing in handshakeSpring Boot相互身份验证握手失败
【发布时间】:2018-05-16 11:42:43
【问题描述】:

我在SpringBoot 应用程序中嵌入了一个RestTemplate 客户端,如下所示,

@SpringBootApplication
public class SecureAppClientApplication {

    private static final Logger LOG = LoggerFactory.getLogger(SecureAppClientApplication.class);

    public static void main(String[] args) {
        SpringApplication.run(SecureAppClientApplication.class, args);
        LOG.debug("Testing connecting to secure site");
        HttpsURLConnection.setDefaultHostnameVerifier ((hostname, session) -> false);
        RestTemplate restTemp = new RestTemplate();
        String greetings = restTemp.getForObject("https://my.secure.service", String.class);
        LOG.debug("Received greetings from secured server ---> " + greetings );
    }
}

application.proerties 的内容类似于,

server.ssl.key-store=myKeyStore.jks
server.ssl.key-store-password=Store@123
server.ssl.key-alias=myClient
server.ssl.key-password=Key@1234
server.ssl.trust-store=cacerts
server.ssl.trust-store-password=changeit

还有其他属性,但与 HTTPS 无关(如 Logging、JPA 等)

现在,使用从浏览器 (Mozilla) 导出的主机证书,我将其安装在

  • myKeyStore.jks 出现在类路径中,
  • cacerts 的副本存在于类路径中,
  • cacerts 在 JDK 家,以及
  • cacertsJRE 家

我仍然收到 PKIX 验证错误,其根是

Caused by: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

不知道如何开始进行故障排除。难道是

  • 从浏览器中提取的证书,因为我之前使用由openssl 生成的 X509 证书进行了测试。
  • 服务器根路径;我正在尝试访问“https://host/service”,而证书仅显示“https://host”的信息
  • 使用keytool -importcert -file certificate.cer -keystore keystore.jks -alias my-alias导入

更新 这似乎是一个证书问题。事实证明,从浏览器获取证书并没有真正的帮助。我联系了我的服务器主机管理员,并获得了 3 个证书来安装。愚蠢的我依赖浏览器证书。我猜有类似certificate chaining的东西。

结论 确保在实际用例中获得适当的证书,因为世界并不依赖于浏览器上可用的简单证书,尤其是对于 Web 服务调用。

【问题讨论】:

  • 看来您需要建立信任,但您正在将站点的密钥添加到您的密钥库中,除非我对您的示例有误解。您是否尝试使用密钥工具将站点证书添加到您的信任库?
  • 我相信随 Java 安装提供的 cacerts 文件用作 trust-store。我已经提到了所有证书的安装位置。我还需要添加什么吗?

标签: java spring-boot ssl resttemplate


【解决方案1】:

对于这种情况,它获得了正确的证书,因为我对它们知之甚少。您可能需要多个证书。

【讨论】:

    猜你喜欢
    • 2012-03-02
    • 2015-08-15
    • 2021-06-24
    • 2014-04-14
    • 1970-01-01
    • 2019-07-21
    • 2022-08-16
    • 2019-01-06
    • 1970-01-01
    相关资源
    最近更新 更多