2016-03-08 79 views
0

我需要爲Webview執行公鑰/證書鎖定。我看到有已API21 被引入作爲每Android文檔的API, http://developer.android.com/reference/android/webkit/WebViewClient.html#onReceivedClientCertRequest(android.webkit.WebView,android.webkit.ClientCertRequest)未在Webview中獲取onReceivedClientCertRequest的回調

onReceivedClientCertRequest()在API 21中添加的,但我沒有得到回調,當我加載任何URL。任何人都可以請幫忙?

@Override 
public void onReceivedClientCertRequest(WebView view, final ClientCertRequest request) { 
      Log.e("ClientCertRequest", "===> certificate required!"); 

      KeyChain.choosePrivateKeyAlias(WebViewActivity.this, new KeyChainAliasCallback(){ 
       @TargetApi(Build.VERSION_CODES.LOLLIPOP) 
       @Override 
       public void alias(String alias) { 
        Log.e(getClass().getSimpleName(), "===>Key alias is: " + alias); 
        try { 
         PrivateKey changPrivateKey = KeyChain.getPrivateKey(WebViewActivity.this, alias); 
         X509Certificate[] certificates = KeyChain.getCertificateChain(WebViewActivity.this, alias); 
         Log.v(getClass().getSimpleName(), "===>Getting Private Key Success!"); 
         request.proceed(changPrivateKey, certificates); 
        } catch (KeyChainException e) { 
         Log.e(getClass().getSimpleName(), Util.printException(e)); 
        } catch (InterruptedException e) { 
         Log.e(getClass().getSimpleName(), Util.printException(e)); 
        } 
       } 
      },new String[]{"RSA"}, null, null, -1, null); 
      super.onReceivedClientCertRequest(view,request); 
     } 

回答

1

客戶端證書身份驗證可以通過多種方式無法在安卓

  • 你WebViewClient可能沒有被正確的接線方式:請確保您從web視圖如WebViewClient.onPageStarted()
  • 製作得到其他通知確定您實際上使用SSL和https URL
  • 甚至在您進行客戶端證書檢查之前,SSL可能會失敗。這對於自簽名服務器證書是很典型的。您可以通過調用WebViewClient.onReceivedSslError(view, handler, error)
  • 中的handler.proceed()來解決此問題。服務器端可能未打開SSL客戶端證書身份驗證。在使用Apache時,請在配置文件中設置SSLVerifyClient require以及所需的參數SSLVerifyDepthSSLCACertificateFile
  • 在服務器上使用有效的CA證書(由您或第三方創建)以及由此CA證書籤署的客戶端證書
  • 確保客戶端證書已安裝在Android設備上。您通常將客戶端證書作爲PKCS 12文件(pfx文件擴展名)複製到設備的存儲器中