当前位置:网站首页>Trust anchor for certification path not found.异常解决方法。
Trust anchor for certification path not found.异常解决方法。
2022-07-30 05:43:00 【¥伊人独醉】
一、异常详情:
在进行OkHttp访问网络时,总是出现下面的错误。
java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.
二、问题原因:
由于项目的https.bks证书不是正规的CA签发的证书,而是二级代理商等签发的证书,验证不通过造成的!!!
三、解决方案:
忽略https的证书校验
具体做法:需要在获取sslParams时,修改并自定义TrustManager为trustAllCerts
再主类里面加入该方法
1、JAVA 版本:
public static void handleSSLHandshake() {
try {
TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager() {
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[0];
}
@Override
public void checkClientTrusted(X509Certificate[] certs, String authType) {
}
@Override
public void checkServerTrusted(X509Certificate[] certs, String authType) {
}
}};
SSLContext sc = SSLContext.getInstance("TLS");
// trustAllCerts信任所有的证书
sc.init(null, trustAllCerts, new SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
return true;
}
});
} catch (Exception ignored) {
}
}
2、Kotlin 版本:
fun handleSSLHandshake() {
try {
var trustAllCerts:Array<TrustManager> = arrayOf<TrustManager>(object: X509TrustManager{
override fun checkClientTrusted(p0: Array<out X509Certificate>?, p1: String?) {
}
override fun checkServerTrusted(p0: Array<out X509Certificate>?, p1: String?) {
}
override fun getAcceptedIssuers(): Array<X509Certificate?> {
val arrayOfNulls = arrayOfNulls<X509Certificate?>(0)
return arrayOfNulls
}
})
val sc: SSLContext = SSLContext.getInstance("TLS")
// trustAllCerts信任所有的证书
sc.init(null, trustAllCerts, SecureRandom())
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory())
HttpsURLConnection.setDefaultHostnameVerifier(object : HostnameVerifier{
override fun verify(p0: String?, p1: SSLSession?): Boolean {
return true
}
})
}catch (e:Exception){
}
}
3、然后再oncreate()方法里面调用
handleSSLHandshake()
此坑遇到次数实在是多,查了大量博客,才找到好的解决方法,由此转载修改分享给大家,希望能够帮助到大家。
边栏推荐
- MySQL - Multi-table query and case detailed explanation
- sqli-labs less3/4 Targeting Notes
- mysql delete duplicate data in the table, (retain only one row)
- FastAPI Quick Start
- 【面经】米哈游数据开发面经
- Mycat2.0搭建教程
- DVWA installation tutorial (understand what you don't understand · in detail)
- The most powerful and most commonly used SQL statements in history
- JDBC programming of MySQL database
- C# WPF下限制TextBox只输入数字、小数点、删除等键
猜你喜欢

Jackson 序列化失败问题-oracle数据返回类型找不到对应的Serializer

uni-app使用npm命令安装组件

MySQL数据库之JDBC编程

Arrays工具类的使用

【MySQL功法】第5话 · SQL单表查询

《MySQL高级篇》四、索引的存储结构

mysql delete duplicate data in the table, (retain only one row)

oracle row to column, column to row summary

Solution to TypeError The view function did not return a valid response. The function either returned None

jsonpath
随机推荐
sql concat()函数
mysql删除表中重复数据,(只保留一行)
Function 函数式接口及应用
misc-file steganography of CTF
Invalid bound statement (not found)出现的原因和解决方法
学生成绩管理系统(C语言版)
uni-app: The use of uni-icons and how to customize icons
Misc-traffic analysis of CTF
DVWA安装教程(懂你的不懂·详细)
TypeError The view function did not return a valid response. The function either returned None 的解决
TDengine集群搭建
C#预定义数据类型简介
网上说的挖矿究竟是什么? 挖矿系统开发详解介绍
php-fpm
【十年网络安全工程师整理】—100渗透测试工具使用方法介绍
Redis 发布/订阅
互联网商城盲盒app为何如此火爆
C#下大批量一键空投实现
Usage of exists in sql
正则表达式语法详解及实用实例