【问题标题】:how to implement JSON Web Token with DRF (Django rest framework) and mongoengine?如何使用 DRF(Django rest 框架)和 mongoengine 实现 JSON Web Token?
【发布时间】:2016-03-23 20:02:41
【问题描述】:

我使用 django-rest-framework 和 mongoengine 作为我的 REST API 的后端框架和 Angular JS 作为前端。如何使用自定义用户文档实现 JSON Web Token (JWT) 身份验证?我已经检查了这个链接https://jpadilla.github.io/django-rest-framework-jwt/。 但它只支持 django-rest 和 Auth User 模型。我该怎么做呢?我应该尝试使用 python 库https://pyjwt.readthedocs.org/en/latest/installation.html 来实现它吗?欢迎所有建议。 谢谢。

【问题讨论】:

    标签: django django-rest-framework jwt mongoengine


    【解决方案1】:

    你可能想在你的设置文件中进行配置

    AUTH_USER_MODEL = 'your_custom_user_class
    

    https://docs.djangoproject.com/en/3.0/topics/auth/customizing/#substituting-a-custom-user-model

    然后您可以继续使用 Django Rest Framework 的身份验证类:

    REST_FRAMEWORK = {
        ...
        'DEFAULT_AUTHENTICATION_CLASSES': (
            'rest_framework_jwt.authentication.JSONWebTokenAuthentication',
        ),
        ...
    }
    

    【讨论】:

    • 如果使用 sqlalchemy 作为 ORM,即代替 MyUser(models.Model):我有 MyUser(Base):
    猜你喜欢
    • 2018-04-18
    • 1970-01-01
    • 2018-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-16
    • 2021-08-25
    • 1970-01-01
    相关资源
    最近更新 更多