惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

The GitHub Blog
The GitHub Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Microsoft Security Blog
Microsoft Security Blog
J
Java Code Geeks
S
SegmentFault 最新的问题
Apple Machine Learning Research
Apple Machine Learning Research
N
Netflix TechBlog - Medium
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园_首页
宝玉的分享
宝玉的分享
Google DeepMind News
Google DeepMind News
B
Blog RSS Feed
Hugging Face - Blog
Hugging Face - Blog
量子位
Blog — PlanetScale
Blog — PlanetScale
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
阮一峰的网络日志
阮一峰的网络日志
D
Docker
罗磊的独立博客
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
云风的 BLOG
云风的 BLOG
IT之家
IT之家
MyScale Blog
MyScale Blog
Microsoft Azure Blog
Microsoft Azure Blog

博客园 - ganzhijie

清除应用缓存 Glide将网络图片压缩成指定大小并保存到本地 Android加固之后Apk重签名 Mac显示和隐藏“隐藏文件”命令 Android Installation failed with message INSTALL_FAILED_TEST_ONLY Could not find any version that matches com.android.support:appcompat-v7:30.+.的解决步骤 ndk{abiFilters:}的使用 Android Studio解决support-annotations版本冲突 如何查看手机cpu是32位还是64位 Android DialogFragment 遇到 java.lang.IllegalStateException: Fragment already added: 的解决方法 DialogFragment异常Fragment already added的原因与解决办法 查看apk安装包的AndroidManifest.xml文件 ListView中动态显示和隐藏Header&Footer Android动画效果translate、scale、alpha、rotate Android手机主流屏幕分辨率有哪些? OPhone中的图片特效处理 Android手势(上,下,左和右的判断) Android UI秘笈:谨记该做什么不该做什么 Android permission 访问权限大全
解决javax.net.ssl.SSLHandshakeException: java.security.ce...
ganzhijie · 2021-08-07 · via 博客园 - ganzhijie

解决javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.的问题,出现这个问题的原因是:服务器不信任我们自己创建的证书,所以在代码中必须要忽略证书信任问题。

解决代码如下:

package net.joydao.startools.ssl;

import android.util.Log;

import java.security.cert.X509Certificate;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

public class SSLAgent {

    private final static String TAG = "SSLAgent";
    private final static boolean DEBUG = true;

    private static SSLAgent mSSLAgent;

    public static SSLAgent getInstance(){
        if(mSSLAgent == null){
            mSSLAgent = new SSLAgent();
        }
        return mSSLAgent;
    }

    /**
     * 信任所有的https证书
     * */
    public void trustAllHttpsCertificates() {
        try{
            TrustManager[] trustAllCerts = new TrustManager[1];
            TrustManager tm = new MyTrustManager();
            trustAllCerts[0] = tm;
            SSLContext sc = SSLContext.getInstance("SSL");
            sc.init(null, trustAllCerts, null);
            HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
            HttpsURLConnection.setDefaultHostnameVerifier(mHostnameVerifier);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    private HostnameVerifier mHostnameVerifier = new HostnameVerifier() {

        public boolean verify(String hostname, SSLSession session) {
            log("hostname:" + hostname);
            return true;
        }

    };

    private static class MyTrustManager 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) {
            return;
        }

        public void checkClientTrusted(X509Certificate[] certs, String authType) {
            return;
        }

    }

    private void log(String msg){
        if(DEBUG){
            Log.i(TAG, msg);
        }
    }

}

在调用获取url内容之前调用:

SSLAgent.getInstance().trustAllHttpsCertificates();