温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Android Webview上的ssl warning的处理方式详解及实例

发布时间:2020-09-30 10:59:09 来源:脚本之家 阅读:366 作者:yizhihaohut 栏目:移动开发

Android Webview上的ssl warning的处理方式详解

前言:

因为最近遇到google pay上汇报的安全漏洞问题,需要处理ssl warning.

安全提醒

您的应用中 WebViewClient.onReceivedSslError 处理程序的实施方式很不安全。具体来说,这种实施方式会忽略所有 SSL 证书验证错误,从而使您的应用容易受到中间人攻击。攻击者可能会更改受影响的 WebView 内容、读取传输的数据(例如登录凭据),以及执行应用中使用 JavaScript 的代码。

为了正确处理 SSL 证书验证,请将您的代码更改为在服务器提供的证书符合您的预期时调用 SslErrorHandler.proceed(),否则应调用SslErrorHandler.cancel()。系统已向您的开发者帐号地址发送了一封电子邮件提醒,其中列出了受影响的应用和类。

所以查阅了相关Webview上的访问ssl协议的网址的警告处理方式。

其实大概意思就是说客户端在处理https链接返回的ssl错误的时候不要无脑的直接通过,需要友好的在客户端主动弹出对话框让用户做出选择。

然后添加代码如下:

public void onReceivedSslError(WebView view,final SslErrorHandler handler,
  SslError error) {
      final AlertDialog.Builder builder = new AlertDialog.Builder(WebViewActivity.this);
      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;
        case SslError.SSL_DATE_INVALID:
          message = "The date of the certificate is invalid";
          break;
        case SslError.SSL_INVALID:
        default:
          message = "A generic error occurred";
          break;
      }
      message += " Do you want to continue anyway?";

      builder.setTitle("SSL Certificate Error");
      builder.setMessage(message);

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

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI