Django REST framework基礎:認證、權限、限制

Django REST framework基礎:認證、權限、限制

 

 

認證、權限和限制

身份驗證是將傳入請求與一組標識憑據(例如請求來自的用戶或其簽名的令牌)相關聯的機制。而後 權限 和 限制 組件決定是否拒絕這個請求。html

簡單來講就是:app

認證肯定了你是誰dom

權限肯定你能不能訪問某個接口ide

限制肯定你訪問某個接口的頻率post

認證

REST framework 提供了一些開箱即用的身份驗證方案,而且還容許你實現自定義方案。this

 

接下類咱們就本身動手實現一個基於Token的認證方案:url

自定義Token認證

定義一個用戶表和一個保存用戶Token的表:spa

複製代碼
class UserInfo(models.Model):
    username = models.CharField(max_length=16)
    password = models.CharField(max_length=32)
    type = models.SmallIntegerField(
        choices=((1, '普通用戶'), (2, 'VIP用戶')),
        default=1
    )


class Token(models.Model):
    user = models.OneToOneField(to='UserInfo')
    token_code = models.CharField(max_length=128)
複製代碼

定義一個登陸視圖:

複製代碼
def get_random_token(username):
    """
    根據用戶名和時間戳生成隨機token
    :param username:
    :return:
    """
    import hashlib, time
    timestamp = str(time.time())
    m = hashlib.md5(bytes(username, encoding="utf8"))
    m.update(bytes(timestamp, encoding="utf8"))
    return m.hexdigest()


class LoginView(APIView):
    """
    校驗用戶名密碼是否正確從而生成token的視圖
    """
    def post(self, request):
        res = {"code": 0}
        print(request.data)
        username = request.data.get("username")
        password = request.data.get("password")

        user = models.UserInfo.objects.filter(username=username, password=password).first()
        if user:
            # 若是用戶名密碼正確
            token = get_random_token(username)
            models.Token.objects.update_or_create(defaults={"token_code": token}, user=user)
            res["token"] = token
        else:
            res["code"] = 1
            res["error"] = "用戶名或密碼錯誤"
        return Response(res)
複製代碼

定義一個認證類

複製代碼
from rest_framework.authentication import BaseAuthentication
from rest_framework.exceptions import AuthenticationFailed


class MyAuth(BaseAuthentication):
    def authenticate(self, request):
        if request.method in ["POST", "PUT", "DELETE"]:
            request_token = request.data.get("token", None)
            if not request_token:
                raise AuthenticationFailed('缺乏token')
            token_obj = models.Token.objects.filter(token_code=request_token).first()
            if not token_obj:
                raise AuthenticationFailed('無效的token')
            return token_obj.user.username, None
        else:
            return None, None
複製代碼

視圖級別認證

class CommentViewSet(ModelViewSet):

    queryset = models.Comment.objects.all()
    serializer_class = app01_serializers.CommentSerializer
    authentication_classes = [MyAuth, ]

全局級別認證

# 在settings.py中配置
REST_FRAMEWORK = {
    "DEFAULT_AUTHENTICATION_CLASSES": ["app01.utils.MyAuth", ]
}

權限

只有VIP用戶才能看的內容。rest

自定義一個權限類

複製代碼
# 自定義權限
class MyPermission(BasePermission):
    message = 'VIP用戶才能訪問'

    def has_permission(self, request, view):
        """
        自定義權限只有VIP用戶才能訪問
        """
        # 由於在進行權限判斷以前已經作了認證判斷,因此這裏能夠直接拿到request.user
        if request.user and request.user.type == 2:  # 若是是VIP用戶
            return True
        else:
            return False
複製代碼

視圖級別配置

class CommentViewSet(ModelViewSet):

    queryset = models.Comment.objects.all()
    serializer_class = app01_serializers.CommentSerializer
    authentication_classes = [MyAuth, ]
    permission_classes = [MyPermission, ]

全局級別設置

# 在settings.py中設置rest framework相關配置項
REST_FRAMEWORK = {
    "DEFAULT_AUTHENTICATION_CLASSES": ["app01.utils.MyAuth", ],
    "DEFAULT_PERMISSION_CLASSES": ["app01.utils.MyPermission", ]
}

限制

DRF內置了基本的限制類,首先咱們本身動手寫一個限制類,熟悉下限制組件的執行過程。code

自定義限制類

複製代碼
VISIT_RECORD = {}
# 自定義限制
class MyThrottle(object):

    def __init__(self):
        self.history = None

    def allow_request(self, request, view):
        """
        自定義頻率限制60秒內只能訪問三次
        """
        # 獲取用戶IP
        ip = request.META.get("REMOTE_ADDR")
        timestamp = time.time()
        if ip not in VISIT_RECORD:
            VISIT_RECORD[ip] = [timestamp, ]
            return True
        history = VISIT_RECORD[ip]
        self.history = history
        history.insert(0, timestamp)
        while history and history[-1] < timestamp - 60:
            history.pop()
        if len(history) > 3:
            return False
        else:
            return True

    def wait(self):
        """
        限制時間還剩多少
        """
        timestamp = time.time()
        return 60 - (timestamp - self.history[-1])
複製代碼

 

視圖使用

class CommentViewSet(ModelViewSet):

    queryset = models.Comment.objects.all()
    serializer_class = app01_serializers.CommentSerializer
    throttle_classes = [MyThrottle, ]

全局使用

# 在settings.py中設置rest framework相關配置項
REST_FRAMEWORK = {
    "DEFAULT_AUTHENTICATION_CLASSES": ["app01.utils.MyAuth", ],
    "DEFAULT_PERMISSION_CLASSES": ["app01.utils.MyPermission", ]
    "DEFAULT_THROTTLE_CLASSES": ["app01.utils.MyThrottle", ]
}

使用內置限制類

複製代碼
from rest_framework.throttling import SimpleRateThrottle


class VisitThrottle(SimpleRateThrottle):

    scope = "xxx"

    def get_cache_key(self, request, view):
        return self.get_ident(request)
複製代碼

全局配置

複製代碼
# 在settings.py中設置rest framework相關配置項
REST_FRAMEWORK = {
    "DEFAULT_AUTHENTICATION_CLASSES": ["app01.utils.MyAuth", ],
    # "DEFAULT_PERMISSION_CLASSES": ["app01.utils.MyPermission", ]
    "DEFAULT_THROTTLE_CLASSES": ["app01.utils.VisitThrottle", ],
    "DEFAULT_THROTTLE_RATES": {
        "xxx": "5/m",
    }
}
複製代碼
相關文章
相關標籤/搜索