【问题记录】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)禁止转载。
【问题记录】Java服务发起HTTPS请求报错:PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException的更多相关文章
- Flutter配置环境报错“PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target”
背景:最近看了很多Flutter漂亮的项目,想要尝试一下.所有环境都搭建好之后,按照文档一步一步配置(抄袭),但始终报如下图错误. PKIX path building failed: sun.sec ...
- 报错PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target"
今天在调用第三方HTTPS接口的时候,一直显示这个报错,然后百度很久,有2种解决方法,一个是说自己手动去导入,第二种用代码忽略证书验证.我用二种方式, 复制即用, public void test2( ...
- java程序中访问https时,报 PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
在java中使用https访问数据时报异常: Caused by: sun.security.validator.ValidatorException: PKIX path building fail ...
- 解决 java 使用ssl过程中出现"PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target"
今天,封装HttpClient使用ssl时报一下错误: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorExc ...
- mvn 编译报错mavn sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested targ
mavn 编译报错: mavn sun.security.validator.ValidatorException: PKIX path building failed: sun.security.p ...
- PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilder,阿里MAVEN仓库地址更新为了https问题
http://maven.aliyun.com/nexus/content/groups/public/,仓库地址更新为了https,所以下载时需要ssl认证,我们可以忽略ssl检查导致的问题,我们可 ...
- http 使用curl发起https请求报错的解决办法
使用curl发起https请求的时候报错:“SSL certificate problem, verify that the CA cert is OK. Details: error:1409008 ...
- java 使用https协议,cas认证PKIX path building failed错误解决方法
如果遇到的是 上图的异常,请继续往下看. linux 下 添加 证书 (1) 获取网站安全证书 xx.cer ( 详情见随笔 获取网站安全证书 ) (2) 将上面导出的证书导入java中的cacert ...
- 解决java使用https协议请求出现证书不信任问题(PKIX path building failed)
解决https请求时出现pkix path building fail错误 方法 将submail.cer 安全证书导入到java中的cacerts证书库 (sumail是我从https://api. ...
随机推荐
- .Net Core WebApi(三)--使用 IdentityServer4 4.1.1 踩坑记录
目的:创建IdentityServer 并通过PostMan验证获取token 第一次配置如下 public class Config { public static IEnumerable<A ...
- Ajax 与 Struts 1
Xml配置 <action path="/product/product/validateCurrencyDecimalSupport" type="com.neu ...
- BufferedReader 和BufferedWriter
BufferedWriter: private void test(String content,String destPath) throws IOException { BufferedReade ...
- 快速排序(C++)
快速排序 快速排序是面试中经常问到的排序算法 基本思想:通过一趟排序将待排序记录分割成独立的两部分,其中一部分记录的关键字均比另一部分记录的关键字小, 则可分别对这两部分记录继续进行排序,以达到整个序 ...
- Git使用教程六
冲突的产生与解决 案例:模拟产生冲突. ①同事在下班之后修改了线上仓库的代码 注意:此时我本地仓库的内容与线上不一致的. 2.第二天上班的时候,我没有做git pull操作,而是直接修改了本地的对应文 ...
- OAuth2-简介
1. 简介 OAuth(开放授权)是一个开放标准,允许用户让第三方应用访问该用户在某一网站上存储的私密的资源(如照片,视频,联系人列表),而无需将用户名和密码提供给第三方应用.因此OAUTH是安全的. ...
- 聚类算法与K-means实现
聚类算法与K-means实现 一.聚类算法的数学描述: 区别于监督学习的算法(回归,分类,预测等),无监督学习就是指训练样本的 label 未知,只能通过对无标记的训练样本的学习来揭示数据的内在规律和 ...
- shp的基本操作
本节将介绍如何利用python完成对shp的基本操作 1.读取shp四至 import shapefile sf = shapefile.Reader(r"E:\shp\1.shp" ...
- Typora画各类流程图、甘特图、饼图等详细文档
Draw Diagrams With Markdown August 15, 2016 by typora.io Typora supports some Markdown extensions fo ...
- Openswan支持的算法及参数信息:
数据封装加密算法: algorithm ESP encrypt: id=2, name=ESP_DES, ivlen=8, keysizemin=64, keysizemax=64 algorithm ...