zoukankan      html  css  js  c++  java
  • 三大认件

    三大认件

    一、认证组件

    1.1 系统认证

    # session认证
    rest_framework.authentication.SessionAuthentication
    # ajax请求通过验证
    # cookie携带sessionid、csrftoken
    # 请求头中携带 x-csrftoken
    

    1.2 第三方:jwt认证

    rest_framework_jwt.authentication.JSONWebTokenAuthentication
    # ajax请求通过认证
    # 请求头中要携带 authorization,值为 jwt空格token
    

    1.3 自定义

    # 基于jwt、其它
    # 自定义认证类,继承BaseAuthentication(或其子类),重写authenticate
    # authenticate中完成
    #	拿到认证标识 auth
    #	反解析出用户 user
    #	前两步操作失败 返回None => 游客
    #	前两步操作成功 返回user,auth => 登录用户
    #	注:如果在某个分支抛出异常,直接定义失败 => 非法用户
    
    from rest_framework.authenticate import BaseAuthentication
    def authenticate(self, request):
        auth = # request中获得
        user = # auth中获得
        if not user:
            return None
        return user, auth
    

    二、权限组件

    2.1 系统

    1)AllowAny:允许所有用户,校验方法直接返回True
    2)IsAuthenticated:只允许登录用户
     必须request.user和request.user.is_authenticated都通过
    3)IsAuthenticatedOrReadOnly:游客只读,登录用户无限制
     get、option、head 请求无限制
     前台请求必须校验 request.user和request.user.is_authenticated
    4)IsAdminUser:是否是后台用户
     校验 request.user和request.user.is_staff is_staff(可以登录后台管理系统的用户)
    

    2.2 自定义

    # 基于auth的Group与Permission表
    1)自定义权限类,继承BasePermission,重写has_permission
    2)has_permission中完成
     拿到登录用户 user <= request.user
     校验user的分组或是权限
     前两步操作失败 返回False => 无权限
     前两步操作成功 返回True => 有权限
    

    ​ 自定义权限类:管理员与组成员

    from rest_framework.pemissions import BasePermission
    
    clss AdminPermission(BasePermission):
        # 继承并重写BasePermission
        def has_permissoon(self, request, view):
            # True:有权限
            # False:无权限
            user = request.user
            if not user:
                return False
            if not user.groups.filter(name='管理员')
            	return False
            return True
    

    三、频率组件

    3.1 系统

    1)AnonRateThrottle:对同一IP游客的限制
    2)UserRateThrottle:对同一IP登录用户的限制
    必须在settings.py中
    'DEFAULT_THROTTLE_RATES': {
     'user': '10/min', # 登录的用户一分钟可以访问10次
     'anon': '3/min', # 游客一分钟可以访问3次
    }
    在视图类中:
    class TempAPIView(APIView):
     ...
     throttle_classes = [AnonRateThrottle, UserRateThrottle]
    

    3.2 自定义

    基于auth的Group与Permission表
    1)自定义频率类,继承SimpleRateThrottle,重写get_cache_key,明确scope
     SimpleRateThrottle已经帮我们实现了 allow_request、wait
    2)scope与settings.py的DEFAULT_THROTTLE_RATES配合使用
    3)get_cache_key中完成
     拿到限制信息 ident <= request中获取
     没有限制信息 返回None => 不限制
     有限制信息 返回限制信息字符串 => 有限制
    
    

    ​ 一个手机号一分钟只可以访问一次:

    # settings.py
    'DEFAULT_THROTTLE-RATES': {
        'user': '10/min',
        'anon': '3/min',
        'sms': '1/min',
    }
    
    
    from rest_framework.throttling import SimpleThrottle
    
    class ThreeMinRateThrottle(SimpleRateThrottle)
    	scope = 'sms'
        def get_cache_key(self, request, view):
            ident = request.data.get('mobile')
            if not ident:
                return None
            return self.cache_format % {
                'scope': self.scope,
                'ident': ident
            }
    
    
  • 相关阅读:
    17. 电话号码的字母组合
    12. 整数转罗马数字
    01-正则表达式基础
    前端SEO技巧
    node.js
    Vue.生命周期
    Vue小案例--过滤器的基本操作
    vue简单的计算器
    VSCode 自动刷新
    Vue.js学习
  • 原文地址:https://www.cnblogs.com/tangceng/p/11931207.html
Copyright © 2011-2022 走看看