【问题标题】:Client certificate authentication in WebViewWebView 中的客户端证书认证
【发布时间】:2017-11-08 12:49:51
【问题描述】:

我正在编写一个 Android 应用程序,它在 webview 中查看 https url 。此webpage 使用 SSL 的自签名证书并需要客户端证书进行身份验证。 我如何通过webview 获得证书

【问题讨论】:

    标签: android ssl webview ssl-certificate android-webview


    【解决方案1】:

    您必须为您的 WebView 类提供一个 WebViewClient 对象。当服务器需要Client认证时,通过WebViewClient对象的onReceivedClientCertRequest通知WebView。

    例子:

    MyWebClient 类。

    public class myWebClient extends WebViewClient
    {
        @Override
        public void onReceivedClientCertRequest(WebView view, ClientCertRequest request) {
           //HERE YOU CAN DO SOME STUFF TO RETRIEVE KEY AND CERTIFICATES
            request.proceed(mPrivateKey, mCertificates);
        }
        public void onReceivedError(WebView view, int errorCode,
                                    String description, String failingUrl) {
            super.onReceivedError( view,  errorCode,
                    description,  failingUrl);
        }
    
        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            // TODO Auto-generated method stub
            super.onPageStarted(view, url, favicon);
        }
    
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            // TODO Auto-generated method stub
    
            view.loadUrl(url);
            return true;
    
        }
    }
    

    MainActivity.java

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    
        web = (WebView) findViewById(R.id.webview01);
        web.setWebViewClient(new myWebClient());
        web.getSettings().setJavaScriptEnabled(true);
        web.loadUrl("my_url.com");
    }
    

    proceed(PrivateKey k, X509Certificate[] chain) 方法获取客户端私钥和证书链以完成 SSL 2 次握手。

    【讨论】:

      猜你喜欢
      • 2015-09-06
      • 2010-11-30
      • 2012-09-04
      • 2013-08-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-12
      • 1970-01-01
      相关资源
      最近更新 更多