处理Android Webview中的证书错误并清除证书参数

and*_*seb 16 ssl https android android-webview android-security

我正在尝试找到一种在Android Webview中处理SSL证书错误的正确方法.我的目标是提供一种加载具有SSL证书错误的页面的方法,但是当用户尝试加载带有证书错误的URL时,让用户选择在警告他安全性之后加载该页面.

我在线程中找到的最接近的解决方案建议覆盖WebViewClient,如下所示:

webView.setWebViewClient(new WebViewClient() {
    @Override
    public void onReceivedSslError(final WebView view, final SslErrorHandler handler, final SslError error) {
        handler.proceed();
    }
});
Run Code Online (Sandbox Code Playgroud)

但是,这在没有用户同意的情况下基本上禁用了WebView中的SSL.

这里参考的是我找到解决方案的线程:

Android WebView SSL'安全警告'

Android上的Web View是否支持SSL?

Android WebView未加载HTTPS URL

带有客户端证书的android webview

在Android中使用WIFI时,Web视图在加载URL后显示空白/白页

无法在Android网页视图上加载特定网页

WebView显示某些链接的空白视图

Android WebView阻止从https重定向到http

忽略webview中的ssl证书请求

我继续实施了一个略有不同的版本,提示用户:

webView.setWebViewClient(new WebViewClient() {
    @Override
    public void onReceivedSslError(final WebView view, final SslErrorHandler handler, final SslError error) {
        //Showing a first confirmation dialog
        AndroidUtils.showYesNoDialog(
            //First confirmation message
            "WARNING - THIS PAGE IS NOT SECURE! Are you sure you want to continue loading it?",
            //First confirmation "YES" option runnable
            new Runnable() {
                @Override
                public void run() {
                    //Showing a second confirmation dialog
                    AndroidUtils.showYesNoDialogWithResId(
                        //Second confirmation message
                        "You chose to load an unsecure page, are you sure you want to do that?",
                        //Second confirmation "YES" option runnable
                        new Runnable() {
                            @Override
                            public void run() {
                                //Disregard the error and proceed with the bad certificate anyways
                                handler.proceed();
                            }
                        },
                        //Second confirmation "NO" option runnable
                        new Runnable() {
                            @Override
                            public void run() {
                                //Cancel loading the page with that certificate error
                                handler.cancel();
                            }
                        }
                    );
                }
            },
            //First confirmation "NO" option runnable
            new Runnable() {
                @Override
                public void run() {
                    //Cancel loading the page with that certificate error
                    handler.cancel();
                }
            });
    }
});
Run Code Online (Sandbox Code Playgroud)

此实现向用户询问是否要加载页面两次,如果他说是两次,则忽略错误并加载页面,否则页面加载被取消.

第一次加载带有证书错误的URL时WebViewClient.onReceivedSslError,如果用户继续执行证书错误并被SslErrorHandler.proceed()调用,则以下相同的URL加载,WebViewClient.onReceivedSslError永远不会再次调用:只有杀死应用程序才会重置此行为.

WebViewClient.onReceivedSslError当加载证书错误的URL加载时,我希望系统地调用,而不仅仅是第一次.我尝试调用这些方法但没有成功:

/** JAVADOC QUOTE: Clears the SSL preferences table stored in response to proceeding with SSL certificate errors.*/
webView.clearSslPreferences();
//Those other methods I tried out of despair just in case
webView.clearFormData();
webView.clearCache(true);
webView.clearHistory();
webView.clearMatches();
Run Code Online (Sandbox Code Playgroud)

在调用之后,是否有人知道如何WebViewClient.onReceivedSslError为同一个URL多次调用WebView SslErrorHandler.proceed()

Chi*_*nda 5

永远不要重写onReceivedSslError方法。Goole播放将拒绝您的上传,最明智的方法是处理SSL错误使用webSettings.setDomStorageEnabled(true);


小智 2

是的,您可以像这样使用clearSslPreferences():

webView.clearSslPreferences()
Run Code Online (Sandbox Code Playgroud)

它将明确您对 WebView 对象的决定