【問題記錄】Java服務發起HTTPS請求報錯:PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException


問題報錯

今天上線了我開發的一個OAuth2單點登錄客戶端的實現,在測試系統驗證沒問題,到生產環境由於單點登錄服務端HTTPS協議,報錯如下:

I/O error on POST request for "xxx.xxx.xxx.xxx:8080": sun.security.validator.ValidatorException: 
PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target; nested exception is 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

導致報錯的原因是 HTTPS 域名的公鑰證書不在 JDK/JRE 的證書庫中,被Java認為是不可信的。

解決方案

1、導出證書

首先需要獲取 HTTPS 域名的公鑰證書,可以通過瀏覽器導出,如下是 Chrome 瀏覽器導出百度的公鑰證書。

2、上傳證書到服務器

如果是本地 JDK 則不需要此步驟。我把CA.cer上傳到了 Linux 服務器的 /root

3、為 keytool 授予執行權限

防止 keytool 沒有操作權限。

sudo chmod +x ${JDK目錄}/bin/keytool

4、導入證書

Java 證書庫文件的路徑為 ${JDK目錄}/jre/lib/security/cacerts,以下命令公鑰證書放在 /root/CA.cer

${JDK目錄}/bin/keytool -import \ 
-alias cacerts -keystore ${JDK目錄}/jre/lib/security/cacerts \ 
-file /root/CA.cer

順序輸入 yes + 回車 與 changeit +回車,完成證書導入。

yes 表示允許信任此證書,changeit 是 Java 證書庫默認的密碼。

5、重啟 Java 服務

這我就不多說了,懂得都懂 :happy:

最后

希望對您能有所啟發,如果您有更優雅的實現方式 或者 文中有錯誤,希望您能不吝賜教評論指出,感謝。

本文同步發布於博客園(東北小狐狸 https://www.cnblogs.com/hellxz/)與CSDN(東北小狐狸-Hellxz https://blog.csdn.net/u012586326)禁止轉載。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



猜您在找 報錯PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target" maven PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path mvn 編譯報錯mavn sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested targ PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target 解決 java 使用ssl過程中出現"PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target" nginx中配置wss的websocket報錯:PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target Maven:sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target 大坑!maven的web項目初始化錯誤:sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification ... 異常解決:sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM