java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.

0.标题就是0

1.解决办法

a.通过后台生成正规合法的https证书,存在assets中,进行应用验证,这里又分为单向验证和双向验证,这是最根本的解决办法!

具体请移步

b.信任所有证书

在Application的onCreate方法中添加:

/**
     * 忽略OKHttp所有证书验证(信任所有证书)
     */
    private void handleSSLHandshake() {
        try {
            HttpsUtils.SSLParams sslParams = HttpsUtils.getSslSocketFactory(null, null, null);
            OkHttpClient okHttpClient = new OkHttpClient.Builder()
                    .sslSocketFactory(sslParams.sSLSocketFactory, sslParams.trustManager)
                    //其他配置
                    .build();
            OkHttpUtils.initClient(okHttpClient);
            //使用原生的Http请求
            /*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) {
        }
    }

 

你可能感兴趣的:(疑难杂症)