您的位置:首页 > 移动开发

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

2017-02-25 11:59 656 查看

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

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

您可能感兴趣的文章:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Android Webview ssl warning