android-java.security.cert.CertPathValidatorException:找不到证书路径的信任锚.在api上少于24

android-java.security.cert.CertPathValidatorException:找不到证书路径的信任锚.在api上少于24,第1张

概述com.android.volley.NoConnectionError: javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found. 我从19到24在api

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,此代码不相关,不应使用! Google禁止使用它.

所以谁知道谷歌允许该错误的替代解决方案是什么?

最佳答案首先,您将需要生成您的证书文件,这是步骤

>在firefox浏览器上转到您的网站链接
>单击网站链接右侧的绿色锁
>单击更多信息,然后查看证书
>将出现一个新窗口,其中包含两个常规和详细信息
选择细节
>单击导出以导出证书并保存此文件
在androID项目资产中.

项目应用程序类中的第二个定义hurlStack变量,并在应用程序中使用next方法OnCreate方法

 private voID handleCertificationOnolderDevices() {    try {        CertificateFactory cf = CertificateFactory.getInstance("X.509");        inputStream cainput = new                             BufferedinputStream(getAssets().open("porter_cert.crt"));        Certificate ca;        try {            ca = cf.generateCertificate(cainput);            Log.d("certificate",((X509Certificate) ca).getSubjectDN().toString());        } finally {            cainput.close();        }        String keyStoreType = KeyStore.getDefaultType();        KeyStore keyStore = KeyStore.getInstance(keyStoreType);        keyStore.load(null,null);        keyStore.setCertificateEntry("ca",ca);        String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm();        TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm);        tmf.init(keyStore);        TrustManager[] trustManagers = tmf.getTrustManagers();        final x509trustmanager origTrustmanager =                                                  (x509trustmanager) trustManagers[0];        TrustManager[] wrappedTrustManagers = new TrustManager[]{                new x509trustmanager() {                   public java.security.cert.X509Certificate[] getAcceptedissuers() {                        return origTrustmanager.getAcceptedissuers();                   }                   public voID checkClIEntTrusted(X509Certificate[] certs,String authType)                    {                        try {                            origTrustmanager.checkClIEntTrusted(certs,authType);                        } catch (CertificateException e) {                            e.printstacktrace();                        }                    }                    public voID checkServerTrusted(X509Certificate[] certs,String authType)                     {                        try {                            origTrustmanager.checkServerTrusted(certs,authType);                        } catch (CertificateException e) {                            e.printstacktrace();                        }                    }                }        };        SSLContext context = SSLContext.getInstance("TLS");        context.init(null,tmf.getTrustManagers(),null);        SSLSocketFactory sslSocketFactory = context.getSocketFactory();        hurlStack = new HurlStack(null,sslSocketFactory);    } catch (Exception e) {        e.printstacktrace();    }}

并在volley requestQueue上使用hurlStack

    public RequestQueue getRequestQueue() {       if (requestQueue == null)           requestQueue = Volley.newRequestQueue(getApplicationContext(),hurlStack);       return requestQueue;    }

第三,如果您使用GlIDe拍摄图像,则会遇到与glIDe相关的ssl证书出现第二个错误,您需要通过这种方式解决

1-在应用程序中更新,将您的gilde和okhttp3构建为这些版本

    implementation "com.squareup.okhttp3:okhttp:3.8.1"    implementation 'com.github.bumptech.glIDe:glIDe:4.9.0'    annotationProcessor 'com.github.bumptech.glIDe:compiler:4.9.0'    implementation ('com.github.bumptech.glIDe:okhttp3-integration:4.9.0'){    exclude group: 'glIDe-parent'    }

2-将下一个类添加到您的项目中

@GlIDeModule public class CustomGlIDeModule extends AppGlIDeModule {   @OverrIDe   public voID registerComponents(Context context,GlIDe glIDe,Registryregistry) {      if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.N) {          OkhttpClIEnt clIEnt =                         SafeOkhttpClIEnt.getSafeOkhttpClIEnt(context);          OkhttpUrlLoader.Factory factory = new                                        OkhttpUrlLoader.Factory(clIEnt);          glIDe.getRegistry().replace(GlIDeUrl.class,inputStream.class,factory);      }   } }

现在滑行将与您配合正常. 总结

以上是内存溢出为你收集整理的android-java.security.cert.CertPathValidatorException:找不到证书路径的信任锚.在api上少于24 全部内容,希望文章能够帮你解决android-java.security.cert.CertPathValidatorException:找不到证书路径的信任锚.在api上少于24 所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1145074.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存