【发布时间】: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