2017-10-03 8 views

答えて

-1

この方法を試すことができます。これがベストプラクティスだと私は信じている。

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; 
    } 
関連する問題