「Django」rest_framework学习系列-权限认证
权限认证:
1.项目下utils文件写permissions.py文件
from rest_framework.permissions import BasePermission class SVIPPermission(BasePermission): #用户权限类 message = 'SVIP才可以访问' #认证错误时的返回信息 def has_permission(self, request, view): ut = request.user.usertype if ut == 3: return True class VIPPermission(BasePermission): #用户权限类 message = 'VIP才可以访问' def has_permission(self, request, view): ut = request.user.usertype if ut == 2: return True class AllowAny(BasePermission): def has_permission(self, request, view): return True
2、settings配置用户全局认证如下:
'DEFAULT_PERMISSION_CLASSES':['api.utils.permissions.SVIPPermission',],#权限全局认证
3.views业务类可以在全局认证外设置单独认证规则:
permission_classes = [permissions.VIPPermission,] #权限局部认证