我目前正在尝试访问Flutter中的Web API,该Web API需要JWT访问令牌进行授权。访问令牌在一定时间后过期。
可以使用单独的刷新令牌来请求新的访问令牌。现在,一旦请求返回401响应,就会执行此访问令牌刷新。 之后,应该使用新的访问令牌重试失败的请求。
我在最后一步遇到了麻烦。 http.BaseRequest
似乎只能发送一次。我将如何使用新令牌重试http请求?
按照dart http readme中的建议,我创建了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();
}
答案 0 :(得分:2)
您不能send
两次BaseRequest
。从第一个副本中新建一个BaseRequest
,然后发送该副本。
这里有一些代码(来自io_client)来“克隆” BaseRequest。
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);
});
答案 1 :(得分:1)
到目前为止,这是我根据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;
}