我已经实施了onReceivedSslError
我的 WebViewClient 中的方法来正确处理 webview 中无效的 https 证书:
@Override
public void onReceivedSslError(WebView view, final SslErrorHandler handler, SslError error) {
final AlertDialog.Builder builder = new AlertDialog.Builder(WebActivity.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;
}
message += " Do you want to continue anyway?";
builder.setTitle("SSL Certificate Error");
builder.setMessage(message);
builder.setPositiveButton("continue", (dialog, which) -> handler.proceed());
builder.setNegativeButton("cancel", (dialog, which) -> handler.cancel());
final AlertDialog dialog = builder.create();
dialog.show();
}
当 webview 加载我的网页时SslError.SSL_UNTRUSTED
正在检测到错误。但是,如果我在 Chrome(桌面版或移动版)中打开相同的网址,则证书被视为有效且受信任:
为什么会发生这种情况?
对我来说,这是我试图访问的服务器的问题。它的中间证书链已损坏。这是重定向服务器的链条断了。
当存在断链时,webview 无法解决,因为它不知道在哪里寻找正确的证书。
使用这个工具 https://www.ssllabs.com/ssltest/检查常见的错误配置。请务必检查所有重定向。
安卓不支持权威信息获取 https://www.rfc-editor.org/rfc/rfc5280#section-4.2.2.1
因此不存在友邦保险抓取 https://www.thesslstore.com/blog/aia-fetching/
但是?!...它可以在浏览器中运行是的,它适用于浏览器,因为所有浏览器都带有一个中间体列表,当证书链断开时可以依靠中间体列表。
解决方案:修复服务器上的证书链。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)