WebView

时间:2017-11-08 12:49:51

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

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

传递证书

1 个答案:

答案 0 :(得分:1)

您必须向WebView类提供WebViewClient对象。当服务器需要客户端身份验证时,将通过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双向握手。