Meh*_*liz 5 ssl webview flutter
我有这个问题,的帮助下解决了它这个,但我花了一些时间来搞清楚在哪里把代码,因为代码之间有一点不同flutter_webview_pugin VS webview_flutter。所以这是一个教程来展示如何在 MacOS 上为webview_flutter实现这个方法(在 Windows 上,只有文件可能会有所不同)
1- 将此文件夹/Volumes/.../Flutter/SDK/flutter/.pub-cache/hosted/pub.dartlang.org/webview_flutter-0.3.10+4复制到从项目根目录向上一步的位置,例如例如。
如果这是你的项目:/Volumes/Depo/MyProject/ 那么把插件文件夹放在这里很方便:/Volumes/Depo/edited/
2-然后打开这个文件/Volumes/Depo/edited/webview_flutter-0.3.10+4/android/src/main/java/io/flutter/plugins/webviewflutter/FlutterWebViewClient.java
并添加这一行
@Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
handler.proceed();
}
Run Code Online (Sandbox Code Playgroud)
到 internalCreateWebViewClient 函数。完成后它应该是这样的
private WebViewClient internalCreateWebViewClient() {
return new WebViewClient() {
@TargetApi(Build.VERSION_CODES.N)
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
return FlutterWebViewClient.this.shouldOverrideUrlLoading(view, request);
}
@Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
handler.proceed();
}
@Override
public void onPageFinished(WebView view, String url) {
FlutterWebViewClient.this.onPageFinished(view, url);
}
};
}
Run Code Online (Sandbox Code Playgroud)
3- 添加这些进口
import android.net.http.SslError;
import android.webkit.SslErrorHandler;
Run Code Online (Sandbox Code Playgroud)
由于此方法绕过 SSL,因此不建议将其用于生产用途。
即使服务器的 SSL 证书有效,也会出现此问题。因为有效的 SSL 并不能保证客户端通过该域访问的每个服务最终都将使用相同的来源,在我的情况下,我试图连接到服务器以使用 RTSP 流式传输安全摄像头,但它是“101 交换协议”实现对没有有效 SSL 的不同端口的第一个请求。
您可以使用我的插件flutter_inappwebview。它有很多事件,包括管理 SSL 错误和 SSL 客户端证书请求的事件:
onReceivedServerTrustAuthRequest:当WebView需要执行服务器信任身份验证(证书验证)时触发的事件。这是onReceivedSslError在 Android 上使用事件来实现的。onReceivedClientCertRequest:通知主机应用程序处理 SSL 客户端证书请求。因此,在您的情况下,您只需要使用onReceivedServerTrustAuthRequest事件并简单地返回ServerTrustAuthResponse(action: ServerTrustAuthResponseAction.PROCEED);:
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';
Future main() async {
runApp(new MyApp());
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => new _MyAppState();
}
class _MyAppState extends State<MyApp> {
@override
void initState() {
super.initState();
}
@override
void dispose() {
super.dispose();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: InAppWebViewPage()
);
}
}
class InAppWebViewPage extends StatefulWidget {
@override
_InAppWebViewPageState createState() => new _InAppWebViewPageState();
}
class _InAppWebViewPageState extends State<InAppWebViewPage> {
InAppWebViewController webView;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("InAppWebView")
),
body: Container(
child: Column(children: <Widget>[
Expanded(
child: Container(
child: InAppWebView(
initialUrl: "https://myUrl",
initialHeaders: {},
initialOptions: InAppWebViewWidgetOptions(
inAppWebViewOptions: InAppWebViewOptions(
debuggingEnabled: true,
),
),
onWebViewCreated: (InAppWebViewController controller) {
webView = controller;
},
onLoadStart: (InAppWebViewController controller, String url) {
},
onLoadStop: (InAppWebViewController controller, String url) {
},
onReceivedServerTrustAuthRequest: (InAppWebViewController controller, ServerTrustChallenge challenge) async {
return ServerTrustAuthResponse(action: ServerTrustAuthResponseAction.PROCEED);
},
),
),
),
]))
);
}
}
Run Code Online (Sandbox Code Playgroud)
"https://myUrl"你的网址在哪里。
| 归档时间: |
|
| 查看次数: |
3528 次 |
| 最近记录: |