zoukankan      html  css  js  c++  java
  • Django rest_framework----认证,权限,频率组件

    认证

    from rest_framework.authentication import BaseAuthentication
    from rest_framework.exceptions import AuthenticationFailed
    from api.models import *
    
    class AuthToken(BaseAuthentication):
        def authenticate(self, request):
            token=request.GET.get('token')
            token_obj=Token.objects.filter(token=token)
    
            if token_obj:
    
                return token_obj.user,token_obj
    
            else:
    
                raise AuthenticationFailed('验证失败')
    
            
    全局使用settings配置

    REST_FRAMEWORK={ "DEFAULT_AUTHENTICATION_CLASSES":["app01.service.auth.Authentication",] }
    局部使用,只需要在视图类里加入:
    
    authentication_classes = [TokenAuth, ]


    权限

    class SVIPPermission(object):
      message="只有超级用户才能访问"
      def has_permission(self,request,view):
        username=request.user
        user_type=User.objects.filter(name=username).first().user_type
    
        if user_type==3:
    
          return True # 通过权限认证
        else:
          return False
  • 相关阅读:
    Codeforces Round #456 (Div. 2)
    Codeforces Round #455 (Div. 2)
    Codeforces Round #453 (Div. 1)
    Codeforces Round #450 (Div. 2)
    退役了
    退役了
    这个博客不想要了
    Hello!The familiar and strange world.
    真正的退役了。
    bzoj4231: 回忆树
  • 原文地址:https://www.cnblogs.com/hanbowen/p/9904230.html
Copyright © 2011-2022 走看看