【问题标题】:Excessive number of api calls for refresh token刷新令牌的 api 调用次数过多
【发布时间】:2020-01-26 04:32:48
【问题描述】:

在阅读了多个 JWT 刷新令牌教程后,我仍然不清楚 API 调用流程应该如何工作。

这是我的理解:

1) 客户端拥有访问令牌和刷新令牌。它将访问令牌提交给api/getCustomerData

2) 假设访问令牌已过期。服务器回复401

3) 客户端响应401 请求,将刷新令牌发送到api/token

4) 服务器响应新的访问令牌,因为刷新令牌仍然有效。

5) 客户端随后使用新的访问令牌向api/getCustomerData 发出新请求。

我的印象是,这是过多的 API 调用,但我没有看到任何教程阐明了更有效地执行此操作的方法。就目前而言,如果我遵循这种模式,每个 API 请求将如下所示:

    const getCustomers = async () => {
    const config = {
        data: body,
        withCredentials: true,
        method: 'POST' as 'POST',
    }
    await axios(address + '/api/getCustomerData', config)
        .then((response) => {
            ...
        })
        .catch((error: any) => {
            const response = error.response;
            if (response) {
                if (response.status === 401) {
                    if (!failcount){
                       failcount++;
                       getCustomers();
                    }
                    else {
                       history.push('/login')
                    }
                }
            }
        })
    }

【问题讨论】:

    标签: authentication jwt axios


    【解决方案1】:

    您可以做的是在您知道当前访问令牌即将到期之前不久,使用令牌的“exp(到期时间)”声明,使用刷新令牌抢先获取新的访问令牌。这至少会删除一个 API 调用 - 使用访问令牌的调用会导致 401。

    【讨论】:

      猜你喜欢
      • 2019-11-26
      • 2022-10-31
      • 1970-01-01
      • 1970-01-01
      • 2021-06-24
      • 2019-08-05
      • 2018-09-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多