android - java.security.cert.CertPathValidatorException : Trust anchor for certification path not found. on api less 24

标签 android android-volley android-security trustmanager x509trustmanager

com.android.volley.NoConnectionError: javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.

从 19 到 24,我在 api 上的 logcat 中遇到了这个错误,并且在我的应用程序中没有从服务器加载数据我搜索了那个错误并找到了 solution

 @SuppressLint("TrulyRandom")
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("SSL");
        sc.init(null, trustAllCerts, new SecureRandom());
        HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
        HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
            @Override
            public boolean verify(String arg0, SSLSession arg1) {
                return true;
            }
        });
    } catch (Exception ignored) {
    }
}

并在我的应用程序类 onCreate 中调用它,这解决了我的问题,但在该答案中,如果找到该解决方案,则有一个 hint此代码不相关,不应使用!它被谷歌禁止。

所以有人知道谷歌针对该错误允许的替代解决方案是什么吗?

最佳答案

您服务器上的 TLS 版本是多少?最有可能是 1.2 或更高。对于 kitkat 设备,1.2 默认关闭,needs to be enabled .如果您的设备有 Google Play 服务,您可以通过它来执行此操作。否则,您需要一个自定义套接字工厂类,它指定 1.2 并且可能是 1.3。

如果它低于 1.2,则 Kitkat 后的设备将不允许连接,除非您信任所有带有您问题中的 hack 的证书。它不应该更低。

关于android - java.security.cert.CertPathValidatorException : Trust anchor for certification path not found. on api less 24,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55992416/

相关文章:

android - imageView 选中状态不显示

php - JSON 验证器 : Response test vaild but URL test is not

android - 如何在易受攻击的 Intent 方案劫持中为 WebView 使用正确的 Intent

android - 方法返回空值

java - 无法解析 : com. android.volley :volley:1. 0.0

android - Android 权限(我们在 list 文件中定义)与相应 API 调用/方法之间的映射

android-security - 与 PayPal android 相关的 HostnameVerifier 接口(interface)的不安全实现

android - 如何从 Gradle 设置 AAR 输出的名称

android - 如何创建基于 Google Play 游戏的计算机对手

android - Fresco 未加载到我在 Android studio 中的应用程序