Java访问https时解决sun.security.validator.ValidatorException: PKIX path building failed 问题
2021-02-12 01:19
标签:contex 作者 nec manage building throws imp rust instance 转自:https://www.cnblogs.com/chenzhanxun/articles/4463151.html 今天刚学了网络编程在网上尝试下载东西时报: sun.security.validator.ValidatorException: PKIX path building failed 异常 一阵搜错终于解决了,在这里记录下代码:具体请到上面的原作者阅读! Java访问https时解决sun.security.validator.ValidatorException: PKIX path building failed 问题 标签:contex 作者 nec manage building throws imp rust instance 原文地址:https://www.cnblogs.com/bingstudy/p/12732249.html
static HostnameVerifier hv = new HostnameVerifier() {
public boolean verify(String urlHostName, SSLSession session) {
System.out.println("Warning: URL Host: " + urlHostName + " vs. "
+ session.getPeerHost());
return true;
}
};
private static void trustAllHttpsCertificates() throws Exception {
javax.net.ssl.TrustManager[] trustAllCerts = new javax.net.ssl.TrustManager[1];
javax.net.ssl.TrustManager tm = new miTM();
trustAllCerts[0] = tm;
javax.net.ssl.SSLContext sc = javax.net.ssl.SSLContext
.getInstance("SSL");
sc.init(null, trustAllCerts, null);
javax.net.ssl.HttpsURLConnection.setDefaultSSLSocketFactory(sc
.getSocketFactory());
}
static class miTM implements javax.net.ssl.TrustManager,
javax.net.ssl.X509TrustManager {
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
public boolean isServerTrusted(
java.security.cert.X509Certificate[] certs) {
return true;
}
public boolean isClientTrusted(
java.security.cert.X509Certificate[] certs) {
return true;
}
public void checkServerTrusted(
java.security.cert.X509Certificate[] certs, String authType)
throws java.security.cert.CertificateException {
return;
}
public void checkClientTrusted(
java.security.cert.X509Certificate[] certs, String authType)
throws java.security.cert.CertificateException {
return;
}
}
2)在访问https资源前,调用
trustAllHttpsCertificates();
HttpsURLConnection.setDefaultHostnameVerifier(hv);
上一篇:python的使用
下一篇:二叉树的建立和遍历(c语言描述)
文章标题:Java访问https时解决sun.security.validator.ValidatorException: PKIX path building failed 问题
文章链接:http://soscw.com/index.php/essay/54245.html