如何解决如何使用更新的令牌重复请求?
我一直使用http和http_interceptor在flutter应用程序中进行联网。在请求中,我从SecureStorage添加令牌头。现在,一旦令牌过期(401错误),我刷新令牌,更新SecureStorage中的令牌。但是当请求重复时,将使用旧令牌代替新令牌。
class ExpiredTokenRetryPolicy extends RetryPolicy {
@override
Future<bool> shouldAttemptRetryOnResponse(ResponseData response) async {
if (response.statusCode == 401) {
//request for the new token and update it in the SecureStorage.
//But when the request gets repeated the new updated token is not used.
//Same old token is being used
return true;
}
return false;
}
}
解决方法
因为您的interceptRequest函数将在shouldAttemptRetryOnResponse函数之后再次触发。您应该从拦截请求函数的存储中获取新令牌,并将其添加到请求中
data.headers[HttpHeaders.authorizationHeader] = token;
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。