1

私の Android アプリケーションでは、https 経由で接続します。自己署名証明書を使用して接続しています。API レベル 24 未満 (android nougat より前) のデバイスで動作していますが、android nougat では SSL ハンドシェイク例外がスローされます。

javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: 証明書パスのトラスト アンカーが見つかりません。

これは私がhttps経由で接続する方法です:-

SSLContext context = null;
    try 
    {
        KeyStore keyStore = KeyStore.getInstance("PKCS12");
        // Get the raw resource, which contains the keystore with
        // your trusted certificates (root and any intermediate certs)
        InputStream input = new BufferedInputStream(context.getAssets().open(pkcsFilename));
        try {
            // Initialize the keystore with the provided trusted certificates
            // Also provide the password of the keystore
            keyStore.load(input, password.toCharArray());
        } finally {
            input.close();
        }

        KeyManagerFactory keyFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
        keyFactory.init(keyStore, "".toCharArray());

        // Load CAs from an InputStream
        // (could be from a resource or ByteArrayInputStream or ...)
        CertificateFactory cf = CertificateFactory.getInstance("X.509");
        Certificate ca = null;
        input = new BufferedInputStream(context.getAssets().open(certificateFilename));
        try 
        {
            ca = cf.generateCertificate(input);
        }
        finally
        {
            input.close();
        }
        KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
        trustStore.load(null, null);
        trustStore.setCertificateEntry("server", ca);

        // Create a TrustManager that trusts the CAs in our KeyStore
        TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
        tmf.init(trustStore);

        // Create an SSLContext that uses our TrustManager
        context = SSLContext.getInstance("TLS");
        context.init(keyFactory.getKeyManagers(), tmf.getTrustManagers(), null);

        HttpsURLConnection urlConnection = (HttpsURLConnection) url.openConnection();

次のリンクを試しましたが、役に立ちません。

これは私のネットワーク構成ファイルです。AndroidManifest.xml ファイルに追加しました。

    <?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <domain-config>
        <domain includeSubdomains="true">xyz.com</domain>
        <trust-anchors>
        <certificates src="@raw/root_ca" />
        </trust-anchors>
    </domain-config>
</network-security-config>

これを解決する方法を教えてください。

4

2 に答える 2

0

カスタムトラストマネージャーを追加することで機能しました。SSL コンテキストの初期化中context.init(keyFactory.getKeyManagers(), tmf.getTrustManagers(), null);

I modified it as :

context.init(keyFactory.getKeyManagers(), new TrustManager[] { tm }, null);
TrustManager tm = new X509TrustManager() {
                public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
                }

                public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
                    for (int j=0; j<chain.length; j++)
                    {
                        chain[j].checkValidity();
                        try {
                            chain[j].verify(ca.getPublicKey());
                        } catch (NoSuchAlgorithmException | InvalidKeyException | NoSuchProviderException |
                                SignatureException e) {
                            e.printStackTrace();
                            throw new CertificateException(e.getMessage());
                        }
                    }
                }

                public X509Certificate[] getAcceptedIssuers() {
                    return null;
                }
            };

サーバー証明書で証明書を検証します。今、その作業。

于 2016-11-18T07:30:27.747 に答える
0

System.setProperty("javax.net.ssl.trustStore",newKeystoreFile.getAbsolutePath());

同様に、上記のプロパティhttps://docs.oracle.com/cd/E17802_01/webservices/webservices/reference/tutorials/wsit/doc/WSIT_Security6を使用しようとするたびに、keystorepassword と keypassword プロパティを設定してサーバーを再起動してみて ください。 html

于 2016-11-16T05:35:59.220 に答える