python - Django Rest 框架 JWT "Authentication credentials were not provided."}

标签 python django authentication jwt

我正在尝试编写一个使用 JWT token 进行身份验证的简单 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
}

我的 View 是这样的:

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 token 提供程序。当我调用此端点时,我会收到一个 token 。

但是,当我随后对我的 APIView 发出请求(其中包含 header Authorization: JWT <my_token> 时,我总是收到以下错误:

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

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

最佳答案

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

WSGIPassAuthorization On

这对我有用。

关于python - Django Rest 框架 JWT "Authentication credentials were not provided."},我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46262074/

相关文章:

django verbose_name 不工作

php - 如何使用另一个 API 的用户群在 Laravel 中设置身份验证?

python - 将 JSON 数据从 pandas 转换为 python 中的特定 JSON 模式/格式

python - 如何从 tkinter 中的绑定(bind)回调中获取回调引用?

Python在django应用程序中的所有模块之间共享数据

sql - 我可以避免在不必要的时间内锁定行吗 [在 Django 中]?

php - 网站登录系统

c# - 在 Asp.net Web 服务中集成 JWT

python - 如何在Python中解析带有括号的电子邮件FROM header ?

python - 如何在Python中压缩文件?