【问题标题】:Django Rest Framework JWT "Authentication credentials were not provided."}Django Rest Framework JWT“未提供身份验证凭据。”}
【发布时间】:2018-02-25 23:23:05
【问题描述】:

我正在尝试编写一个使用 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."}

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

【问题讨论】:

  • 您用来调用视图的令牌是否无效?
  • 不,我只是在一秒钟前收到它,当我对令牌验证端点运行请求时它也显示为有效
  • @JoSauderGH 你曾经解决过这个问题吗?我面临同样的问题,我不知道出路。

标签: python django authentication jwt


【解决方案1】:

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

WSGIPassAuthorization On

这对我有用。

【讨论】:

    猜你喜欢
    • 2019-02-19
    • 2015-05-16
    • 2019-04-29
    • 2015-01-10
    • 2015-02-05
    • 2021-06-15
    • 2022-11-10
    • 2019-07-19
    • 1970-01-01
    相关资源
    最近更新 更多