关于permission-class, def perform_authentication(self, request) 和authentication_class之间的关系

--------authentication_classes:

代码如下:

class APIView(View):

    # The following policies may be set at either globally, or per-view.
    renderer_classes = api_settings.DEFAULT_RENDERER_CLASSES
    parser_classes = api_settings.DEFAULT_PARSER_CLASSES
    authentication_classes = api_settings.DEFAULT_AUTHENTICATION_CLASSES
    throttle_classes = api_settings.DEFAULT_THROTTLE_CLASSES
    permission_classes = api_settings.DEFAULT_PERMISSION_CLASSES
    content_negotiation_class = api_settings.DEFAULT_CONTENT_NEGOTIATION_CLASS
    metadata_class = api_settings.DEFAULT_METADATA_CLASS
    versioning_class = api_settings.DEFAULT_VERSIONING_CLASS

作用是:指定何种方式进行用户身份的认证



---------def  perform_authentication(self, request) :

“”“重写父类的验证方法”“”

    pass

它是具体认证的逻辑代码,用request.user来取值,如果不为None,说明验证通过

class CartView(APIView):
    """购物车后端的增删改查"""

    def perform_authentication(self, request):
        """
        重写父类的验证方法,不在检查JWT
        保证用户未登录的情况下也能进行下面的请求方法
        不让他执行request.user的方法
        """
        # request.user
        pass

    def get(self, request):
        """查询"""
        # 需求是:根据判断用户是否登陆,进行不同的操作,判断用户是否登陆
        try:

------------permission-class

class AddressViewSet(mixins.CreateModelMixin, mixins.UpdateModelMixin, GenericViewSet):
    """
    用户地址新增与修改
    """
    serializer_class = serializers.UserAddressSerializer
    permissions = [IsAuthenticated]

    def get_queryset(self):
        return self.request.user.addresses.filter(is_deleted=False)

    def list(self, request, *args, **kwargs):
        """
        用户地址列表数据
        """

permissions = [IsAuthenticated, IsAdminUser, ]

表示认证到什么程度, 前提是必须登录了,才可以使用该方法

比如:

IsAuthenticated:用户

IsAdminUser:特权用户





  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值