Django Rest Framework JWT"未提供身份验证凭据。"}

时间:2017-09-17 08:21:59

标签: python django authentication jwt

我正在尝试编写一个使用JWT令牌进行身份验证的简单API。

我的settings.py

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'rest_framework',
]
REST_FRAMEWORK = {
    'DEFAULT_PERMISSION_CLASSES': (
        'rest_framework.permissions.IsAuthenticated',
        'rest_framework.permissions.IsAdminUser'
    ),
    'DEFAULT_AUTHENTICATION_CLASSES': (
        'rest_framework_jwt.authentication.JSONWebTokenAuthentication',
        'rest_framework.authentication.SessionAuthentication',
        'rest_framework.authentication.BasicAuthentication',
    ),
   'PAGE_SIZE': 10
}

我的视图看起来像这样:

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

    def get(self, request, format=None):
        return JsonResponse({"username":str(request.user)})

哪个什么都没做,但我甚至都没有到达那里。我在url(r'^api-token-auth/', obtain_jwt_token)的文档中注册了JWT令牌提供程序,并在调用此端点时收到令牌。

然而,当我对我的APIView(包含标题Authorization: JWT <my_token>发出请求时,我总是收到以下错误:

[Response]: 403 [Data]: {"detail":"Authentication credentials were not provided."}

我错过了什么?提前谢谢!

1 个答案:

答案 0 :(得分:1)

如果您使用apache作为Web服务器,请在apache httpd.conf文件中添加以下内容:

WSGIPassAuthorization On

这对我有用。

相关问题