java - Java 应用程序中的 PKIX 路径构建失败

标签 java exception ssl certificate

在将我的应用程序从 Windows 2000 迁移到 Windows 2008 R2 Server 后,我花了将近一周的时间才让我的应用程序正常运行。

过程:

  1. 已安装 Java JDK 1.7.0_25
  2. 设置系统环境变量JAVA_HOMEC:\Progra~1\Java\jdk1.7.0_25\
  3. 使用 keytool 将证书导入到 cacerts 中
  4. 使用 -list 确保证书存在于 keytool 中。

我已尝试使用 InstallCert 重复第 3 步,以确保我没有搞砸任何事情。

以上方法都没有解决我的问题,所以我尝试通过编程方式来解决:

System.setProperty("javax.net.ssl.trustStore",
"C:/Progra~1/Java/jdk1.7.0_25/jre/lib/security/cacerts");
System.setProperty("javax.net.ssl.trustStorePassword", "changeit");

仍然没有任何运气。我被困住了,不太确定从这里往哪个方向走。

堆栈跟踪:

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
    at sun.security.ssl.Alerts.getSSLException(Alerts.java:192)
    at sun.security.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1886)
    at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:276)
    at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:270)
    at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1341)
    at sun.security.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:153)
    at sun.security.ssl.Handshaker.processLoop(Handshaker.java:868)
    at sun.security.ssl.Handshaker.process_record(Handshaker.java:804)
    at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1016)
    at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1312)
    at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1339)
    at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1323)
    at sun.net.www.protocol.https.HttpsClient.afterConnect(HttpsClient.java:515)
    at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:185)
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.connect(HttpsURLConnectionImpl.java:153)
    at util.SMS.send(SMS.java:93)
    at domain.ActivationSMSSenderMain.sendActivationMessagesToCustomers(ActivationSMSSenderMain.java:80)
    at domain.ActivationSMSSenderMain.<init>(ActivationSMSSenderMain.java:44)
    at domain.ActivationSMSSenderMain.main(ActivationSMSSenderMain.java:341)
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 sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:385)
    at sun.security.validator.PKIXValidator.engineValidate(PKIXValidator.java:292)
    at sun.security.validator.Validator.validate(Validator.java:260)
    at sun.security.ssl.X509TrustManagerImpl.validate(X509TrustManagerImpl.java:326)
    at sun.security.ssl.X509TrustManagerImpl.checkTrusted(X509TrustManagerImpl.java:231)
    at sun.security.ssl.X509TrustManagerImpl.checkServerTrusted(X509TrustManagerImpl.java:126)
    at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1323)
    ... 14 more
Caused by: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
    at sun.security.provider.certpath.SunCertPathBuilder.engineBuild(SunCertPathBuilder.java:196)
    at java.security.cert.CertPathBuilder.build(CertPathBuilder.java:268)
    at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:380)
    ... 20 more

更新:

两者都有 System.out.println(System.getProperty("javax.net.ssl.trustStore"));System.out.println(System.getProperty("javax.net.ssl.keyStore"));

返回 null

最佳答案

我遇到了类似的问题,其原因和解决方案都很简单:

主要原因:没有使用 keytool 导入正确的证书

注意:仅导入根 CA(或您自己的自签名)证书

注意:不要导入中间的非证书链根证书

imap.gmail.com 的解决方案示例

  1. 确定根 CA 证书:

    openssl s_client -showcerts -connect imap.gmail.com:993
    

    在这种情况下,我们发现根 CA 是 Equifax 安全证书颁发机构

  2. 下载root CA cert .
  3. 通过与信息 found here 比较,验证下载的证书是否具有正确的 SHA-1 和/或 MD5 指纹
  4. javax.net.ssl.trustStore 导入证书:

    keytool -import -alias gmail_imap -file Equifax_Secure_Certificate_Authority.pem
    
  5. 运行你的java代码

关于java - Java 应用程序中的 PKIX 路径构建失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56648204/

相关文章:

java - 将异常放在自己的子包中?

ssl - 在 Python3.6 中使用 http.client 返回 [SSL : UNKNOWN_PROTOCOL]

c# - 实现自定义异常树的最准确方法是什么?

ssl - 如何使用私钥生成SSL证书?

ssl - 安全页面上的非安全项目

java - 在java中检索具有Predicate返回类型的方法的值

java - 与多个不同版本的kafka集群通信

java - 如何获取两个日期之间的月份和天数

java - 没有找到适合 ProductoExtranjero 的构造函数

java - 执行捕获异常中的前面步骤