sun.security.validator.ValidatorException: PKIXpath building failed: sun.security.provider,javax.net.ssT.SSLHandshakeExceptions.certpath.SunCertPathBuilderException
2024-10-21 06:43:31
报错信息:
sun.security.validator.ValidatorException: PKIXpath building failed:
sun.security.provider,javax.net.ssT.SSLHandshakeExceptions.certpath.SunCertPathBuilderException: unable to find valid certification path to reguested target
问题描述:
在java代码中调用其他项目接口,发起的是https请求。报错信息说找不到有效证书路径。
问题解决:
信任所有SSL证书
1、新建一个SslUtil类
package com.asiainfo.strategy.cloud.base.utils;
import javax.net.ssl.*;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
/**
* @Author huoyl
* @create 2023/1/3 14:45
*/
public class SslUtil {
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证书,必须在openConnection之前调用
* @throws Exception
*/
public static void ignoreSsl() throws Exception{
HostnameVerifier hv = new HostnameVerifier() {
public boolean verify(String urlHostName, SSLSession session) {
System.out.println("Warning: URL Host: " + urlHostName + " vs. " + session.getPeerHost());
return true;
}
};
trustAllHttpsCertificates();
HttpsURLConnection.setDefaultHostnameVerifier(hv);
}
}
2、在HttpUtil工具类中修改代码
InputStream inputStream = null;
OutputStream outStream = null;
HttpURLConnection conn = null;
try {
byte[] entity = jsonObject.toJSONString().getBytes();
//信任所有SSL证书
URL url = new URL(path);
if("https".equalsIgnoreCase(url.getProtocol())){
SslUtil.ignoreSsl();
}
conn = (HttpURLConnection) url.openConnection();
// conn = (HttpURLConnection) new URL (path).openConnection ();
conn.setConnectTimeout (5000);// 设置超时
conn.setRequestMethod ("POST");
// 允许对外输出数据
conn.setDoOutput (true);
...
} catch (Exception e) {
e.printStackTrace ();
logger.info("http调用发生异常,错误信息:{}", e.getMessage());
} finally {
if (outStream != null) {
outStream.close();
}
if (conn != null) {
conn.disconnect ();
}
}
忽略HTTPS请求的SSL证书代码,必须在openConnection之前调用
解决方案参考文章https://developer.aliyun.com/article/812846
最新文章
- eclipse导入第三方jar包进入web项目的方法
- 11月14日用AJAX、PHP、SESSION做购物车
- ASP.NET MVC图片管理(上传,预览与显示)
- Windows 托盘区域显示图标
- [Java] 数据库连接管理类
- 云计算PAAS平台测试设计之镜像管理
- (转)浅谈HTML5与css3画饼图!
- <;Chapter 2>;2-1-1.安装Python SDK
- 大四实习准备3_java多线程
- [AngularJS + Webpack] Production Setup
- openstack windows 2008 img
- BZOJ 3479: [Usaco2014 Mar]Watering the Fields(最小生成树)
- (转)Java开发中的23种设计模式详解
- 当final作用于变量、参数、方法和类时该如何处理
- webapi框架搭建-创建项目(二)-以iis为部署环境的配置
- C# 构造器总结
- DevExpress学习笔记之如何获取Repository Item的值
- Unity性能优化 – 脚本篇
- <;转>;php中heredoc与nowdoc的使用方法
- [个人项目] 使用 Vuejs 完成的音乐播放器