【问题记录】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