报错信息:

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

最新文章

  1. eclipse导入第三方jar包进入web项目的方法
  2. 11月14日用AJAX、PHP、SESSION做购物车
  3. ASP.NET MVC图片管理(上传,预览与显示)
  4. Windows 托盘区域显示图标
  5. [Java] 数据库连接管理类
  6. 云计算PAAS平台测试设计之镜像管理
  7. (转)浅谈HTML5与css3画饼图!
  8. <Chapter 2>2-1-1.安装Python SDK
  9. 大四实习准备3_java多线程
  10. [AngularJS + Webpack] Production Setup
  11. openstack windows 2008 img
  12. BZOJ 3479: [Usaco2014 Mar]Watering the Fields(最小生成树)
  13. (转)Java开发中的23种设计模式详解
  14. 当final作用于变量、参数、方法和类时该如何处理
  15. webapi框架搭建-创建项目(二)-以iis为部署环境的配置
  16. C# 构造器总结
  17. DevExpress学习笔记之如何获取Repository Item的值
  18. Unity性能优化 – 脚本篇
  19. <转>php中heredoc与nowdoc的使用方法
  20. [个人项目] 使用 Vuejs 完成的音乐播放器

热门文章

  1. ThreadLocal的使用及原理解析
  2. 聊聊kafka
  3. MIT6.828学习笔记1
  4. CheckBox 单选实现及取值
  5. 【Shell案例】【wc记录单词长度、for循环和if、awk文本分析工具】7、打印字母数小于8的单词
  6. 你真的了解 RSA 加密算法吗?
  7. Springboot配置多Redis源
  8. 天坑,这样一个lambda随机取数据也有Bug
  9. 如何通过C#合并Word文档?
  10. Day36:List详解