【问题标题】:Android webview get sslError SSL_UNTRUSTED but certificate is validAndroid webview 获取 sslError SSL_UNTRUSTED 但证书有效
【发布时间】:2018-11-05 17:36:24
【问题描述】:

我已经在我的 WebViewClient 中实现了onReceivedSslError 方法来正确处理 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(桌面或移动设备)中打开相同的完全相同的 url,则证书被认为是有效且受信任的:

为什么会这样?

【问题讨论】:

  • Java 不使用 Chrome 的信任库。它有自己的。您的消息应为“证书不受信任。”
  • 我遇到了同样的问题....这个运气好吗?您可以使用 onReceivedSslError 但在生产中 google play store 不会让您发布您的应用程序...我读到 android 停止了 oauth2 登录(我的 url 有验证用户名和密码确认)!你解决了吗?!
  • 你找到解决办法了吗?

标签: android ssl https webview webviewclient


【解决方案1】:

对我来说,这是我试图访问的服务器的问题。它有一个损坏的中间证书链。是重定向服务器的链断开了。 当链中断时,webview 无法解决,因为它不知道在哪里寻找正确的证书。

Use this tool 检查常见的错误配置。请务必检查所有重定向。

Android 不支持Authority Information Access

因此没有AIA Fetching

但是?!.. 它适用于浏览器 是的,它可以在浏览器中使用,因为所有浏览器都带有一个中间列表,当证书链断开时可以使用。

解决方案:修复服务器上的证书链。

【讨论】:

    【解决方案2】:

    即使对我来说,当证书在 android chrome 上抛出无效的 CN(SSL_IDMISMATCH) 时,它也会给出 SSL_UNTRUSTED。 添加了network-security-config,一切似乎都运行良好。 对我来说,我安装了一个未被 webview 拾取的 user-ca。

    添加了这个 sn-p 代码,它允许我使用安装在用户凭据中的 user-ca。

    <network-security-config>  
      <base-config>  
            <trust-anchors>  
                <!-- Trust preinstalled CAs -->  
                <certificates src="system" />  
                <!-- Additionally trust user added CAs -->  
                <certificates src="user" />  
           </trust-anchors>  
      </base-config>  
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-08-01
      • 2021-07-20
      • 2021-11-10
      • 2013-05-09
      • 2023-03-04
      • 1970-01-01
      • 2023-04-02
      • 2013-03-13
      相关资源
      最近更新 更多