我开始收到此错误A token_in_blacklist_callback must be provided via the '@token_in_blacklist_loader' if JWT_BLACKLIST_ENABLED is True
,但无法解决。
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-Extended回调:
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"))
请告知我我做错了什么。先感谢您。 enter image description here