小编Jho*_*anz的帖子

Django Rest Framework 避免身份验证 JWT

我正在使用rest_framework_simplejwt对我的用户进行身份验证,但是在某些视图中我需要忽略它,因为这些是公共视图。我想将令牌检查到视图流中。预期的行为是:

在公众视野中

  • 避免令牌验证:如果有过期或无效的令牌,请忽略它并让我在 APIView 中验证它

实际上rest_framework_simplejwt检查令牌并401在令牌无效或过期时引发......

我尝试authentication_classes在 APIView 中像这样禁用:

class SpecificProductApi(APIView):

    def get_authenticators(self):
        if self.request.method == 'GET':
            self.authentication_classes = []
        return super(SpecificProductApi, self).get_authenticators()
Run Code Online (Sandbox Code Playgroud)

但是如果我在输入GET APIView方法之前禁用它,我不能这样做,if reques.user.is_authenticated:因为我禁用了令牌:(

是否存在启用进入 api http 方法并手动检查用户查看的方法?谢谢

django python-3.x django-rest-framework jwt-auth django-rest-framework-simplejwt

4
推荐指数
3
解决办法
3298
查看次数