-6

我需要信任管理器默认实现的源代码

4

2 回答 2

0

Android 使用几种不同的信任管理器。很多代码都在 Google Conscrypt 库的扩展中。这里有一个例子

这里还有另一个非 Constrypt 示例。它主要代表电话。就我个人而言,虽然经验不多,但委托人是上面提到的 Conscrypt 信任经理之一。

于 2022-02-19T00:26:16.453 回答
-2

你可以试试这种方式。我相信这是最好的做法。

  try {
                TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
                trustManagerFactory.init((KeyStore) null);
                TrustManager[] trustManagers = trustManagerFactory.getTrustManagers();
                X509TrustManager trustManager = (X509TrustManager) trustManagers[0];
                httpClient.sslSocketFactory(getSSLConfig(getApplicationContext()).getSocketFactory(), trustManager);
            } catch (Exception e) {
                Timber.e(String.valueOf(e));
            }


private static SSLContext getSSLConfig(Context context) throws CertificateException, IOException,
            KeyStoreException, NoSuchAlgorithmException, KeyManagementException {
        CertificateFactory cf = CertificateFactory.getInstance("X.509");
        Certificate ca;
        try (InputStream cert = context.getResources().openRawResource(R.raw.yourCERT)) {
            ca = cf.generateCertificate(cert);
        }
        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);
        SSLContext sslContext = SSLContext.getInstance("TLS");
        sslContext.init(null, tmf.getTrustManagers(), null);
        return sslContext;
    }
于 2017-10-03T05:36:44.150 回答