【问题标题】:Flutter WebView with Basic Authentication具有基本身份验证的 Flutter WebView
【发布时间】:2019-09-27 11:33:50
【问题描述】:

我正在加载一个网页,我想使用基本身份验证登录,我有使用 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});
}

【问题讨论】:

    标签: webview flutter basic-authentication


    【解决方案1】:

    你也可以试试我的插件flutter_inappbrowser编辑:它已经重命名为flutter_inappwebview)。

    initialHeaders 属性中使用Authorization: Basic ... 标头的示例如下所示:

    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);
                      },
                    ),
                  ),
                ),
              ]))
        );
      }
    }
    

    【讨论】:

    • 在第一个示例中,您能否改为使用不记名访问令牌并让用户进行身份验证?
    【解决方案2】:

    将您的附加标头添加到 WebviewScaffold 构造函数。

        url: widget.url,
        withJavascript: true,
        headers: {'Authorization': 'Basic ' + base64Encode(utf8.encode('$widget.username:$widget.password'))},
    

    将用户名和密码传递到小部件中,与传递 url 的方式相同。

    【讨论】:

    • 它对我不起作用。我不知道为什么?我想通过url请求直接登录网站。
    猜你喜欢
    • 1970-01-01
    • 2020-06-15
    • 1970-01-01
    • 1970-01-01
    • 2014-04-24
    • 2013-03-03
    • 2017-05-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多