繁体   English   中英

如何在任何 Flutter/Dart http 客户端上删除重定向授权 header

[英]How to remove Authorization header on redirect on any Flutter/Dart http client

我目前正在从事一个项目,该项目与许多其他项目一样使用 s3 存储。 在这种情况下,存储通过后端链接。

情况是这样的,我可以通过 URL 获取“附件”,比方说example.com/api/attachments/{uuid} 如果用户获得授权(通过 header Authorization ),它应该返回302状态码并重定向到 s3 url。问题是在重定向后, Authorization header 仍然存在,而 http 客户端返回400响应,这是因为持久Authorization header。有什么办法可以在重定向后删除Authorization header 而无需捕获第一个请求并触发新请求?

我的 http 客户端代码目前如下所示:

  @override
  Future get({
    String url,
    Map<String, dynamic> data,
    Map<String, String> parameters,
  }) async {
    await _refreshClient();
    try {
      final response = await dio.get(
        url,
        data: json.encode(data),
        queryParameters: parameters,
      );
      return response.data;
    } on DioError catch (e) {
      throw ServerException(
        statusCode: e.response.statusCode,
        message: e.response.statusMessage,
      );
    }
  }

  Future<void> _refreshClient() async {
    final token = await auth.token;
    dio.options.baseUrl = config.baseUrl;
    dio.options.headers.addAll({
      'Authorization': 'Bearer $token',
      'Accept': 'application/json',
    });
    dio.options.contentType = 'application/json';
  }

查看 Dio 文档,这似乎是故意的行为。

添加到请求中的所有标头都将添加到重定向请求中。 但是,随请求发送的任何正文都不会成为重定向请求的一部分。

https://api.flutter.dev/flutter/dart-io/HttpClientRequest/followRedirects.html

但是,我理解(并同意)这通常是不受欢迎的行为,我的解决方案是自己手动遵循重定向。 这不是很好,但在紧要关头有效。

    Response<String> response;
    try {
      response = await dio.get(
        url,
        options: Options(
          // Your headers here, which might be your auth headers
          headers: ...,
          // This is the key - avoid following redirects automatically and handle it ourselves
          followRedirects: false,
        ),
      );
    } on DioError catch (e) {
      final initialResponse = e.response;

      // You can modify this to understand other kinds of redirects like 301 or 307
      if (initialResponse != null && initialResponse.statusCode == 302) {
        response = await dio.get(
          initialResponse.headers.value("location")!, // We must get a location header if we got a redirect
          ),
        );
      } else {
        // Rethrow here in all other cases
        throw e;
      }
    }

好消息。 最近已通过 Dart 2.16 / Flutter v2.10 修复此问题!

dart 问题跟踪器中的相关错误:

官方公告: https://medium.com/dartlang/dart-2-16-improved-tooling-and-platform-handling-dd87abd6bad1

TLDR:升级到 Flutter v2.10!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM