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