[英]Using flask-jwt-extended callbacks with flask-restful and create_app
I'm trying to create API tokens for my flask API with flask-jwt-extended. 我正在尝试使用flask-jwt-extended为我的flask API创建API令牌。 I'm trying to initialize the token_in_blacklist_loader but can't figure out the right way to do that.
我正在尝试初始化token_in_blacklist_loader,但找不到正确的方法。
The problem is that token_in_blacklist_loader
is implemented as a decorator. 问题是
token_in_blacklist_loader
被实现为装饰器。 It is supposed to be used in the following way: 应该以以下方式使用它:
@jwt.token_in_blacklist_loader
def check_if_token_in_blacklist(decrypted_token):
jti = decrypted_token['jti']
return jti in blacklist
^ from the docs here 来自此处文档的 ^
Where jwt
is defined as: 其中
jwt
定义为:
jwt = JWTManager(app)
But if using the create_app
pattern, then jwt
variable is hidden inside a function, and cannot be used in the global scope for decorators. 但是,如果使用
create_app
模式,则jwt
变量将隐藏在函数内部,并且不能在装饰器的全局范围内使用。
What is the right way to fix this / work around this? 解决此问题/解决此问题的正确方法是什么?
Put the JWTManager
in a different file, and initialize it with the jwt.init_app
function 将
JWTManager
放在另一个文件中,并使用jwt.init_app
函数对其进行初始化
As an example, see: 例如,请参见:
https://github.com/vimalloc/flask-jwt-extended/blob/master/examples/database_blacklist/extensions.py https://github.com/vimalloc/flask-jwt-extended/blob/master/examples/database_blacklist/extensions.py
and 和
https://github.com/vimalloc/flask-jwt-extended/blob/master/examples/database_blacklist/app.py https://github.com/vimalloc/flask-jwt-extended/blob/master/examples/database_blacklist/app.py
What I ended up doing was putting the handler inside of create_app
like so: 我最终要做的是将处理程序放入
create_app
如下所示:
def create_app(name: str, settings_override: dict = {}):
app = Flask(name, ...)
...
jwt = JWTManager(app)
@jwt.token_in_blacklist_loader
def check_token_in_blacklist(token_dict: dict) -> bool:
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.