问题描述

最近开发项目中在springboot接口中调用第三方https接口,后台日志报错:
sun.sec urity.validator.ValidatorException: PKIX path building failed
意思就是非安全的调用,java不认识这个接口证书。

报错原因

java security仓库中没有这个第三方站点的SSL证书,调用失败。

解决方案

方案一: 手动导入网站证书到java security库

弊端:换了一个SSL证书有需要导入一次,很麻烦。不推荐。
可参考我另一篇文章:
https://blog.csdn.net/IndexMan/article/details/128658805

方案二:彻底解决,一劳永逸

新增工具类

import javax.net.ssl.*;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
/**
* java security信任SSL证书工具类
*/
public class SslUtils {
private static void trustAllHttpsCertificates() throws Exception {
TrustManager[] trustAllCerts = new TrustManager[1];
TrustManager tm = new miTM();
trustAllCerts[0] = tm;
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, null);
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
} static class miTM implements TrustManager,X509TrustManager {
public X509Certificate[] getAcceptedIssuers() {
return null;
} public boolean isServerTrusted(X509Certificate[] certs) {
return true;
} public boolean isClientTrusted(X509Certificate[] certs) {
return true;
} public void checkServerTrusted(X509Certificate[] certs, String authType)
throws CertificateException {
return;
} public void checkClientTrusted(X509Certificate[] certs, String authType)
throws CertificateException {
return;
}
} /**
* 忽略HTTPS请求的SSL证书
*/
public static void ignoreSsl() throws Exception{
HostnameVerifier hv = new HostnameVerifier() {
public boolean verify(String urlHostName, SSLSession session) {
return true;
}
};
System.out.println("已忽略HTTPS请求的SSL证书!");
trustAllHttpsCertificates();
HttpsURLConnection.setDefaultHostnameVerifier(hv);
}
}

在启动类中调用工具类

@SpringBootApplication
public class WebApplication { public static void main(String[] args) {
SpringApplication.run(WebApplication .class, args);
}
// 调用工具类中的方法
@PostConstruct
public void run() throws Exception{
SslUtils.ignoreSsl();
}
}

解决Springboot发起https请求报错:sun.sec urity.validator.ValidatorException: PKIX path building failed的更多相关文章

  1. 抓取https网页时,报错sun.security.validator.ValidatorException: PKIX path building failed 解决办法

    抓取https网页时,报错sun.security.validator.ValidatorException: PKIX path building failed 解决办法 原因是https证书问题, ...

  2. 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 ...

  3. 解决 sun.security.validator.ValidatorException: PKIX path building failed

    今天用java HttpClients写爬虫在访问某Https站点报如下错误: sun.security.validator.ValidatorException: PKIX path buildin ...

  4. 异常信息:javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed

    上周五遇到一个问题,工程本地编译运行正常,打包本地tomcat运行也正常.部署到测试环境报错: 2017-05-05 09:38:11.645 ERROR [HttpPoolClientsUtil.j ...

  5. Maven:sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

    还是记录使用 maven 时遇到的问题. 一.maven报错 maven package 进行打包时出现了以下报错: Non-resolvable parent POM for com.wpbxin: ...

  6. javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed

    1.使用HttpClient4.3 调用https出现如下错误: javax.net.ssl.SSLHandshakeException: sun.security.validator.Validat ...

  7. javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certificatio

    场景:Java调用PHP接口,代码部署在服务器上后,调用报错,显示PHP服务器那边证书我这边服务器不信任(我猜的). 异常信息: 2019-08-06 14:00:09,102 [http-nio-4 ...

  8. sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

    httpclient-4.5.jar 定时发送http包,忽然有一天报错,http证书变更引起的. 之前的代码 try { CloseableHttpClient httpClient = build ...

  9. ES访问遇到sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

    cmd命令cd到jre/bin目录下 输入命令keytool -import -alias 别名 -keystore cacerts -file ‪C://certs//elasticsearch// ...

  10. 【问题记录】Java服务发起HTTPS请求报错:PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException

    问题报错 今天上线了我开发的一个OAuth2单点登录客户端的实现,在测试系统验证没问题,到生产环境由于单点登录服务端HTTPS协议,报错如下: I/O error on POST request fo ...

随机推荐

  1. Laravel - Eloquent 更新数据

    public function ormUpdate()     {         # 1.通过模型更新数据        /*  $student = Student::find(14);      ...

  2. [转帖]linux系统主机双网卡实现路由转发问题与解决

    1. 环境 拓扑: 网卡配置: host1: 192.168.1.1/24 host2: 左eth0: 192.168.1.2/24 右eth1: 192.168.2.2/24 host3: 192. ...

  3. [转帖]如何用python连接Linux服务器

    1.安装paramiko库 pip install paramiko 2.使用paramiko库连接linux #导入库 import paramiko 创建一个sshclient对象 ssh = p ...

  4. [转帖]SPEC-cpu2006的详细使用一键安装、手动安装。

    一.SPEC-cpu2006简介 SPEC CPU 2006 benchmark是SPEC新一代的行业标准化的CPU测试基准套件.重点测试系统的处理器,内存子系统和编译器. 说明:由于spec2006 ...

  5. [转帖]一口气看完45个寄存器,CPU核心技术大揭秘

    https://www.cnblogs.com/xuanyuan/p/13850548.html 序言 前段时间,我连续写了十来篇CPU底层系列技术故事文章,有不少读者私信我让我写一下CPU的寄存器. ...

  6. [转帖]07-rsync企业真实项目备份案例实战(需求收集--服务器配置---客户端配置---报警机制---数据校验---邮件告警)

    https://developer.aliyun.com/article/885820?spm=a2c6h.24874632.expert-profile.279.7c46cfe9h5DxWK 简介: ...

  7. [转帖]Shell脚本中利用expect实现非交互式

    https://developer.aliyun.com/article/885723?spm=a2c6h.24874632.expert-profile.295.7c46cfe9h5DxWK 简介: ...

  8. CentOS7 上面升级git 2.24的方法

    本来想使用tar包进行安装 但是发现tar包安装时总是报错如下: [root@centos76 git-2.25.0]# make LINK git-imap-send imap-send.o: In ...

  9. dmidecode 查看内存以及硬件信息

    安装工具dmidecode 使用 1.查看内存槽及内存条 $ sudo dmidecode -t memory 2.查看内存的插槽数,已经使用多少插槽.每条内存多大 $ sudo dmidecode  ...

  10. Redis-dump Docker搭建的快速指南

    背景 最近学习redis想能够将dump文件进行导入处理. 看到比较好的办法都是使用ruby ,但是公司的网络太感人了. 想着比较简单的办法是通过docker方式来搭建. 这里简单记录一下搭建过程. ...