简体   繁体   English

gunicorn 和 flask-jwt-extended 没有获得当前用户

[英]gunicorn and flask-jwt-extended not getting current user

I've been building an app that uses flask, flask_jwt_extended and the decorator @jwt_required around protected functions that need an access token to access.我一直在构建一个应用程序,它使用flask、 flask_jwt_extended和装饰器@jwt_required围绕需要访问令牌才能访问的受保护函数。 From these endpoints, I can use flask_jwt_extended 's get_current_user function to fetch the currrent user.从这些端点,我可以使用flask_jwt_extendedget_current_user函数来获取当前用户。

This has been working fine while in development, but now I'm planning to serve a production application using gunicorn .这在开发过程中一直运行良好,但现在我计划使用gunicorn为生产应用程序提供服务。 However, when I run the gunicorn server, it seems like the get_current_user function is always returning none.但是,当我运行gunicorn服务器时,似乎get_current_user函数总是不返回任何内容。 For example, the following code works with python3 -m flask run , but not with gunicorn run:app -b localhost:5000 .例如,以下代码适用于python3 -m flask run ,但不适用于gunicorn run:app -b localhost:5000 What could be the problem here?这里可能有什么问题?

@jwt_required
def get_user_by_id(user_id: str) -> Dict[str, Any]:
    # returns user when using flask run, but not with gunicorn
    curr_user = get_current_user()

Class-worker班工

If you try to use the sync worker type and set the threads setting to more than 1, the gthread worker type will be used instead.如果您尝试使用同步工作线程类型并将线程设置设置为大于 1,则将使用 gthread 工作线程类型。

  • In your python configuration file or you can change worker-class directly in the command line.在您的 python 配置文件中,或者您可以直接在命令行中更改 worker-class。

worker_class = 'gthread' worker_class = 'gthread'

Check your JWT_SECRET_KEY检查您的 JWT_SECRET_KEY

  • JWT_SECRET_KEY need to have the same value all over each workers JWT_SECRET_KEY需要在每个工人JWT_SECRET_KEY都具有相同的值

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

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