[英]PKIX path building failed in Java application
在將我的應用程序從 Windows 2000 移動到 Windows 2008 R2 Server 之后,我一直在努力讓我的應用程序運行近一個星期。
程序,流程:
JAVA_HOME
為C:\\Progra~1\\Java\\jdk1.7.0_25\\
keytool
將證書導入 cacerts-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 的解決方案示例
確定根 CA 證書:
openssl s_client -showcerts -connect imap.gmail.com:993
在這種情況下,我們發現根 CA 是Equifax Secure Certificate Authority
為javax.net.ssl.trustStore
導入證書:
keytool -import -alias gmail_imap -file Equifax_Secure_Certificate_Authority.pem
您已將證書導入到 JDK 中提供的 JRE 的信任庫中,但您正在運行直接安裝的 JRE 的 java.exe。
編輯
為了清楚起見,並解決下面評論中的誤解,您需要將證書導入到您打算使用的 JRE的cacerts
文件中,並且很少會在 JDK 內部傳送該證書,因為客戶端通常不會有 JDK。 以下評論中任何暗示其他內容的內容都應該被忽略,因為這里沒有表達我的意圖。
更好的解決方案是創建您自己的信任庫,從cacerts
文件的副本開始,並通過系統屬性javax.net.ssl.trustStore.
專門告訴 Java 使用該信任庫javax.net.ssl.trustStore.
您應該將構建這部分作為構建過程的一部分,以便與 JDK 升級引起的cacerts
文件的更改保持cacerts
。
如果您使用的是 Eclipse,只需在 Eclipse Windows--> 首選項--> java ---> 安裝的 JRE 中交叉檢查當前 JRE 和您配置證書的 JRE。 如果沒有刪除 JRE 並在安裝證書的位置添加 jre
根據您的 pastebin,您需要將proxy.tkk.com
證書添加到信任庫。
在 Windows 上,您可以嘗試以下步驟:
/lib/security
找到一個文件 jssecacerts(您可以使用命令System.out.println(System.getProperty("java.home");
找到包含當前 JRE 的文件夾)。備份文件。在我的情況下,問題是通過安裝Oracle 的官方 JDK 10而不是使用我的 Ubuntu 附帶的默認 OpenJDK 解決的。 這是我遵循的指南: https : //www.linuxuprising.com/2018/04/install-oracle-java-10-in-ubuntu-or.html
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.