DRF自定义认证类

# 自定义认证类
class
Authtication(object):
  # 自定义认证方法
def authenticate(self, request):
     # 查看前端传过来的数据是否有token token
= request._request.GET.get('token')
     # 查询数据库用token匹配用户 token_obj
= models.UserToken.objects.filter(token=token).first()
     # 若没有匹配结果
if not token_obj:
        # 抛出异常
raise exceptions.AuthenticationFailed('请先登录')
     # 若有返回结果
return (token_obj.user, token_obj) def authenticate_header(self,request): pass class OrderView(APIView):
  # 调用认证类,保证若想调用该类中的方法,必先通过认证 authentication_classes
= [Authtication,] def get(self,request): pass

猜你喜欢

转载自www.cnblogs.com/hellozizi/p/11829026.html
今日推荐