from rest_framework.views import APIView
from rest_framework import exceptions
from rest_framework.throttling import BaseThrottle
# 认证
class MyAuthentication(BaseAuthentication):
# 重写父类的该方法实现认证
def authenticate(self, request):
token = request.GET.get('token')
# 获取用户名和密码去数据库校验
if not token:
raise exceptions.AuthenticationFailed('用户认证失败')
return ("用户对象", None)
def authenticate_header(self, val):
pass
class UserView(APIView):
# 认证的类对象列表
authentication_classes = [MyAuthentication, ]
# 权限控制的类对象列表
permission_classes = [MyBasePermission, ]
# 频率限制的类对象列表
throttle_classes = [VisitThrottle, ]
def get(self, request, *args, **kwargs):
data = {'code': 1000, 'msg': 'xxx'}
return HttpResponse(json.dumps(data), status=201)
django-rest-framework认证
最新推荐文章于 2024-05-16 12:56:17 发布