我正在使用flask和刷新/访问令牌构建一个应用程序。 如果访问令牌即将过期,我将负责刷新该令牌
有以下代码可自动刷新身份验证令牌:
@app.after_request
def refresh_expiring_jwts(response):
try:
exp_timestamp = get_jwt()["exp"]
now = datetime.now(timezone.utc)
target_timestamp = datetime.timestamp(now + timedelta(minutes=30))
if target_timestamp > exp_timestamp:
access_token = create_access_token(identity=get_jwt_identity())
set_access_cookies(response, access_token)
return response
except (RuntimeError, KeyError):
# Case where there is not a valid JWT. Just return the original respone
return response
这里的问题是没有使用刷新令牌(但根据我对文档的理解,这是刷新令牌的目标)
因此,我尝试添加此函数的decorator:
@jwt_refresh_token_required
然后,只有在cookie中有刷新令牌时,我们才能刷新访问令牌
问题是,我在登录之前遇到了一个问题:当你第一次访问网站时,你需要登录,因此你没有刷新令牌,并且因为上面的代码是@app.after_请求完成的,它将失败,因为用户在cookie中仍然没有刷新令牌。为了让我的新decorator保留上面的脚本,我的代码中有以下错误:
flask_jwt_extended.exceptions.NoAuthorizationError: Missing cookie "refresh_token_cookie"
和网站返回内部错误
所以我想要的不是在每个请求之后执行这个脚本,而是仅在访问机密的请求之后执行(需要@jwt_的函数)
也许这不是正确的方法,所以如果你有更好的解决方案,我就用它
谢谢
目前没有回答
相关问题 更多 >
编程相关推荐