gpt4 book ai didi

java - SSLHandshakeException : ValidatorException: PKIX path building failed: sun. security.provider.certpath.SunCertPathBuilderException:

转载 作者:太空宇宙 更新时间:2023-11-03 13:52:37 25 4
gpt4 key购买 nike

我尝试在没有证书检查的情况下使用 https 请求本地服务。但是我得到了这个异常(exception)。

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

部分代码:

try {
RestTemplate restTemplate = new RestTemplate();
HttpsURLConnection.setDefaultHostnameVerifier(
(hostname, session) -> hostname.equals("IPADDRESS"));
responseEntity = restTemplate.exchange(url, HttpMethod.POST, httpEntity, String.class);
} catch (HttpClientErrorException e) {
LOGGER.error(e.toString());
}

这里有什么问题吗?

最佳答案

此问题是由于服务器证书的信任路径不完整造成的:客户端可能不信任服务器证书。

通常的解决方法是将服务器证书导入客户端信任库。默认的 trustStore 位于 jre/lib/security/cacerts 中,但使用您自己的 keystore 是更好的做法

您可以创建一个 SSLSocketFactory 并在连接之前添加到您的连接或使用静态方法应用于所有连接

 HttpsURLConnection.setDefaultSSLSocketFactory(sslFactory);

这是一个创建套接字工厂的例子

/* Load the keyStore that includes the server cert as a "trusted" entry. */
KeyStore keyStore = ...
TrustManagerFactory tmf =
TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
tmf.init(keyStore);
SSLContext ctx = SSLContext.getInstance("TLS");
ctx.init(null, tmf.getTrustManagers(), null);
sslFactory = ctx.getSocketFactory();

加载keyStore的例子

KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
keyStore.load(trustStore, trustStorePassword);
trustStore.close();

也可以使用系统属性配置信任库

 System.setProperty("javax.net.ssl.trustStore", "pathtoyourjavakeystorefile");
System.setProperty("javax.net.ssl.trustStorePassword", "password");

创建 key 存储文件的最简单方法是使用 GUI 工具 Portecle。 新 keystore > 导入可信证书

如果你想“信任”来自根的所有证书,你可以导入链的根证书,或者只导入服务器证书。对于自签名证书,直接导入

关于java - SSLHandshakeException : ValidatorException: PKIX path building failed: sun. security.provider.certpath.SunCertPathBuilderException:,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37906420/

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