简体   繁体   English

尽管在这里应用了所有指令,但为什么会出现CertPathValidatorException:找不到证书路径的信任锚。

[英]Although applied all directives in here why am I getting CertPathValidatorException: Trust anchor for certification path not found.?

I am new to Android and Node.js. 我是Android和Node.js的新手。 I am trying to make https service calls from my android application to my server. 我正在尝试从我的Android应用程序向服务器进行https服务调用。 Firstly on the server side I created self signed certificates with the following command; 首先,在服务器端,我使用以下命令创建了自签名证书;

req -x509 -nodes -days 2000 -newkey rsa:2048 -keyout /etc/nginx/ssl/nginx.key -out /etc/nginx/ssl/nginx.crt req -x509 -nodes -days 2000 -newkey rsa:2048 -keyout /etc/nginx/ssl/nginx.key -out /etc/nginx/ssl/nginx.crt

Then I put the created nginx.crt file to my res/raw folder in the android side. 然后,将创建的nginx.crt文件放入android端的res / raw文件夹中。 Below you can see my Anroid code to make https call. 在下面,您可以看到我的Anroid代码进行https调用。

I added the following lines to network_configuration.xml 我在network_configuration.xml中添加了以下几行

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <base-config>
        <trust-anchors>
            <certificates src="@raw/nginx"/>
            <certificates src="system"/>
        </trust-anchors>
    </base-config>
</network-security-config>

Then gave reference from Manifest file like 然后从清单文件中给出了参考

 <application android:networkSecurityConfig="@xml/network_security_config">

Tried the following links but still getting exception 尝试了以下链接,但仍然出现异常

Trust Anchor not found for Android SSL Connection 找不到Android SSL连接的信任锚

https://developer.android.com/training/articles/security-ssl.html https://developer.android.com/training/articles/security-ssl.html

My Code 我的密码

 public RequestQueue getRequestQueue() {
    if (mRequestQueue == null) {
        // getApplicationContext() is key, it keeps you from leaking the
        // Activity or BroadcastReceiver if someone passes one in.

        HurlStack hstakc = new HurlStack() {
            @Override
            protected HttpURLConnection createConnection(URL url) throws IOException {
                HttpsURLConnection httpsURLConnection = (HttpsURLConnection) super.createConnection(url);
                try {
                    httpsURLConnection.setSSLSocketFactory(getSSLSocketFactory());
                   // httpsURLConnection.setHostnameVerifier(getHostnameVerifier());
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return httpsURLConnection;
            }

        };

        mRequestQueue = Volley.newRequestQueue(mCtx.getApplicationContext(),hstakc);
    }


    mRequestQueue.getCache().clear();
    return mRequestQueue;
}


private SSLSocketFactory getSSLSocketFactory()
        throws CertificateException, KeyStoreException, IOException, NoSuchAlgorithmException, KeyManagementException {
    CertificateFactory cf = CertificateFactory.getInstance("X.509");
    InputStream caInput = mCtx.getResources().openRawResource(R.raw.certificate); 

    Certificate ca = cf.generateCertificate(caInput);
    caInput.close();


    KeyStore keyStore = KeyStore.getInstance("BKS");
    keyStore.load(null,null);
    keyStore.setCertificateEntry("ca", ca);

    String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
    TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm);
    tmf.init(keyStore);

    TrustManager[] wrappedTrustManagers = getWrappedTrustManagers(tmf.getTrustManagers());

    SSLContext sslContext = SSLContext.getInstance("TLS");
    sslContext.init(null, wrappedTrustManagers, null);
    return sslContext.getSocketFactory();
}

private TrustManager[] getWrappedTrustManagers(TrustManager[] trustManagers) {
    final X509TrustManager originalTrustManager = (X509TrustManager) trustManagers[0];
    return new TrustManager[]{
            new X509TrustManager() {
                public X509Certificate[] getAcceptedIssuers() {
                    return originalTrustManager.getAcceptedIssuers();
                }

                public void checkClientTrusted(X509Certificate[] certs, String authType) {
                    try {
                        if (certs != null && certs.length > 0){
                            certs[0].checkValidity();
                        } else {
                            originalTrustManager.checkClientTrusted(certs, authType);
                        }
                    } catch (CertificateException e) {
                        Log.w("checkClientTrusted", e.toString());
                    }
                }

                public void checkServerTrusted(X509Certificate[] certs, String authType) {
                    try {
                        if (certs != null && certs.length > 0){
                            certs[0].checkValidity();
                        } else {
                            originalTrustManager.checkServerTrusted(certs, authType);
                        }
                    } catch (CertificateException e) {
                        Log.w("checkServerTrusted", e.toString());
                    }
                }
            }
    };
}

