在views中 from rest_framework.views import APIView from rest_framework.viewsets import ViewSetMixin import hashlib import time from app01 import models from rest_framework.response import Response from django.http import JsonResponse class Login(ViewSetMixin,APIView): def get_token(self): h6=hashlib.md5() ctime=time.time() h6.update(bytes(str(ctime),encoding='utf-8')) token=h6.hexdigest() return token def ulogin(self,request): response={'status':100,'msg':'登錄成功'} name=request.data.get('name') pwd=request.data.get('pwd') user=models.UserInfo.objects.all().filter(name=name,pwd=pwd).first() if user: ret=models.UserToken.objects.all().create(token=self.get_token(),user=user)#登錄成功就去數(shù)據(jù)庫中寫token response['token']=self.get_token() else: response['status']=101 response['msg']='用戶名或密碼錯誤' return Response(response) class Author(ViewSetMixin,APIView): #局部使用登錄認證,頻率,權(quán)限 throttle_classes = [MyThrottle, ] authentication_classes = [LoginAuth, ] permission_classes = [UserPermission,] def get_authors(self,request): author=models.Author.objects.all() ser=Myser.Authorser(author,many=True) return Response(ser.data) #頻率錯誤信息顯示 def throttled(self, request, wait): class MyThrottled(exceptions.Throttled): default_detail = '×××' extra_detail_singular = '還剩 {wait} 秒.' extra_detail_plural = '還剩 {wait} 秒' 在MyAuth中---------------------------------------------------------------------------------------------------- from app01 import models from rest_framework import exceptions class LoginAuth(): def authenticate(self, request): token = request.query_params.get('token') ret = models.UserToken.objects.all().filter(token=token).first() if ret: return ret.user, ret else: raise exceptions.APIException('認證失敗') class UserPermission(): # message是出錯顯示的中文 message = '您沒有權(quán)限查看' def has_permission(self, request, view): user_type = request.user.user_type # 取出用戶類型對應(yīng)的文字 # 固定用法:get_字段名字_display() user_type_name = request.user.get_user_type_display() print(user_type_name) if user_type == 2: return True else: return False from rest_framework.throttling import SimpleRateThrottle class MyThrottle(SimpleRateThrottle): scope = 'pinglv' def get_cache_key(self, request, view): return self.get_ident(request) 在Myser中--------------------------------------------------------------------------------- from rest_framework import serializers from app01 import models class Bookser(serializers.ModelSerializer): class Meta: model=models.Book fields='__all__' class Authorser(serializers.ModelSerializer): class Meta: model=models.Author fields='__all__' 在setting中----------------------------------------------------------------------------------------- REST_FRAMEWORK = { # 'DEFAULT_AUTHENTICATION_CLASSES':['app01.MyAuth.LoginAuth',],#認證全局使用 # 'DEFAULT_PERMISSION_CLASSES':['app01.MyAuth.UserPermission',],#權(quán)限全局使用 # 'DEFAULT_THROTTLE_CLASSES': ['app01.MyAuth.MyThrottle', ],#頻率全局使用 # 每分鐘訪問10次 'DEFAULT_THROTTLE_RATES': { 'pinglv': '10/m' }, }
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機、免備案服務(wù)器”等云主機租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。
當前名稱:rest_framework認證,頻率,權(quán)限-創(chuàng)新互聯(lián)
網(wǎng)站網(wǎng)址:http://www.rwnh.cn/article28/dsdccp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供關(guān)鍵詞優(yōu)化、移動網(wǎng)站建設(shè)、定制開發(fā)、定制網(wǎng)站、網(wǎng)站建設(shè)、網(wǎng)站內(nèi)鏈
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容