【问题标题】:Unable to authenticate using REST Framework JWT无法使用 REST Framework JWT 进行身份验证
【发布时间】:2016-01-08 23:14:02
【问题描述】:

我可以成功请求令牌,并将其包含在新请求的标头中。但是,当我尝试访问受限视图时,我不断收到 AttributeError: 'WSGIRequest' object has no attribute 'successful_authenticator'以及 AttributeError: 'module' object has no attribute 'InvalidTokenError'

这是我的虚拟受限视图:

class RestrictedView(APIView):
    permission_classes = (IsAuthenticated, )
    authentication_classes = (JSONWebTokenAuthentication, )

    def post(self, request):

        response_data = json.dumps({"authenticated": "mooh"})

        return HttpResponse(response_data, content_type='application/json')

post 方法自然不会开始执行,因为身份验证有问题。

这是在 Silk profiler 中检查后传入请求的 Authorization 标头:

"AUTHORIZATION: JWT iOjE0NDQ1NjQ...."

我的 settings.py 包含以下内容:

REST_FRAMEWORK = {
    'DEFAULT_PERMISSION_CLASSES': (
        'rest_framework.permissions.IsAuthenticated',
    ),
    'DEFAULT_AUTHENTICATION_CLASSES': (
        'rest_framework.authentication.SessionAuthentication',
        'rest_framework.authentication.BasicAuthentication',
        'rest_framework_jwt.authentication.JSONWebTokenAuthentication',
    ),
    'FORM_METHOD_OVERRIDE': None,
    'FORM_CONTENT_OVERRIDE': None,
    'FORM_CONTENTTYPE_OVERRIDE': None
}

我很感激任何帮助,因为我已经被这个看似基本的问题困住了一段时间。错误的真正含义是什么?

编辑:原来的问题解决了,回答如下。犯了一个非常愚蠢的错误。

【问题讨论】:

    标签: python django django-rest-framework jwt


    【解决方案1】:

    事实证明,我只需要将我的 PyJWT 安装更新到最新的 1.4.0 版本。

    【讨论】:

    • 你应该接受这个作为答案。这可能会帮助其他面临同样问题的人。
    • 我一定会的,我只需要等2天才能接受。
    • @Marlyyy 我面临同样的问题,没有让用户进入 request.user 所以你能指导我如何验证并将正确的用户对象放入请求中。
    • PyJWT 低于1.3.0 版本时出现相同错误。 1.3.0版本或更高版本会解决这个问题
    猜你喜欢
    • 2019-01-01
    • 1970-01-01
    • 2020-04-10
    • 2015-06-01
    • 2016-01-09
    • 2019-05-02
    • 2019-11-20
    • 2017-01-18
    • 2016-04-07
    相关资源
    最近更新 更多