Exception is given in this line HttpsURLConnection httpsURLConnection = (HttpsURLConnection) super.createConnection(url); 在此行中例外HttpsURLConnection httpsURLConnection =(HttpsURLConnection)super.createConnection(url);

Can you help please ? 你能帮忙吗? Thanks 谢谢

Full Stack Trace 全栈跟踪

java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.
           javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.
               at com.android.org.conscrypt.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:355)
               at com.android.okhttp.internal.io.RealConnection.connectTls(RealConnection.java:192)
               at com.android.okhttp.internal.io.RealConnection.connectSocket(RealConnection.java:149)
               at com.android.okhttp.internal.io.RealConnection.connect(RealConnection.java:112)
               at com.android.okhttp.internal.http.StreamAllocation.findConnection(StreamAllocation.java:184)
               at com.android.okhttp.internal.http.StreamAllocation.findHealthyConnection(StreamAllocation.java:126)
               at com.android.okhttp.internal.http.StreamAllocation.newStream(StreamAllocation.java:95)
               at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:281)
               at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:224)
               at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:461)
               at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:407)
               at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:538)
               at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getResponseCode(DelegatingHttpsURLConnection.java:105)
               at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getResponseCode(Unknown Source:0)
               at com.nostra13.universalimageloader.core.download.BaseImageDownloader.getStreamFromNetwork(BaseImageDownloader.java:117)
               at com.nostra13.universalimageloader.core.download.BaseImageDownloader.getStream(BaseImageDownloader.java:88)
               at com.nostra13.universalimageloader.core.LoadAndDisplayImageTask.downloadImage(LoadAndDisplayImageTask.java:291)
               at com.nostra13.universalimageloader.core.LoadAndDisplayImageTask.tryCacheImageOnDisk(LoadAndDisplayImageTask.java:274)
               at com.nostra13.universalimageloader.core.LoadAndDisplayImageTask.tryLoadBitmap(LoadAndDisplayImageTask.java:230)
               at com.nostra13.universalimageloader.core.LoadAndDisplayImageTask.run(LoadAndDisplayImageTask.java:136)
               at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
               at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
               at java.lang.Thread.run(Thread.java:764)

我在清单文件中犯了一个错误。现在它可以了。如果您遇到相同的问题,请首先像上面一样编辑清单。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android:CertPathValidatorException:找不到证书路径的信任锚 - Android: CertPathValidatorException: Trust anchor for certification path not found CertPathValidatorException:未找到证书路径的信任锚 - CertPathValidatorException: Trust anchor for certification path not found java.security.cert.CertPathValidatorException:未找到证书路径的信任锚。 在api少24 - java.security.cert.CertPathValidatorException: Trust anchor for certification path not found. on api less 24 java.security.cert.CertPathValidatorException:未找到证书路径的信任锚。 安卓 2.3 - java.security.cert.CertPathValidatorException: Trust anchor for certification path not found. Android 2.3 java.security.cert.CertPathValidatorException:未找到证书路径的信任锚。 在 Glide 中加载图像时 - java.security.cert.CertPathValidatorException: Trust anchor for certification path not found. while loading image in Glide 获取“未找到证书路径的信任锚”。 对于曾经有效的服务 - Getting “Trust anchor for certification path not found.” for a service that used to work “未找到证书路径的信任锚”的问题。 - Problem with “Trust anchor for certification path not found.” java.security.cert.CertPathValidatorException:找不到证书路径的信任锚 - java.security.cert.CertPathValidatorException: Trust anchor for certification path not found java.security.cert.CertPathValidatorException:找不到证书路径的信任锚 - java.security.cert.CertPathValidatorException: Trust anchor for certification path not found Android JavaMail应用程序-CertPathValidatorException:找不到证书路径的信任锚 - Android JavaMail application - CertPathValidatorException: Trust anchor for certification path not found
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM