我正在通过扩展 rest_framework.views.APIView
类来构建 Django API View 。
我已经成功构建了许多只能由经过身份验证的用户调用的 API。我通过添加以下内容来完成此操作: permission_classes = [permissions.IsAuthenticated,]
有一些 API 我只希望未经身份验证的用户调用。例如“忘记密码”。基本上,我想确保 API 调用者不会在请求 header 中发送 JWT token 。我怎样才能强制执行呢?没有permissions.IsUnAuthenticated
。
最佳答案
您可以轻松创建自己的 IsNotAuthenticated
类
像这样:
from rest_framework.permissions import BasePermission
class IsNotAuthenticated(BasePermission):
"""
Allows access only to non authenticated users.
"""
def has_permission(self, request, view):
return not request.user.is_authenticated()
然后:permission_classes = (myapp.permissions.IsNotAuthenticated,)
问候。
关于django - 如何使 Django View 仅可供未经身份验证的用户访问?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26107525/