【发布时间】:2020-04-10 01:01:48
【问题描述】:
我正在使用rest_framework_simplejwt 对我的用户进行身份验证,但是在某些视图中我需要忽略它,因为这些是公共视图。我想将令牌检查到视图流中。预期的行为是:
在公众视野中
- 避免令牌验证:如果有过期或无效的令牌,忽略它,让我在 APIView 中验证它
其实rest_framework_simplejwt会检查token,如果token无效或过期则提升401...
我尝试在 APIView 中禁用 authentication_classes,如下所示:
class SpecificProductApi(APIView):
def get_authenticators(self):
if self.request.method == 'GET':
self.authentication_classes = []
return super(SpecificProductApi, self).get_authenticators()
但是如果我在输入 GET APIView 方法之前禁用它,我不能这样做 if reques.user.is_authenticated: 因为我禁用了令牌:(
是否存在一种方法可以进入 api http 方法并手动检查用户进入视图?谢谢
【问题讨论】:
-
我不明白您这样做时遇到了什么问题
request.user.is_authenticated。你能解释一下问题吗? -
您好!感谢您的回答,碰巧如果我使用
self.authentication_classes = [],稍后在使用request.user.is_authenticated的视图内部无效,它总是返回false,我需要在视图内部手动处理身份验证避免在其外部进行验证,我希望更清楚, 非常感谢:) -
我想知道执行此操作的标准方法是什么。像一个规范来处理这个。有人知道吗?
标签: django python-3.x django-rest-framework jwt-auth django-rest-framework-simplejwt