如何将flask_jwt_extended 与蓝图一起使用?

Posted

技术标签:

【中文标题】如何将flask_jwt_extended 与蓝图一起使用?【英文标题】:How to use flask_jwt_extended with blueprints? 【发布时间】:2019-12-11 15:15:35 【问题描述】:

我正在尝试使用 python3 和 flask 以及 flask_jwt_extended 构建一个博客作为投资组合示例。

我可以像这样创建一个文件,它会运行:

from flask_jwt_extended import (create_access_token, get_jwt_identity, JWTManager, jwt_required, get_raw_jwt)
from flask import Flask, request, Blueprint


app = Flask(__name__)

app.config['JWT_SECRET_KEY'] = 'this-is-super-secret'
app.config['JWT_BLACKLIST_ENABLED'] = True
app.config['JWT_BLACKLIST_TOKEN_CHECKS'] = ['access']

jwt = JWTManager(app)


@app.route(....)
@jwt required

但是当我尝试使用 Blueprint 时,它不会注册 JWTManager

这是我的 user.py 文件:

from flask_jwt_extended import (create_access_token, get_jwt_identity, JWTManager, jwt_required, get_raw_jwt)
from flask import Flask, request, Blueprint


app = Flask(__name__)

app.config['JWT_SECRET_KEY'] = 'this-is-super-secret'
app.config['JWT_BLACKLIST_ENABLED'] = True
app.config['JWT_BLACKLIST_TOKEN_CHECKS'] = ['access']

jwt = JWTManager(app)

user_blueprint = Blueprint('user_blueprint', __name__)

@user_blueprint.route(....)
@jwt required

这是我的 app.py:

from user import *
app = Flask(__name__)
app.register_blueprint(user_blueprint)

现在当我尝试运行 app.py 时,它会返回 500(内部错误)并将其记录到日志文件中:

Traceback (most recent call last):
  File "/usr/local/lib/python3.6/dist-packages/flask_jwt_extended/utils.py", line 127, in _get_jwt_manager
    return current_app.extensions['flask-jwt-extended']
KeyError: 'flask-jwt-extended'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 2292, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1815, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1718, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/local/lib/python3.6/dist-packages/flask/_compat.py", line 35, in reraise
    raise value
  File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1813, in full_dispatch_request
    rv = self.dispatch_request()
  File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1799, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/ali/Desktop/cetia_api/user.py", line 63, in login
    return create_token(user_inputs)
  File "/home/ali/Desktop/cetia_api/user_functions.py", line 103, in create_token
    access_token = create_access_token(identity=data, expires_delta=expires)
  File "/usr/local/lib/python3.6/dist-packages/flask_jwt_extended/utils.py", line 156, in create_access_token
    jwt_manager = _get_jwt_manager()
  File "/usr/local/lib/python3.6/dist-packages/flask_jwt_extended/utils.py", line 129, in _get_jwt_manager
    raise RuntimeError("You must initialize a JWTManager with this flask "
RuntimeError: You must initialize a JWTManager with this flask application before using this method

有人可以告诉我该怎么做吗?在过去的 3 天里,我尝试了一切。调试了20个小时,还是没有修复

【问题讨论】:

【参考方案1】:

您需要在每个位置使用相同的app。目前,您正在 users.py 文件中创建 app,并在 app.py 文件中创建不同的 app

通常您会希望使用烧瓶应用程序工厂模式来执行此操作 (https://flask.palletsprojects.com/en/1.1.x/patterns/appfactories/)。示例可能如下所示:

extensions.py

from flask_jwt_extended import JWTManager

jwt = JWTManager()

users.py

from flask_jwt_extended import (create_access_token, get_jwt_identity, jwt_required, get_raw_jwt)
from flask import Flask, request, Blueprint

user_blueprint = Blueprint('user_blueprint', __name__)

@user_blueprint.route(....)
@jwt_required

app.py

from extensions import jwt
from users import users_blueprint

def create_app():
    app = Flask(__name__)

    app.secret_key = 'ChangeMe!'
    app.config['JWT_BLACKLIST_ENABLED'] = True
    app.config['JWT_BLACKLIST_TOKEN_CHECKS'] = ['access']

    jwt.init_app(app)

    app.register_blueprint(user_blueprint)

    return app

main.py

from app import create_app

app = create_app()

if __name__ == '__main__':
    app.run()
    

【讨论】:

我非常感谢你,伙计。现在一切都好。非常感谢,很多很多。你救了我。

以上是关于如何将flask_jwt_extended 与蓝图一起使用?的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 Flask 和 flask_jwt_extended 进行自定义 JWT 验证?

使用模板渲染实现 flask_jwt_extended

插件flask_jwt_extended

Flask 学习-30.flask_jwt_extended 自定义 token 过期返回内容

Flask 学习-32.flask_jwt_extended 自定义装饰器

Flask 学习-29.flask_jwt_extended插件jwt_required()中可选项optional=True参数