【发布时间】:2019-11-19 09:32:41
【问题描述】:
我目前有带有 Knox 令牌身份验证的 Django 基本身份验证设置。 Basic Auth 似乎不足以用于生产工作,所以我想替换它。 Django 是否有另一个基于密码的authentication_class,我可以轻松地将BasicAuthentication 替换为,或者这是一个更复杂的过程?如果是这样,我从哪里开始?
我的登录 api 视图:
class UserLoginView(GenericAPIView):
serializer_class = UserOrganizationSerializer
authentication_classes = (BasicAuthentication,)
permission_classes = (IsAuthenticated,)
def post(self, request):
"""User login with username and password."""
token = AuthToken.objects.create(request.user)
return Response({
'user': self.get_serializer(request.user).data,
'token': token
})
我的默认身份验证类:
REST_FRAMEWORK = {
'DEFAULT_PERMISSION_CLASSES': [],
'DEFAULT_AUTHENTICATION_CLASSES': [
'rest_framework.authentication.SessionAuthentication',
'rest_framework.authentication.BasicAuthentication',
],
【问题讨论】:
-
在您看来,您拥有
authentication_classes = (BasicAuthentication,)。这意味着 DRF 将忽略默认类列表。如果您想启用其他方法,请从您的视图中删除该行(首选),或者以与 BasicAuthentication 相同的方式将其他方法添加到列表中。 -
另外,您提到了
knox,但您的示例中似乎没有使用它。
标签: django authentication django-rest-framework