【问题标题】:Not getting callback for onReceivedClientCertRequest in Webview没有在 Webview 中获得 onReceivedClientCertRequest 的回调
【发布时间】:2016-03-08 15:25:14
【问题描述】:

我需要为 Webview 进行公钥/证书固定。我看到API21中已经引入了一个api 根据 Android 文档, http://developer.android.com/reference/android/webkit/WebViewClient.html#onReceivedClientCertRequest(android.webkit.WebView, android.webkit.ClientCertRequest)

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);
        }

【问题讨论】:

    标签: android ssl webview ssl-certificate webclient


    【解决方案1】:

    客户端证书身份验证在 Android 中可能以多种方式失败:

    • 您的 WebViewClient 可能未正确连接:确保您从 WebView 收到其他通知,例如 WebViewClient.onPageStarted()
    • 确保您实际使用的是 SSL 和 https URL
    • 在您进行客户端证书检查之前,SSL 可能会失败。这对于自签名服务器证书很典型。您可以通过在WebViewClient.onReceivedSslError(view, handler, error) 中调用handler.proceed() 来解决此问题
    • SSL 客户端证书身份验证可能未在服务器端打开。使用 Apache 时,在配置中设置 SSLVerifyClient require 以及所需的参数 SSLVerifyDepthSSLCACertificateFile
    • 在服务器上使用有效的 CA 证书(由您或第三方创建)和由该 CA 证书签名的客户端证书
    • 确保客户端证书已安装在 Android 设备上。您通常将客户端证书作为 PKCS 12 文件(pfx 文件扩展名)复制到设备的存储中

    【讨论】:

      猜你喜欢
      • 2016-05-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-09
      • 2017-06-03
      • 1970-01-01
      • 2014-04-15
      相关资源
      最近更新 更多