当前位置:网站首页>Trust anchor for certification path not found. Exception solution.
Trust anchor for certification path not found. Exception solution.
2022-07-30 06:55:00 【¥Yiren alone drunk】
1. Exception details:
When doing OkHttp to access the network, the following error always occurs.
java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.
Second, the cause of the problem:
Because the https.bks certificate of the project is not a certificate issued by a formal CA, but a certificate issued by a secondary agent, etc., the verification fails!!!
Third, the solution:
Ignore https certificate verification
Specific method: When obtaining sslParams, modify and customize TrustManager to trustAllCerts
Then add this method to the main class
1, JAVA version:
public static void handleSSLHandshake() {try {TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager() {public X509Certificate[] getAcceptedIssuers() {return new X509Certificate[0];}@Overridepublic void checkClientTrusted(X509Certificate[] certs, String authType) {}@Overridepublic void checkServerTrusted(X509Certificate[] certs, String authType) {}}};SSLContext sc = SSLContext.getInstance("TLS");// trustAllCerts trust all certificatessc.init(null, trustAllCerts, new SecureRandom());HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {@Overridepublic boolean verify(String hostname, SSLSession session) {return true;}});} catch (Exception ignored) {}}
2, Kotlin version:
fun handleSSLHandshake() {try {var trustAllCerts:Array = arrayOf(object: X509TrustManager{override fun checkClientTrusted(p0: Array?, p1: String?) {}override fun checkServerTrusted(p0: Array?, p1: String?) {}override fun getAcceptedIssuers(): Array {val arrayOfNulls = arrayOfNulls(0)return arrayOfNulls}})val sc: SSLContext = SSLContext.getInstance("TLS")// trustAllCerts trust all certificatessc.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, and then call it in the oncreate() method
handleSSLHandshake()
This pit has been encountered a lot of times. After checking a lot of blogs, I found a good solution. I reprinted it and shared it with everyone, hoping to help everyone.
边栏推荐
- Operators and Interaction Basics
- SQL Server Installation Tutorial
- protobuf编码及网络通信应用(一)
- uncategorized SQLException; SQL state [null]; error code [0]; sql injection violation, syntax error
- 标准输入输出流(System.in,System.out)
- C#预定义数据类型简介
- sqli-labs less3/4 Targeting Notes
- CTF misc-audio and video steganography
- Mysql 客户端常见异常分析
- GraphQL(一)基础介绍及应用示例
猜你喜欢
随机推荐
misc-file steganography of CTF
【零基础搞定C语言——导航汇总篇】
线程的5种状态
Trust anchor for certification path not found.异常解决方法。
Detailed MySQL-Explain
在线sql编辑查询工具sql-editor
【数仓】数据仓库高频面试题题英文版(1)
uni-app: about custom components, easycom specs, uni_modules, etc.
史上超强最常用SQL语句大全
Invalid bound statement (not found)出现的原因和解决方法
npm安装和npm安装——保存
phpok website vulnerability exploitation analysis
Calendar类的习题
FastAPI Quick Start
MySQL 索引优化及失效场景
Redis 客户端常见异常分析
Offensive and defensive world easy_web
Go简单实现协程池
【数仓】数据质量
GraphQL (1) Basic introduction and application examples