Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flutter 如何在任何flatter/Dart http客户端上删除重定向时的授权标头_Flutter_Dart_Dio - Fatal编程技术网

Flutter 如何在任何flatter/Dart http客户端上删除重定向时的授权标头

Flutter 如何在任何flatter/Dart http客户端上删除重定向时的授权标头,flutter,dart,dio,Flutter,Dart,Dio,我目前正在从事一个项目,该项目与许多其他项目一样使用s3存储。在这种情况下,存储通过后端链接 情况是这样的,我可以通过URL获取“附件”,比如说example.com/api/attachments/{uuid}。如果用户被授权(通过标题Authorization),它应该返回302statuscode并重定向到S3URL。问题是,在重定向之后,授权头仍然存在,http客户端返回一个400响应,这是因为持续的授权头。在重定向后,是否有任何方法可以在不捕获第一个请求并触发新请求的情况下删除授权头

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

情况是这样的,我可以通过URL获取“附件”,比如说
example.com/api/attachments/{uuid}
。如果用户被授权(通过标题
Authorization
),它应该返回
302
statuscode并重定向到S3URL。问题是,在重定向之后,
授权
头仍然存在,http客户端返回一个
400
响应,这是因为持续的
授权
头。在重定向后,是否有任何方法可以在不捕获第一个请求并触发新请求的情况下删除
授权

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

@覆盖
未来得到({
字符串url,
地图数据,
映射参数,
})异步的{
等待_refreshClient();
试一试{
最终响应=等待dio.get(
网址,
数据:json.encode(数据),
queryParameters:参数,
);
返回响应数据;
}关于错误捕获(e){
抛出服务器异常(
状态代码:即响应状态代码,
消息:e.response.statusMessage,
);
}
}
Future\u refreshClient()异步{
最终令牌=等待auth.token;
dio.options.baseUrl=config.baseUrl;
dio.options.headers.addAll({
“授权”:“持票人$token”,
“接受”:“应用程序/json”,
});
dio.options.contentType='application/json';
}

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

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

然而,我理解(并同意!)这通常是不受欢迎的行为。我的解决方案是自己手动跟踪重定向,这不是很好,但在紧急情况下可以工作

    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;
      }
    }
响应;
试一试{
响应=等待dio.get(
网址,
选项:选项(
//这里是您的标题,可能是您的身份验证标题
标题:。。。,
//这是关键-避免自动跟踪重定向并自行处理
followRedirects:false,
),
);
}关于错误捕获(e){
最终初始响应=e.响应;
//您可以修改它来理解其他类型的重定向,如301或307
if(initialResponse!=null&&initialResponse.statusCode==302){
响应=等待dio.get(
initialResponse.headers.value(“location”)!,//如果获得重定向,则必须获得位置头
),
);
}否则{
//在所有其他情况下,在此处重新缩回
投掷e;
}
}

我也有同样的问题。这实际上是一个令人担忧的安全问题-我不希望我的服务标题发送到第三方重定向。是的,这也是我目前解决它的方法。。虽然不是很流畅,但它确实起到了作用