2015-11-20 23 views
17

私はロードURLにwebviewを持っていますが、動作しません。私のコードでwebview.loadUrl()のAndroidエラー - 証明書パスのトラストアンカーが見つかりません

ルック:

public class MainActivity extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    WebView wv = (WebView) findViewById(R.id.webView); 

    //Log.d("rudyy", "aqui"); 
    wv.loadUrl("https://tripulanteaims.tam.com.br/wtouch/wtouch.exe/index"); 
    //Log.d("rudyy", "fim"); 


    } 
} 

このコードを実行

は、このエラーを返すアンドロイド:

Failed to validate the certificate chain, error: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found. 

ヘルプミーしてください。

答えて

37

WebViewClientを作成します。

private class WvClient extends WebViewClient 
{ 
    @Override 
    public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError er) { 
     handler.proceed(); 
     // Ignore SSL certificate errors 
    } 
} 

そして、あなたのWebViewに初期化さWebViewClient( "WvClient")(その場合の "WV")に設定します。

wv.setWebViewClient(new WvClient()); 

または1つの行では:

wv.setWebViewClient(new WebViewClient() {@Override public void onReceivedSslError(WebView v, SslErrorHandler handler, SslError er){ handler.proceed(); }}); 
+1

... –

+1

はアノン '' 'webView.setWebViewClient(新WebViewClient(){@Override ます。public void onReceivedSslError(WebViewのビュー、SslErrorHandlerハンドラ、SslErrorエラー)と同じになります受け入れてください。 { handler.proceed(); } }); '' ' – tyoc213

+1

私が必要とするもの.. – MKY

4

私はこれを扱っており、かなり率直にMITM攻撃を許可していますo-no。ここでは、ピニングをサポートするよりクリーンなソリューションがあります。生のリソースフォルダに証明書を保存します。
注:悲しいことに、SSLErrorはgetCertificate()を呼び出すとSslCertificateを返します。 SslCertificateは無用です。公開APIは、公開鍵、発行日、期限日、発行元、発行元のみを確認することはできません。ただし、このクラスを開くと、公開されていないX509Certificateメンバー変数が表示されます。なぜこの設計決定がなされたのか?しかし、Bundleを取得するためのAPIがあり、X509 Certificateメンバー変数がそこに格納されます。証明書にはより多くの便利なメソッドがあるので、そのようにアクセスします。

@Override 
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) { 
    SslCertificate sslCertificateServer = error.getCertificate(); 
    Certificate pinnedCert = getCertificateForRawResource(R.raw.your_cert, mContext); 
    Certificate serverCert = convertSSLCertificateToCertificate(sslCertificateServer); 

    if(pinnedCert.equals(serverCert)) { 
     handler.proceed(); 
    } else { 
     super.onReceivedSslError(view, handler, error); 
    } 
} 

public static Certificate getCertificateForRawResource(int resourceId, Context context) { 
    CertificateFactory cf = null; 
    Certificate ca = null; 
    Resources resources = context.getResources(); 
    InputStream caInput = resources.openRawResource(resourceId); 

    try { 
     cf = CertificateFactory.getInstance("X.509"); 
     ca = cf.generateCertificate(caInput); 
    } catch (CertificateException e) { 
     Log.e(TAG, "exception", e); 
    } finally { 
     try { 
      caInput.close(); 
     } catch (IOException e) { 
      Log.e(TAG, "exception", e); 
     } 
    } 

    return ca; 
} 

public static Certificate convertSSLCertificateToCertificate(SslCertificate sslCertificate) { 
    CertificateFactory cf = null; 
    Certificate certificate = null; 
    Bundle bundle = sslCertificate.saveState(sslCertificate); 
    byte[] bytes = bundle.getByteArray("x509-certificate"); 

    if (bytes != null) { 
     try { 
      CertificateFactory certFactory = CertificateFactory.getInstance("X.509"); 
      Certificate cert = certFactory.generateCertificate(new ByteArrayInputStream(bytes)); 
      certificate = cert; 
     } catch (CertificateException e) { 
      Log.e(TAG, "exception", e); 
     } 
    } 

    return certificate; 
} 
+0

こんにちはRyan ...これらのメソッドをオーバーライドするだけですか? – RickON

+0

@RickON WebViewClientを拡張します。これは、onReceivedSslErrorメソッドをオーバーライドします。 –

+0

FYI、証明書のピン割り当てを使用していて、固定する証明書が変更された場合は、アプリが破損します。考慮すべきちょうど何か... –

関連する問題