gpt4 book ai didi

java - 为什么我找不到 SSL 握手的信任库?

转载 作者:IT老高 更新时间:2023-10-28 13:53:17 25 4
gpt4 key购买 nike

我在客户端使用 Spring RESTTemplate 来调用 REST 端点。在这种情况下,客户端是 Spring 应用程序,而 Tomcat 是 servlet 容器。

我在连接 HTTPS 端点时遇到问题。我收到一个错误,表明它找不到信任库的有效路径。我在哪里可以指定这个?这是在容器级别还是应用程序配置(Spring)级别完成的?

堆栈跟踪:

org.springframework.web.client.ResourceAccessException: I/O error:
sun.security.validator.ValidatorException: PKIX path building failed:
sun.security.provider.certpath.SunCertPathBuilderException:
unable to find valid certification path to requested target;
nested exception is javax.net.ssl.SSLHandshakeException:
sun.security.validator.ValidatorException:
PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException:
unable to find valid certification path to requested target
org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:330)
org.springframework.web.client.RestTemplate.execute(RestTemplate.java:292)
org.springframework.web.client.RestTemplate.postForObject(RestTemplate.java:227)

最佳答案

更具体地说,调用这个方法就可以了,这样任何后续的 HttpClient 调用都不会关心 SSL 证书的有效性:

public static void trustSelfSignedSSL() {
try {
SSLContext ctx = SSLContext.getInstance("TLS");
X509TrustManager tm = new X509TrustManager() {

public void checkClientTrusted(X509Certificate[] xcs, String string) throws CertificateException {
}

public void checkServerTrusted(X509Certificate[] xcs, String string) throws CertificateException {
}

public X509Certificate[] getAcceptedIssuers() {
return null;
}
};
ctx.init(null, new TrustManager[]{tm}, null);
SSLContext.setDefault(ctx);
} catch (Exception ex) {
ex.printStackTrace();
}
}

关于java - 为什么我找不到 SSL 握手的信任库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1725863/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com