【问题标题】:JWT refresh in flutter颤振中的 JWT 刷新
【发布时间】:2021-11-11 01:53:17
【问题描述】:

如何用flutter实现JWT令牌刷新?

我需要使用哪个包?

可以用 HTTP 包实现吗?

我的尝试

getauth(String username, String password) async {
http.post(url, body: {
 'username': "atom",
 'password': "abi",
 'grant_type': "password",
 'client_id': "2LJhXdj2Cu0LlVqHk2ilm1WWHtdwK**********",
 'client_secret':
     "buQIh6tLPC0hG6QZUMm7yP7QuSjBLiffTBx3zY2HYxw95ssXQ4F85ttE5fGHInm1LBkk9GhGiHZCvoR21l4bqIOQTTJBo0nJ5******************"
}).then((response) {
 Map<String, dynamic> responseMap = json.decode(response.body);
 if (response.statusCode == 200) {
   print(response.body);
 }
 print(response.body);
});
}

【问题讨论】:

  • 你需要使用拦截器。拦截器允许我们使用 HttpClient 拦截传入或传出的 HTTP 请求。通过拦截HTTP请求,我们可以修改或改变请求的值。
  • http插件能处理拦截器吗?
  • 检查我的答案

标签: flutter jwt


【解决方案1】:

使用http_interceptor并实施过期令牌重试策略

经验

class ExpiredTokenRetryPolicy extends RetryPolicy {
  @override
  Future<bool> shouldAttemptRetryOnResponse(ResponseData response) async {
    if (response.statusCode == 401) {
      // Perform your token refresh here.

      return true;
    }

    return false;
  }
}

拦截器允许我们使用 HttpClient 拦截传入或传出的 HTTP 请求。通过拦截HTTP请求,我们可以修改或改变请求的值

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-19
    • 2019-05-21
    • 2019-05-31
    • 2021-10-20
    • 2018-01-04
    • 1970-01-01
    • 2015-10-26
    相关资源
    最近更新 更多