Java API 连接失败:PKIX 路径
Posted
技术标签:
【中文标题】Java API 连接失败:PKIX 路径【英文标题】:Java API connectiong failed: PKIX path 【发布时间】:2022-01-23 12:38:17 【问题描述】:我一直在寻找一些关于 javax.net.ssl.SSLHandshakeException: PKIX 路径构建失败:sun.security.provider.certpath.SunCertPathBuilderException: 无法找到请求目标的有效认证路径的解决方案例外,但我阅读的帖子中提供的解决方案并没有解决问题,这就是我在这里问的原因。
基本上,在我的代码中,我使用 Unirest 进行连接,如下代码:
public void httpConnectionHourlyPred()
try
Map<String, String> asHeaders = new HashMap<String, String>();
asHeaders.put("cache-control", "no-cache");
asHeaders.put("api_key", getsKey());
HttpResponse response = Unirest.get(sURLHourlyPred).headers(asHeaders).asString();
System.out.println(response);
我将 AEMET 开放数据服务 (https://opendata.aemet.es/centrodedescargas/inicio) 连接到的 API,而 API 端点是 https://opendata.aemet.es/opendata/api/prediccion/especifica/municipio/horaria/01001。出于安全原因,我没有包含 API-KEY,但是,如果我在浏览器中尝试 API,我会成功,如下面的快照。
python 的相同脚本也可以工作,我得到了结果。但是,由于使用了只兼容 Java 的硬件,我不得不用 Java 编程。因此,当我运行脚本时,我得到了下面的异常。我正在使用 OpenJDK 虚拟机(特别是 openjdk-8u312-b07)。我该如何解决?
在此先感谢
com.mashape.unirest.http.exceptions.UnirestException: javax.net.ssl.SSLHandshakeException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
at com.mashape.unirest.http.HttpClientHelper.request(HttpClientHelper.java:143)
at com.mashape.unirest.request.BaseRequest.asString(BaseRequest.java:56)
at es.cartif.aemet.AEMETConnector.httpConnectionHourlyPred(AEMETConnector.java:55)
at es.cartif.main.AEMETMain.main(AEMETMain.java:10)
Caused by: javax.net.ssl.SSLHandshakeException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
at java.base/sun.security.ssl.Alert.createSSLException(Alert.java:131)
at java.base/sun.security.ssl.TransportContext.fatal(TransportContext.java:369)
at java.base/sun.security.ssl.TransportContext.fatal(TransportContext.java:312)
at java.base/sun.security.ssl.TransportContext.fatal(TransportContext.java:307)
at java.base/sun.security.ssl.CertificateMessage$T12CertificateConsumer.checkServerCerts(CertificateMessage.java:654)
at java.base/sun.security.ssl.CertificateMessage$T12CertificateConsumer.onCertificate(CertificateMessage.java:473)
at java.base/sun.security.ssl.CertificateMessage$T12CertificateConsumer.consume(CertificateMessage.java:369)
at java.base/sun.security.ssl.SSLHandshake.consume(SSLHandshake.java:396)
at java.base/sun.security.ssl.HandshakeContext.dispatch(HandshakeContext.java:480)
at java.base/sun.security.ssl.HandshakeContext.dispatch(HandshakeContext.java:458)
at java.base/sun.security.ssl.TransportContext.dispatch(TransportContext.java:199)
at java.base/sun.security.ssl.SSLTransport.decode(SSLTransport.java:171)
at java.base/sun.security.ssl.SSLSocketImpl.decode(SSLSocketImpl.java:1497)
at java.base/sun.security.ssl.SSLSocketImpl.readHandshakeRecord(SSLSocketImpl.java:1403)
at java.base/sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:441)
at java.base/sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:412)
at org.apache.http.conn.ssl.SSLConnectionSocketFactory.createLayeredSocket(SSLConnectionSocketFactory.java:290)
at org.apache.http.conn.ssl.SSLConnectionSocketFactory.connectSocket(SSLConnectionSocketFactory.java:259)
at org.apache.http.impl.conn.HttpClientConnectionOperator.connect(HttpClientConnectionOperator.java:125)
at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:319)
at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:363)
at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:219)
at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:195)
at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:86)
at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:108)
at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:106)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:57)
at com.mashape.unirest.http.HttpClientHelper.request(HttpClientHelper.java:138)
... 3 more
Caused by: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
at java.base/sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:439)
at java.base/sun.security.validator.PKIXValidator.engineValidate(PKIXValidator.java:306)
at java.base/sun.security.validator.Validator.validate(Validator.java:264)
at java.base/sun.security.ssl.X509TrustManagerImpl.checkTrusted(X509TrustManagerImpl.java:231)
at java.base/sun.security.ssl.X509TrustManagerImpl.checkServerTrusted(X509TrustManagerImpl.java:132)
at java.base/sun.security.ssl.CertificateMessage$T12CertificateConsumer.checkServerCerts(CertificateMessage.java:638)
... 28 more
Caused by: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
at java.base/sun.security.provider.certpath.SunCertPathBuilder.build(SunCertPathBuilder.java:141)
at java.base/sun.security.provider.certpath.SunCertPathBuilder.engineBuild(SunCertPathBuilder.java:126)
at java.base/java.security.cert.CertPathBuilder.build(CertPathBuilder.java:297)
at java.base/sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:434)
... 33 more
Error making the data request javax.net.ssl.SSLHandshakeException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
【问题讨论】:
顺便说一下,我尝试过***.com/questions/21076179/…或***.com/questions/18378869/…之类的解决方案,但没有成功 【参考方案1】:您似乎没有配置 Unirest 的 ssl 配置。特定的例外可能意味着服务器不受客户端信任。您可以做的是从https://opendata.aemet.es/ 提取证书,即:
我建议将证书放入 p12 或 jks 信任库文件并从中创建一个 sslcontext。
这是一个用于从信任库创建 sslcontext 的代码 sn-p:
InputStream trustStoreStream = ...; // your InputStream from the trust store file
KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
trustStore.load(trustStoreStream, "password".toCharArray());
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
trustManagerFactory.init(trustStore);
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, trustManagerFactory.getTrustManagers(), null);
并像下面的 sn-p 一样配置 Unirest 的主实例:
Unirest.primaryInstance()
.config()
.sslContext(sslContext)
【讨论】:
非常感谢!我注意到 Unirest 版本不是正确的版本。我试图将密钥库与证书一起使用,但使用 Kong Unirest 库,它正在工作。谢谢!!!以上是关于Java API 连接失败:PKIX 路径的主要内容,如果未能解决你的问题,请参考以下文章
sun.security.validator.ValidatorException 的 JDBC 异常:PKIX 路径构建失败
SSLHandshakeException:PKIX 路径构建失败
SAML Java Spring Boot - PKIX 路径构造因不受信任的凭证而失败
WEBLOGIC 上出现“PKIX 路径验证失败:java.security.cert.CertPathValidatorException:时间戳检查失败”错误