【发布时间】:2020-05-19 21:47:55
【问题描述】:
Angular 8 和 Django 3。我使用自定义视图类 CustomAuthToken 从 django 获得 user_id 和 token。我收到包含token 和user_id 的回复。我正在关注一个教程,并想测试向 django 发送自定义标头以查看授权是否有效。
我正在使用 Postman 并将标题设置为 Authorization: Bearer 1a683...9e428。当我向http://127.0.0.1:8000/users/dashboard/23 发送 GET 请求时,我得到了响应
"detail": "Authentication credentials were not provided."
我的理解是提供这些标题应该有效吗?还是在客户端有什么东西可以解码标头并将其发回?
views.py
class CustomAuthToken(ObtainAuthToken):
def post(self, request, *args, **kwargs):
serializer = self.serializer_class(data=request.data,
context={'request': request})
serializer.is_valid(raise_exception=True)
user = serializer.validated_data['user']
token, created = Token.objects.get_or_create(user=user)
return Response({
'token': token.key,
'user_id': user.pk,
})
settings.py
REST_FRAMEWORK = {
'DEFAULT_PERMISSION_CLASSES': (
'rest_framework.permissions.IsAuthenticatedOrReadOnly',
),
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework_jwt.authentication.JSONWebTokenAuthentication',
'rest_framework.authentication.SessionAuthentication',
'rest_framework.authentication.BasicAuthentication',
'rest_framework.authentication.TokenAuthentication',
),
}
urls.py
urlpatterns = [
path('dashboard/<int:pk>', UserDashboard.as_view(), name='dashboard'),
path(r'api-token-auth/', CustomAuthToken.as_view()),
]
【问题讨论】:
标签: django angular jwt jwt-auth