【问题标题】:SSLHandshakeException: PKIX path building failedSSLHandshakeException:PKIX 路径构建失败
【发布时间】:2020-06-01 15:41:12
【问题描述】:

我一直在尝试调试此错误,但没有成功。

基本上我正在通过我的应用程序联系外部应用程序。连接建立良好,应用程序通过回调 URL 联系我的应用程序,然后我需要向外部应用程序发送最终验证。 最后一步由于 SSLHandShajeException 而失败。

javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException:PKIX 路径构建失败: sun.security.provider.certpath.SunCertPathBuilderException:无法 找到请求目标的有效认证路径

我查看了可能的原因,并按照以下步骤创建了一个包含外部应用程序证书的信任库:

  • 从浏览器下载证书链
  • 使用
  • 创建信任库

keytool -import -v -trustcacerts -alias mycert -file x_my_cert_location_x -keystore 信任库

  • 在我的 setenv 文件中添加上面的 trustore
  • 重新启动我的 tomcat 实例。

这并没有解决问题。

我也在cacert文件中添加了cert,和server.xml文件中的connector一样如下:

<Connector port="${catalina.port.https}" server="Apache" protocol="HTTP/1.1"
           SSLEnabled="true" scheme="https" secure="true" sslProtocol="TLS"
           clientAuth="false"
           connectionTimeout="20000"
           compression="on"
           compressionMinSize="200"
           compressableMimeType="text/html,text/xml,text/plain,text/css,text/javascript,application/javascript,application/json,application/xml"
           enableLookups="false"
           URIEncoding="UTF-8"
           keystoreFile="my_store_location" keystorePass="myPwd"/>

在 SSL 中调试,我可以看到:

> %% Invalidated: [Session-15, TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256]
> http-nio-8080-exec-9, SEND TLSv1.2 ALERT: fatal, description =
> certificate_unknown http-nio-8080-exec-9, WRITE: TLSv1.2 Alert, length
> = 2 [Raw write]: length = 7 0000: 15 03 03 00 02 02 2E ....... http-nio-8080-exec-9, called closeSocket() http-nio-8080-exec-9,
> handling 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

这对我来说没有任何意义。我的应用程序在 java 1.8.242 上。请问这方面有什么建议吗?

【问题讨论】:

  • 您是否将证书添加到:./jdk1.8.0_24/jre/lib/security/cacerts
  • 配置Connector时需要定义truststore。您无法保证 Tomcat 使用读取系统属性的默认 SSLContext。
  • 我将证书添加到 cacerts,但没有任何改变。
  • 另外,我对连接器做了同样的事情。我会用这些信息更新问题
  • 我确定做了keytool之后,它已经安装成功了

标签: java ssl httpclient truststore


【解决方案1】:

该错误表明您缺少要访问的站点的证书。所以..

  • -Djavax.net.debug=ALL 启动你的java/tomcat。在日志中,您应该会看到类似这样的行(我去掉了时间戳等):

     trustStore is: {your-cacerts-file} trustStore
     type is: pkcs12
     trustStore provider is: the last modified time is: Tue Feb 02 18:29:33 GMT 2021 
     Reload the trust store
     Reload trust
     Reloaded {x} trust certs adding as trusted certificates
     ( {then you get a big dump of all {x} certs.}
    

    这样你就可以确定你的 cacerts 是否真的被阅读了。

  • 如果您的文件确实被 Tomcat 读取,则编写您自己的小型 Java 程序,该程序使用您的 cacerts 文件(使用 -Djavax.net.ssl.trustStore={path-to-my-file})和相同的调试参数上面用过。您应该会在您添加的证书中看到它。然后从那里调试,也许重新发布这个较小的案例。这不是全部的答案,但至少你会在一定程度上隔离问题。

【讨论】:

    猜你喜欢
    • 2019-08-08
    • 2020-11-25
    • 2014-09-05
    • 2019-05-03
    • 2016-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-24
    相关资源
    最近更新 更多