规避方法:
debug 状态则先不管;
release 状态则弹窗警告,让用户自行选择;
webView.setWebViewClient(new BridgeWebViewClient(webView) {
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
if (BuildConfig.DEBUG) {
handler.proceed();
} else {
final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage("SSL authentication failed. Do you want to continue accessing?");
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();
}
}
});
onReceivedSslError 用于处理 SSL 错误;
当 WebView 加载一个使用 SSL 加密的网页时,如果发生 SSL 错误,比如证书验证失败或域名不匹配等情况,WebViewClient 的 onReceivedSslError 方法会被调用;
开发者可以重写这个方法,以实现自定义的 SSL 错误处理逻辑,比如忽略错误、取消加载页面或者进行其他操作。
这个方法可以帮助开发者在 WebView 加载 SSL 加密网页时处理可能出现的安全性问题。