具有基本身份验证的 Flutter WebView
Flutter WebView with Basic Authentication
我正在加载一个网页,我想使用基本身份验证登录,我有使用 Swift 的经验并且能够像下面那样进行基本身份验证,但我无法为我的 Flutter 版本实现基本身份验证应用
----Swift代码---
func configureView() {
let username = "user"
let password = "pass"
let userPasswordString = "\(user):\(pass)"
let userPasswordData = userPasswordString.data(using: String.Encoding.utf8)
let base64EncodedCredential = userPasswordData!.base64EncodedString(options:[])
let authString = "Basic \(base64EncodedCredential)"
let url = URL(string: "http://myurl")
var request = URLRequest(url: url!)
request.setValue(authString, forHTTPHeaderField: "Authorization")
webView.scalesPageToFit = true
webView.loadRequest(request)
}
--- Flutter WebView ---> 使用 URL
加载 webview
import 'package:flutter/material.dart';
import 'dart:convert';
import 'package:flutter_webview_plugin/flutter_webview_plugin.dart';
class AddShipment extends StatefulWidget {
final String url;
AddShipment(this.url);
@override
State<StatefulWidget> createState() {
return new _AddShipment();
}
}
class _AddShipment extends State<AddShipment> {
final flutterWebviewPlugin = new FlutterWebviewPlugin();
@override
void initState() {
super.initState();
flutterWebviewPlugin.close();
}
@override
void dispose() {
flutterWebviewPlugin.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return new WebviewScaffold(
appBar: new AppBar(
title: new Text("WebView"),
centerTitle: true,
backgroundColor: Colors.blue[900],
elevation: 0.0,
),
url: widget.url,
withJavascript: true,
);
}
}
创建 urlRequest 的正确方法是什么?我试过了:
static Future<Response> getURL(
final String username, final String password) {
final String url = 'http://myurl';
final String auth =
'Basic ' + base64Encode(utf8.encode('$username:$password'));
return http.get(url, headers: {'Authorization': auth});
}
将额外的 headers 添加到 WebviewScaffold
构造函数。
url: widget.url,
withJavascript: true,
headers: {'Authorization': 'Basic ' + base64Encode(utf8.encode('$widget.username:$widget.password'))},
将用户名和密码传递到小部件中,方法与传递 url 相同。
你也可以试试我的插件flutter_inappbrowser (EDIT: it has been renamed to flutter_inappwebview).
在 initialHeaders
属性中使用 Authorization: Basic ...
header 的示例如下所示:
import 'dart:async';
import 'dart:convert';
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;
String username = "USERNAME";
String password = "PASSWORD";
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("InAppWebView")
),
body: Container(
child: Column(children: <Widget>[
Expanded(
child: Container(
child: InAppWebView(
initialUrl: "http://myurl",
initialHeaders: {
'Authorization': 'Basic ' + base64Encode(utf8.encode('$username:$password'))
},
initialOptions: InAppWebViewWidgetOptions(
inAppWebViewOptions: InAppWebViewOptions(
debuggingEnabled: true,
)
),
onWebViewCreated: (InAppWebViewController controller) {
webView = controller;
},
onLoadStart: (InAppWebViewController controller, String url) {
},
onLoadStop: (InAppWebViewController controller, String url) {
},
),
),
),
]))
);
}
}
相反,这是一个使用 onReceivedHttpAuthRequest
事件的示例:
import 'dart:async';
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:flutter_inappbrowser/flutter_inappbrowser.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: "http://myurl",
initialOptions: InAppWebViewWidgetOptions(
inAppWebViewOptions: InAppWebViewOptions(
debuggingEnabled: true,
)
),
onWebViewCreated: (InAppWebViewController controller) {
webView = controller;
},
onLoadStart: (InAppWebViewController controller, String url) {
},
onLoadStop: (InAppWebViewController controller, String url) {
},
onReceivedHttpAuthRequest: (InAppWebViewController controller, HttpAuthChallenge challenge) async {
return HttpAuthResponse(username: "USERNAME", password: "PASSWORD", action: HttpAuthResponseAction.PROCEED);
},
),
),
),
]))
);
}
}
我正在加载一个网页,我想使用基本身份验证登录,我有使用 Swift 的经验并且能够像下面那样进行基本身份验证,但我无法为我的 Flutter 版本实现基本身份验证应用
----Swift代码---
func configureView() {
let username = "user"
let password = "pass"
let userPasswordString = "\(user):\(pass)"
let userPasswordData = userPasswordString.data(using: String.Encoding.utf8)
let base64EncodedCredential = userPasswordData!.base64EncodedString(options:[])
let authString = "Basic \(base64EncodedCredential)"
let url = URL(string: "http://myurl")
var request = URLRequest(url: url!)
request.setValue(authString, forHTTPHeaderField: "Authorization")
webView.scalesPageToFit = true
webView.loadRequest(request)
}
--- Flutter WebView ---> 使用 URL
加载 webviewimport 'package:flutter/material.dart';
import 'dart:convert';
import 'package:flutter_webview_plugin/flutter_webview_plugin.dart';
class AddShipment extends StatefulWidget {
final String url;
AddShipment(this.url);
@override
State<StatefulWidget> createState() {
return new _AddShipment();
}
}
class _AddShipment extends State<AddShipment> {
final flutterWebviewPlugin = new FlutterWebviewPlugin();
@override
void initState() {
super.initState();
flutterWebviewPlugin.close();
}
@override
void dispose() {
flutterWebviewPlugin.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return new WebviewScaffold(
appBar: new AppBar(
title: new Text("WebView"),
centerTitle: true,
backgroundColor: Colors.blue[900],
elevation: 0.0,
),
url: widget.url,
withJavascript: true,
);
}
}
创建 urlRequest 的正确方法是什么?我试过了:
static Future<Response> getURL(
final String username, final String password) {
final String url = 'http://myurl';
final String auth =
'Basic ' + base64Encode(utf8.encode('$username:$password'));
return http.get(url, headers: {'Authorization': auth});
}
将额外的 headers 添加到 WebviewScaffold
构造函数。
url: widget.url,
withJavascript: true,
headers: {'Authorization': 'Basic ' + base64Encode(utf8.encode('$widget.username:$widget.password'))},
将用户名和密码传递到小部件中,方法与传递 url 相同。
你也可以试试我的插件flutter_inappbrowser (EDIT: it has been renamed to flutter_inappwebview).
在 initialHeaders
属性中使用 Authorization: Basic ...
header 的示例如下所示:
import 'dart:async';
import 'dart:convert';
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;
String username = "USERNAME";
String password = "PASSWORD";
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("InAppWebView")
),
body: Container(
child: Column(children: <Widget>[
Expanded(
child: Container(
child: InAppWebView(
initialUrl: "http://myurl",
initialHeaders: {
'Authorization': 'Basic ' + base64Encode(utf8.encode('$username:$password'))
},
initialOptions: InAppWebViewWidgetOptions(
inAppWebViewOptions: InAppWebViewOptions(
debuggingEnabled: true,
)
),
onWebViewCreated: (InAppWebViewController controller) {
webView = controller;
},
onLoadStart: (InAppWebViewController controller, String url) {
},
onLoadStop: (InAppWebViewController controller, String url) {
},
),
),
),
]))
);
}
}
相反,这是一个使用 onReceivedHttpAuthRequest
事件的示例:
import 'dart:async';
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:flutter_inappbrowser/flutter_inappbrowser.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: "http://myurl",
initialOptions: InAppWebViewWidgetOptions(
inAppWebViewOptions: InAppWebViewOptions(
debuggingEnabled: true,
)
),
onWebViewCreated: (InAppWebViewController controller) {
webView = controller;
},
onLoadStart: (InAppWebViewController controller, String url) {
},
onLoadStop: (InAppWebViewController controller, String url) {
},
onReceivedHttpAuthRequest: (InAppWebViewController controller, HttpAuthChallenge challenge) async {
return HttpAuthResponse(username: "USERNAME", password: "PASSWORD", action: HttpAuthResponseAction.PROCEED);
},
),
),
),
]))
);
}
}