【发布时间】:2021-05-07 19:28:53
【问题描述】:
我有这样的 DRF 视图:
class DocumentsView(generics.ListCreateAPIView):
permission_classes = ()
authentication_classes = ()
# I typically enforce authentication with
# authentication_classes = (JWTCookieAuthentication,)
def list(self, request):
pagination_class = None
if request.user.is_authenticated:
... return protected data ...
else:
... return generic data ...
我希望允许发送有效令牌的用户和未发送有效令牌的用户都从该端点获得响应。但是,request.user.is_authenticated 返回False,即使发送了有效的令牌(我明白为什么)。如何尝试对用户进行身份验证,但即使不提供令牌仍允许他们继续?
或者更好的做法是对经过身份验证的未经身份验证的用户不具有相同的视图?
【问题讨论】:
-
但是,request.user.is_authenticated 返回 False,即使发送了有效的令牌(我明白为什么)。 我不知道。是认证后端没有让用户登录还是认证中间件没有运行?
-
@Melvyn,因为视图中的 authentication_classes = () 覆盖了我的 DEFAULT_AUTHENTICATION_CLASSES
标签: django rest authentication django-rest-framework api-design