6

我目前正在尝试访问 Flutter 中需要 JWT 访问令牌进行授权的 Web API。访问令牌在一定时间后过期。

可以使用单独的刷新令牌请求新的访问令牌。现在,只要请求返回 401 响应,就会执行此访问令牌刷新。之后,应该使用新的访问令牌重试失败的请求

我在最后一步遇到了麻烦。好像http.BaseRequest只能发送一次。如何使用新令牌重试 http 请求?


正如dart http 自述文件中所建议的那样,我创建了一个子类http.BaseClient来添加授权行为。这是一个简化版本:

import 'dart:async';

import 'package:http/http.dart' as http;

class AuthorizedClient extends http.BaseClient {
  AuthorizedClient(this._authService) : _inner = http.Client();

  final http.Client _inner;
  final AuthService _authService;

  Future<http.StreamedResponse> send(http.BaseRequest request) async {
    final token = await _authService.getAccessToken();
    request.headers['Authorization'] = 'Bearer $token';

    final response = await _inner.send(request);

    if (response.statusCode == 401) {
      final newToken = await _authService.refreshAccessToken();
      request.headers['Authorization'] = 'Bearer $newToken';

      // throws error: Bad state: Can't finalize a finalized Request
      final retryResponse = await _inner.send(request);

      return retryResponse;
    }

    return response;
  }
}

abstract class AuthService {
  Future<String> getAccessToken();
  Future<String> refreshAccessToken();
}
4

2 回答 2

6

到目前为止,这是我根据 Richard Heap 的回答得出的结论:要重新发送请求,我们必须复制它。

到目前为止,我无法为流请求提出解决方案!

http.BaseRequest _copyRequest(http.BaseRequest request) {
  http.BaseRequest requestCopy;

  if(request is http.Request) {
    requestCopy = http.Request(request.method, request.url)
      ..encoding = request.encoding
      ..bodyBytes = request.bodyBytes;
  }
  else if(request is http.MultipartRequest) {
    requestCopy = http.MultipartRequest(request.method, request.url)
      ..fields.addAll(request.fields)
      ..files.addAll(request.files);
  }
  else if(request is http.StreamedRequest) {
    throw Exception('copying streamed requests is not supported');
  }
  else {
    throw Exception('request type is unknown, cannot copy');
  }

  requestCopy
    ..persistentConnection = request.persistentConnection
    ..followRedirects = request.followRedirects
    ..maxRedirects = request.maxRedirects
    ..headers.addAll(request.headers);

  return requestCopy;
}
于 2018-06-29T16:49:54.433 回答
4

你不能send重复BaseRequest两次。BaseRequest从第一个制作一个新的,然后发送该副本。

这是一些用于“克隆”BaseRequest 的代码(来自 io_client)。

  var copyRequest = await _inner.openUrl(request.method, request.url);

  copyRequest
      ..followRedirects = request.followRedirects
      ..maxRedirects = request.maxRedirects
      ..contentLength = request.contentLength == null
          ? -1
          : request.contentLength
      ..persistentConnection = request.persistentConnection;
  request.headers.forEach((name, value) {
    copyRequest.headers.set(name, value);
  });
于 2018-06-29T10:43:18.957 回答