Webview:无法验证证书链

时间:2017-11-05 10:44:07

标签: 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

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

 <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}}

2 个答案:

答案 0 :(得分:6)

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

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

然后是方法

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

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

来强制处理url。

请参阅下面的实现和你应该处理的案例你也可以通过你的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();
            }

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

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

答案 1 :(得分:3)

有时是因为您的根ca证书未链接到服务器证书中。

只需在server.crt之后(在同一文件中)添加ca.crt

Webview对此很敏感,但chrome桌面却不...