Failed to validate the certificate chain, error:
来源:1-1 Flutter入门课程导学

酒巷清风
2024-05-16
无法打开网页报错:
I/cr_X509Util(26769): Failed to validate the certificate chain, error: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.
E/chromium(26769): [ERROR:ssl_client_socket_impl.cc(995)] handshake failed; returned -1, SSL error code 1, net_error -202
写回答
2回答
-
CrazyCodeBoy
2024-05-17
.修改 Android 原生代码
#### 步骤1: 打开 `MainActivity.java` 或 `MainActivity.kt`
在 `android/app/src/main/java/` 目录下找到你的 `MainActivity` 文件(通常是 `MainActivity.java` 或 `MainActivity.kt`)。
#### 步骤2: 修改 `MainActivity.java` 文件
在 `MainActivity.java` 文件中添加自定义 `WebViewClient` 以忽略 SSL 证书错误:
```java
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.webkit.SslErrorHandler;
import android.net.http.SslError;
import io.flutter.embedding.android.FlutterActivity;
public class MainActivity extends FlutterActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 这段代码的主要目的是忽略SSL证书错误
WebView webView = new WebView(this);
webView.setWebViewClient(new WebViewClient() {
@Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
handler.proceed(); // 忽略SSL证书错误
}
});
}
}
```
#### 步骤2: 修改 `MainActivity.kt` 文件
在 `MainActivity.kt` 文件中添加自定义 `WebViewClient` 以忽略 SSL 证书错误:
```kotlin
import android.os.Bundle
import android.webkit.WebView
import android.webkit.WebViewClient
import android.webkit.SslErrorHandler
import android.net.http.SslError
import io.flutter.embedding.android.FlutterActivity
class MainActivity : FlutterActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// 这段代码的主要目的是忽略SSL证书错误
val webView = WebView(this)
webView.webViewClient = object : WebViewClient() {
override fun onReceivedSslError(view: WebView, handler: SslErrorHandler, error: SslError) {
handler.proceed() // 忽略SSL证书错误
}
}
}
}
```
### 3. 修改 iOS 原生代码
#### 步骤1: 打开 `AppDelegate.swift` 文件
在 `ios/Runner/` 目录下找到并打开 `AppDelegate.swift` 文件。
#### 步骤2: 修改 `AppDelegate.swift` 文件
在 `AppDelegate.swift` 文件中添加自定义 `WKNavigationDelegate` 以忽略 SSL 证书错误:
```swift
import UIKit
import Flutter
import WebKit
@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
GeneratedPluginRegistrant.register(with: self)
let webView = WKWebView()
webView.navigationDelegate = self
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
}
extension AppDelegate: WKNavigationDelegate {
func webView(_ webView: WKWebView, didReceive challenge: URLAuthenticationChallenge,
completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
completionHandler(.useCredential, URLCredential(trust: challenge.protectionSpace.serverTrust!))
}
}
```00 -
CrazyCodeBoy
2024-05-17
代码截图发一下,我帮你看看012024-05-17
相似问题