开发者社区> 问答> 正文

如果JWT_BLACKLIST_ENABLED为真,则必须通过'@token_in_blacklis

我开始得到这个错误一个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

展开
收起
kun坤 2019-12-30 10:20:25 1492 0
0 条回答
写回答
取消 提交回答
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载