2016-03-08 80 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文件扩展名)复制到设备的存储器中