Android Webview上的ssl warning的处理方式详解及实例
AndroidWebview上的sslwarning的处理方式详解
前言:
因为最近遇到googlepay上汇报的安全漏洞问题,需要处理sslwarning.
安全提醒
您的应用中WebViewClient.onReceivedSslError处理程序的实施方式很不安全。具体来说,这种实施方式会忽略所有SSL证书验证错误,从而使您的应用容易受到中间人攻击。攻击者可能会更改受影响的WebView内容、读取传输的数据(例如登录凭据),以及执行应用中使用JavaScript的代码。
为了正确处理SSL证书验证,请将您的代码更改为在服务器提供的证书符合您的预期时调用SslErrorHandler.proceed(),否则应调用SslErrorHandler.cancel()。系统已向您的开发者帐号地址发送了一封电子邮件提醒,其中列出了受影响的应用和类。
所以查阅了相关Webview上的访问ssl协议的网址的警告处理方式。
其实大概意思就是说客户端在处理https链接返回的ssl错误的时候不要无脑的直接通过,需要友好的在客户端主动弹出对话框让用户做出选择。
然后添加代码如下:
publicvoidonReceivedSslError(WebViewview,finalSslErrorHandlerhandler, SslErrorerror){ finalAlertDialog.Builderbuilder=newAlertDialog.Builder(WebViewActivity.this); Stringmessage="SSLCertificateerror."; switch(error.getPrimaryError()){ caseSslError.SSL_UNTRUSTED: message="Thecertificateauthorityisnottrusted."; break; caseSslError.SSL_EXPIRED: message="Thecertificatehasexpired."; break; caseSslError.SSL_IDMISMATCH: message="ThecertificateHostnamemismatch."; break; caseSslError.SSL_NOTYETVALID: message="Thecertificateisnotyetvalid."; break; caseSslError.SSL_DATE_INVALID: message="Thedateofthecertificateisinvalid"; break; caseSslError.SSL_INVALID: default: message="Agenericerroroccurred"; break; } message+="Doyouwanttocontinueanyway?"; builder.setTitle("SSLCertificateError"); builder.setMessage(message); builder.setPositiveButton("continue",newDialogInterface.OnClickListener(){ @Override publicvoidonClick(DialogInterfacedialog,intwhich){ handler.proceed(); } }); builder.setNegativeButton("cancel",newDialogInterface.OnClickListener(){ @Override publicvoidonClick(DialogInterfacedialog,intwhich){ handler.cancel(); } }); finalAlertDialogdialog=builder.create(); dialog.show(); }
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!