Webview:无法验证证书链

Haz*_*ass 2 android react-native

我正在尝试在反应原生应用程序中打开我的网站https://beta.truckerdistrict.com,它给了我一个白屏,没有任何错误或UI中的警报.我测试了https://facebook.com和其他所有工作正常.检查日志我发现以下错误:

Failed to validate the certificate chain, error: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found
Run Code Online (Sandbox Code Playgroud)

这是我的webview代码,它非常简单,没有任何特殊内容:

 <WebView
        ref={this.WEBVIEW_REF}
        userAgent={this.USER_AGENT}
        javaScriptEnabled={true}
        uploadEnabledAndroid={true}
        allowUniversalAccessFromFileURLs={true}
        mixedContentMode="always"
        onNavigationStateChange={this._onNavigationStateChange}
        onLoadEnd={this._onLoadEnd}
        onError={this._onLoadError}
        onMessage={this._onWebMessage}
        source={{uri: this.BASE_URL}}
Run Code Online (Sandbox Code Playgroud)

Mr-*_*IDE 8

如果您使用的是 Android 7.0/6/5/4 设备(API 24 或更低版本),并且您的网站使用“ Let's Encrypt ”SSL 证书来获取 URL 的 HTTPS 支持,这些设备将不再信任 Let's Encrypt 证书,自2021 年 9 月 1 日起。WebViews您将开始在加载 HTTPS 页面时看到类似这样的错误:

I/X509Util: Failed to validate the certificate chain, error: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.
Run Code Online (Sandbox Code Playgroud)

或者

javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.
Run Code Online (Sandbox Code Playgroud)

onReceivedSslError()如果你在你的 中使用WebView,它会触发以下情况SslError.SSL_UNTRUSTED

Android 7.1 (API 25) 及以上版本不受影响。

可能的解决方法是在这些旧设备上回退到 HTTP,或者更改 Web 后端服务上的证书服务。另一个潜在的解决方法是在旧设备上使用 Firefox Web 浏览器,因为 Firefox 确实信任 Let's Encrypt。

资料来源:


Sou*_*ess 7

webview无法加载URL的原因是SSL证书.当webview无法验证ssl ceriticate时,它会抛出异常

(错误:java.security.cert.CertPathValidatorException:找不到证书路径的信任锚.)

然后是方法

onReceivedSslError(WebView视图,SslErrorHandler处理程序,SslError错误)

默认情况下,如果不是@overrided,则调用url的处理被取消,否则如果实现我们在SsError对象中有不同类型的ssl错误,我们应该通过调用handler.process来强制处理url.

请看下面的实现和你应该处理的案例你也可以通过ur URL ssl证书调试和了解真正的问题:

     @Override
            public void onReceivedSslError(WebView view, SslErrorHandler handler,
                                           SslError error) {

                switch (error.getPrimaryError()) {
                    case SslError.SSL_UNTRUSTED:
                        LogUtility.debug("SslError : The certificate authority is not trusted.");
                        break;
                    case SslError.SSL_EXPIRED:
                        LogUtility.debug("SslError : The certificate has expired.");
                        break;
                    case SslError.SSL_IDMISMATCH:
                        LogUtility.debug("The certificate Hostname mismatch.");
                        break;
                    case SslError.SSL_NOTYETVALID:
                        LogUtility.debug("The certificate is not yet valid.");
                        break;
                }
                handler.proceed();
            }
Run Code Online (Sandbox Code Playgroud)

另请注意,有时应用程序未发布到PlayStore,因为我们应该提供一个带有是或否的弹出消息,让用户决定是否同意打开该URL,即使存在证书问题或没有.


我不知道但应该有办法强制从网上验证ssh证书的过程

  • 但是,当同一个站点在普通网络浏览器中完美运行时,*为什么*证书会无法验证? (7认同)