在Flutter应用程序中,拒绝使用flutter_inappwebview进行本地HTTPS连接是因为flutter_inappwebview默认情况下不信任自签名的SSL证书。这是为了确保应用程序的安全性和防止潜在的安全风险。
然而,如果你确定自签名的SSL证书是可信的,并且你想要在flutter_inappwebview中使用它进行本地HTTPS连接,你可以通过以下步骤来实现:
assets
的文件夹,并将自签名的SSL证书文件(例如certificate.crt
)放入其中。pubspec.yaml
文件中,添加以下代码来声明证书文件:flutter:
assets:
- assets/certificate.crt
rootBundle
来加载证书文件,并创建一个SecurityContext
对象来信任该证书。然后,将该SecurityContext
对象传递给flutter_inappwebview
的InAppWebView
组件。import 'package:flutter/services.dart' show rootBundle;
import 'package:flutter_inappwebview/flutter_inappwebview.dart';
// 加载证书文件
final certificateData = await rootBundle.load('assets/certificate.crt');
final certificate = SecurityContext.defaultContext.useCertificateChainBytes(certificateData.buffer.asUint8List());
// 创建信任证书的SecurityContext对象
final securityContext = SecurityContext.defaultContext;
securityContext.setTrustedCertificatesBytes(certificate);
// 创建InAppWebView组件,并传递SecurityContext对象
final webView = InAppWebView(
initialUrlRequest: URLRequest(
url: Uri.parse('https://example.com'),
headers: {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'},
),
initialOptions: InAppWebViewGroupOptions(
crossPlatform: InAppWebViewOptions(
useShouldOverrideUrlLoading: true,
useOnLoadResource: true,
useOnDownloadStart: true,
useOnReceivedHttpAuthRequest: true,
useOnReceivedServerTrustAuthRequest: true,
useOnReceivedClientCertRequest: true,
useOnReceivedHttpError: true,
useOnReceivedSslError: true,
useOnProgressChanged: true,
useOnConsoleMessage: true,
useShouldInterceptAjaxRequest: true,
useShouldInterceptFetchRequest: true,
useShouldInterceptRequest: true,
useOnLoadResourceCustomSchemeHandler: true,
useOnCreateWindow: true,
useOnWindowFocus: true,
useOnSafeBrowsingHit: true,
useOnReceivedContentWorld: true,
useOnRendererProcessGone: true,
useOnCrash: true,
useShouldOverrideUrlLoadingAndroid: true,
useOnLoadResourceAndroid: true,
useOnReceivedHttpAuthRequestAndroid: true,
useOnReceivedServerTrustAuthRequestAndroid: true,
useOnReceivedClientCertRequestAndroid: true,
useOnReceivedHttpErrorAndroid: true,
useOnReceivedSslErrorAndroid: true,
useOnProgressChangedAndroid: true,
useOnConsoleMessageAndroid: true,
useShouldInterceptAjaxRequestAndroid: true,
useShouldInterceptFetchRequestAndroid: true,
useShouldInterceptRequestAndroid: true,
useOnLoadResourceCustomSchemeHandlerAndroid: true,
useOnCreateWindowAndroid: true,
useOnSafeBrowsingHitAndroid: true,
useOnReceivedContentWorldAndroid: true,
useOnRendererProcessGoneAndroid: true,
useOnCrashAndroid: true,
useShouldOverrideUrlLoadingIOS: true,
useOnLoadResourceIOS: true,
useOnReceivedHttpAuthRequestIOS: true,
useOnReceivedServerTrustAuthRequestIOS: true,
useOnReceivedClientCertRequestIOS: true,
useOnReceivedHttpErrorIOS: true,
useOnReceivedSslErrorIOS: true,
useOnProgressChangedIOS: true,
useOnConsoleMessageIOS: true,
useShouldInterceptAjaxRequestIOS: true,
useShouldInterceptFetchRequestIOS: true,
useShouldInterceptRequestIOS: true,
useOnLoadResourceCustomSchemeHandlerIOS: true,
useOnCreateWindowIOS: true,
useOnSafeBrowsingHitIOS: true,
useOnReceivedContentWorldIOS: true,
useOnRendererProcessGoneIOS: true,
useOnCrashIOS: true,
),
),
onWebViewCreated: (controller) {
// WebView创建完成后,将SecurityContext对象设置给WebView
controller.webView.android.useHybridComposition = true;
controller.webView.android.securityContext = securityContext;
},
);
通过以上步骤,你可以在Flutter应用程序中使用flutter_inappwebview进行本地HTTPS连接,并信任自签名的SSL证书。请注意,这仅适用于开发环境和测试目的,对于生产环境,建议使用受信任的SSL证书。
领取专属 10元无门槛券
手把手带您无忧上云