我开始得到这个错误一个token_in_blacklist_callback必须通过'@token_in_blacklist_loader'如果JWT_BLACKLIST_ENABLED是真的,我不能解决它。
import os
from flask import Flask
from flask_cors import CORS
from flask_sqlalchemy import SQLAlchemy
from flask_bcrypt import Bcrypt
from flask_jwt_extended import JWTManager
app = Flask(__name__)
# Enabling CORS
CORS(app)
# app configuration
app_settings = os.getenv("APP_SETTINGS", "app.config.DevelopmentConfig")
app.config.from_object(app_settings)
# Initialize Flask Sql Alchemy
db = SQLAlchemy(app)
# Initialize Bcrypt
bcrypt = Bcrypt(app)
# Initialize JWTManager
jwt = JWTManager(app)
from app.auth.views import auth
app.register_blueprint(auth)
from app.resources.user import users
app.register_blueprint(users)
import os
basedir = os.path.abspath(os.path.dirname(__file__))
postgres_local_base = "postgresql://localhost/db"
class Config(object):
DEBUG = False
TESTING = False
CSRF_ENABLED = True
SECRET_KEY = os.getenv("SECRET_KEY", "secret")
SQLALCHEMY_TRACK_MODIFICATIONS = False
BCRYPT_LOG_ROUNDS = 12
PROPAGATE_EXCEPTIONS = True
JWT_BLACKLIST_ENABLED = True
JWT_BLACKLIST_TOKEN_CHECKS = ['access', 'refresh']
class DevelopmentConfig(Config):
DEVELOPMENT = True
DEBUG = True
BCRYPT_LOG_ROUNDS = 4
SQLALCHEMY_DATABASE_URI = os.getenv("DATABASE_URL", postgres_local_base)
class ProductionConfig(Config):
DEBUG = False
class StagingConfig(Config):
DEVELOPMENT = True
DEBUG = True
class TestingConfig(Config):
TESTING = True
BCRYPT_LOG_ROUNDS = 4
DEBUG = True
我创建了一个单独的文件,我添加了所有flask - jwt扩展回调:
from flask import jsonify
from app import jwt
from app.auth.blacklist_helper import is_token_revoked
@jwt.token_in_blacklist_loader
def check_if_token_revoked(decrypted_token):
return is_token_revoked(decrypted_token)
@jwt.expired_token_loader
def expired_token_callback():
return jsonify({
'description': 'The token has expired',
'error': 'token_expired'
}), 401
@jwt.invalid_token_loader
def invalid_token_callback(error):
return jsonify({
'description': 'Signature verification failed',
'error': 'invalid_token'
}), 401
@jwt.unauthorized_loader
def missing_token_callback(error):
return jsonify({
'description': 'Request does not contain an access token',
'error': 'authorization_required'
}), 401
@jwt.needs_fresh_token_loader
def token_not_fresh_callback():
return jsonify({
'description': 'The token is not fresh',
'error': 'fresh_token_required'
}), 401
@jwt.revoked_token_loader
def revoked_token_callback():
return jsonify({
'description': 'The token has been revoked',
'error': 'token_revoked'
}), 401
这是我的API我调用:
class User(MethodView):
"""This method return a user by their id"""
@jwt_required
def get(self, user_id):
user = UserModel.find_user_by_id(user_id=user_id)
print(get_raw_jwt())
if user:
return user.json()
return {'message': 'User not not'}, 404
# Add Rules for the API endpoints
users.add_url_rule('/api/users/<int:user_id>', view_func=User.as_view("user"))
请告诉我哪里做错了。提前谢谢你。 在这里输入图像描述 问题来源StackOverflow 地址:/questions/59378511/a-token-in-blacklist-callback-must-be-provided-via-the-token-in-blacklist-load
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。