【问题标题】:Webview displayed insecure content fromWebview 显示的不安全内容来自
【发布时间】:2017-05-04 07:10:49
【问题描述】:

我已经在我的 android 应用程序中实现了 web 视图。它可以完美地在 android marshmallow 中显示网页,但是一台装有 android 4.1.1 的设备没有显示页面。我已添加此代码

public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
            handler.proceed();
        }

SSL 错误。

但在 4.1.1 设备中也不会显示页面

something.com 上的页面显示来自

的不安全内容

有什么帮助吗?

【问题讨论】:

  • 它适合你吗?
  • 还需要什么帮助吗?
  • 这只是一个公共站点,在 chrome 或 chrome 自定义选项卡中打开时完美打开,但在 webview 中出现错误
  • 尽管有警告,我还是想打开这个页面,handler.proceed 有助于在较高的 android 版本上打开页面,但在较低版本上不起作用

标签: android ssl webview


【解决方案1】:

要正确处理 SSL 证书验证,请更改代码以在服务器提供的证书符合您的期望时调用 SslErrorHandler.proceed(),否则调用 SslErrorHandler.cancel()。

 @Override
public void onReceivedSslError(WebView view, final SslErrorHandler handler, SslError error) {
    final AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage(R.string.notification_error_ssl_cert_invalid);
    builder.setPositiveButton("continue", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            handler.proceed();
        }
    });
    builder.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            handler.cancel();
        }
    });
    final AlertDialog dialog = builder.create();
    dialog.show();
}

【讨论】:

  • 这只是一个公共站点,在 chrome 或 chrome 自定义选项卡中打开时完美打开,但在 webview 中出现错误
【解决方案2】:

解决 Google Play 警告:WebViewClient.onReceivedSslError 处理程序

并非总是强制handler.proceed();,但您还必须包含 handler.cancel();这样用户就可以避免加载不安全的内容。

处理 WebViewClient.onReceivedSslError 处理程序的不安全实现

使用以下代码

 webView.setWebViewClient(new SSLTolerentWebViewClient());
 webView.loadUrl(myhttps url);

创建类,

private class SSLTolerentWebViewClient extends WebViewClient {
    public void onReceivedSslError(WebView view, final SslErrorHandler handler, SslError error) {

        AlertDialog.Builder builder = new AlertDialog.Builder(Tab1Activity.this);
        AlertDialog alertDialog = builder.create();
        String message = "SSL Certificate error.";
        switch (error.getPrimaryError()) {
            case SslError.SSL_UNTRUSTED:
                message = "The certificate authority is not trusted.";
                break;
            case SslError.SSL_EXPIRED:
                message = "The certificate has expired.";
                break;
            case SslError.SSL_IDMISMATCH:
                message = "The certificate Hostname mismatch.";
                break;
            case SslError.SSL_NOTYETVALID:
                message = "The certificate is not yet valid.";
                break;
        }

        message += " Do you want to continue anyway?";
        alertDialog.setTitle("SSL Certificate Error");
        alertDialog.setMessage(message);
        alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // Ignore SSL certificate errors
                handler.proceed();
            }
        });

        alertDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

                handler.cancel();
            }
        });
        alertDialog.show();
    }
}

【讨论】:

    猜你喜欢
    • 2013-04-25
    • 1970-01-01
    • 2014-02-14
    • 2013-04-04
    • 1970-01-01
    • 2016-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多