解决Springboot发起https请求报错:sun.sec urity.validator.ValidatorException: PKIX path building failed
问题描述
最近开发项目中在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的更多相关文章
- 抓取https网页时,报错sun.security.validator.ValidatorException: PKIX path building failed 解决办法
抓取https网页时,报错sun.security.validator.ValidatorException: PKIX path building failed 解决办法 原因是https证书问题, ...
- 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 ...
- 解决 sun.security.validator.ValidatorException: PKIX path building failed
今天用java HttpClients写爬虫在访问某Https站点报如下错误: sun.security.validator.ValidatorException: PKIX path buildin ...
- 异常信息:javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed
上周五遇到一个问题,工程本地编译运行正常,打包本地tomcat运行也正常.部署到测试环境报错: 2017-05-05 09:38:11.645 ERROR [HttpPoolClientsUtil.j ...
- 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: ...
- javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed
1.使用HttpClient4.3 调用https出现如下错误: javax.net.ssl.SSLHandshakeException: sun.security.validator.Validat ...
- 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 ...
- 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 ...
- 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// ...
- 【问题记录】Java服务发起HTTPS请求报错:PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException
问题报错 今天上线了我开发的一个OAuth2单点登录客户端的实现,在测试系统验证没问题,到生产环境由于单点登录服务端HTTPS协议,报错如下: I/O error on POST request fo ...
随机推荐
- JQuery - CheckBox Prop 和 attr 的区别
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Qt5.9 UI设计(三)——添加UI、类及资源文件
前言 设计一个软件,最简单的方式就是把控件直接往UI上放,然后再把功能实现了.这样可以实现基本的功能,但是界面不能缩放,如果拖动软件改变界面的大小,界面上的控件就会乱成一团,或者是界面的控件压根就不会 ...
- [转帖]VMware常用软件ISO下载汇总(2023年6月更新)
https://www.dinghui.org/vmware-iso-download.html#vcsa-patch 请访问 www.dinghui.org 获得最新版,文章将持续更新.[最后更新时 ...
- [转帖]「更易用的OceanBase」|OceanBase 4.0 一体化安装包 - 把简单留给用户
https://www.modb.pro/db/565842 1. OceanBase 3.x 版本安装浅谈 我是在 OceanBase 3.1.4 版本的时候开始尝试入手测试的.刚开始 OB 3.x ...
- 程序调试利器——GDB使用指南
作者:京东科技 孙晓军 # 1\. GDB介绍 GDB是GNU Debugger的简称,其作用是可以在程序运行时,检测程序正在做些什么.GDB程序自身是使用C和C++程序编写的,但可以支持除C和C++ ...
- 深入浅出RPC服务 | 不同层的网络协议
导读: 本系列文章从RPC产生的历史背景开始讲解,涉及RPC核心原理.RPC实现.JSF的实现等,通过图文类比的方式剖析它的内部世界,让大家对RPC的设计思想有一个宏观的认识. 作者:王禹展 京东 ...
- Promise练习文件读取
1. fs读取文件 const fs=require('fs');//引入文件读取模块 fs.readFile('./README.md',(err,data)=>{ // 如果出现错误,抛出错 ...
- vue数据不响应,可能是用法有问题
<template> <div> <div> <span>用户名: {{ userInfo.name }}</span> <span& ...
- 【JS 逆向百例】网洛者反爬练习平台第七题:JSVMPZL 初体验
关注微信公众号:K哥爬虫,持续分享爬虫进阶.JS/安卓逆向等技术干货! 声明 本文章中所有内容仅供学习交流,抓包内容.敏感网址.数据接口均已做脱敏处理,严禁用于商业用途和非法用途,否则由此产生的一切后 ...
- 宏观上看Spring创建对象的过程
宏观上看Spring创建对象的过程 对于对象而言,可以分为简单对象和复杂对象: 简单对象 简单对象指可以直接new的对象: Spring在创建这些对象时,是基于反射来完成的. 复杂对象 复杂对象指不能 ...