如何使用jwt刷新令牌

问题描述 投票:0回答:1

我正在使用python flask_jwt_extended处理jwt。我有一个刷新端点(来自文档),如下所示:

# The jwt_refresh_token_required decorator insures a valid refresh
# token is present in the request before calling this endpoint. We
# can use the get_jwt_identity() function to get the identity of
# the refresh token, and use the create_access_token() function again
# to make a new access token for this identity.
@app.route('/refresh', methods=['POST'])
@jwt_refresh_token_required
def refresh():
    current_user = get_jwt_identity()
    ret = {
        'access_token': create_access_token(identity=current_user)
    }
    return jsonify(ret), 200

我不确定何时应该在前端调用此端点。当我尝试使用受保护的端点时,我得到以下信息(这是预期的):

{
  "msg": "Token has expired"
}

我应该怎么知道在前端过期之前刷新令牌,它将如何做?

flask jwt refresh-token flask-jwt-extended
1个回答
0
投票

所以您通常要做的是有两个发出令牌的端点

以及发出刷新令牌的刷新

因此,在您第一次从JS代码调用时,您将获得令牌。令牌通常包括DateTime或到期时间(以秒为单位)

然后在您的JS中,您将检查令牌剩余的时间,如果还不多,则应致电refrehtoken路由以获取新令牌

© www.soinside.com 2019 - 2024. All rights reserved.