简体   繁体   English

使用flask-jwt-extended 和flask-restx 处理标头和Cookie 令牌

[英]Working with flask-jwt-extended and flask-restx for Headers and Cookie Token

I'm building an API with Flask-Restx, and I'm using flask-jwt-extended for the JSON web Token, I have views like this我正在使用 Flask-Restx 构建一个 API,并且我正在为 JSON web 令牌使用flask-jwt-extended,我有这样的观点

class InputReaderView(Resource):
    """Endpoint for validate args from PMR Response with XSD and Validate DataType from EfileType.json

    Args:
        Resource (_type_): _description_

    Returns:
        _type_: valid or Invalid Field
    """

    @jwt_required()
    def post(self):
        input_reader = reqparse.RequestParser()
        input_reader.add_argument("type", type=str, required=True)
        input_reader.add_argument("data", type=dict, action="append", required=True)
        args = input_reader.parse_args()
        
        xsd_path = os.path.join(BaseConfig.UPLOAD_FOLDER, "xsd")
        
        for file in os.listdir(xsd_path):
            if file.replace(".json", "") == args["type"]:
                efile_path = os.path.join(
                    os.path.join(BaseConfig.UPLOAD_FOLDER, "efile"), "efileTypes.json"
                )
                validator = validate_schedule(
                    efile=efile_path,
                    xsd_schedule=xsd_path,
                    args=args["data"],
                )
                if validator["valid status"] == True:
                    return jsonify(validator["data"], 200)
                else:
                    return abort(400, validator["data"])

and here is my config file这是我的配置文件

class BaseConfig:
    BASE_DIR = Path(__file__).resolve().parent.parent
    SECRET_KEY = "TheSecretKey"
    SQLALCHEMY_TRACK_MODIFICATIONS = True
    UPLOAD_FOLDER  = os.path.join(BASE_DIR, 'media')
    
    # JSON Web Token Configuration
    JWT_SECRET_KEY = "The JWT Secret Key"
    JWT_TOKEN_LOCATION = ["headers", "cookies"]
    JWT_ACCESS_TOKEN_EXPIRES = timedelta(days=1)
    JWT_COOKIE_SECURE = True
    JWT_REFRESH_TOKEN_EXPIRES = timedelta(days=7)
    
class DevelopmentConfig(BaseConfig):
    SQLALCHEMY_DATABASE_URI = "postgresql://postgres:1234@localhost:5432/ecommerce_db"
  
    # JSON Web Token Configuration
    JWT_SECRET_KEY = "The JWT Secret Key"
    JWT_COOKIE_SECURE = True
    FLASK_ENV= "development"

    

class ProductionConfig(BaseConfig):
    SQLALCHEMY_DATABASE_URI = os.environ.get("DATABASE_URI")
    SQLALCHEMY_TRACK_MODIFICATIONS = True
    FLASK_ENV= os.environ.get("FLASK_ENV")

    # JWT Configuration
    JWT_SECRET_KEY = os.environ.get("JWT_SECRET_KEY")
    JWT_COOKIE_SECURE = os.environ.get("JWT_COOKIE_SECURE")

then an error like this appears when I run it using docker and gunicorn然后当我使用 docker 和 gunicorn 运行它时会出现这样的错误

flask_jwt_extended.exceptions.NoAuthorizationError: Missing JWT in headers or cookies (Missing Authorization Header; Missing cookie "access_token_cookie")

I'm using Postman to test the API that I've created,我正在使用 Postman 来测试我创建的 API,

授权

标题

how do I solve the problem above?我该如何解决上述问题? Can anyone help?任何人都可以帮忙吗?

github issue: https://github.com/python-restx/flask-restx/issues/467 github 问题: https://github.com/python-restx/flask-restx/issues/467

It means that your application didn't received the token generated by postman, have you tried to add a cell in the headers section with "Authorization" as name and your token following "Barear " as value?这意味着您的应用程序没有收到由 postman 生成的令牌,您是否尝试在标题部分添加一个单元格,名称为“授权”,您的令牌在“Barear”之后作为值? Or add it in the cookie, like "access_token_cookie='Barear your_token'"或者在cookie中添加,比如“access_token_cookie='Barear your_token'”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM