Django:rest framework -----APIView

REST即表述性状态传递(英文:Representational State Transfer,简称REST)是Roy Fielding博士在2000年他的博士论文中提出来的一种软件架构风格。它是一种针对网络应用的设计和开发方式,可以降低开发的复杂性,提高系统的可伸缩性。

1、Django生命周期:

① Django的生命周期是:

前端请求---->nginx---->uwsgi---->中间件---->路由系统---->视图---->ORM去数据库取数,拿到数据返回给视图---->视图将数据渲染到模版中(模板字符串)---->中间件---->uwsgi---->nginx---->前端渲染成实际网页页面。

② Django rest framework的生命周期是:

前端请求---->nginx---->uwsgi---->中间件---->版本解析and渲染器---->路由系统,执行CBV的as_view(),实质执行了其dispatch方法---->执行dispatch时,将request进行封装---->版本确认---->认证---->限流---->视图---->【如果视图使用缓存request.data或request.query_params时,解析器登场】---->视图取数进行序列化操作(对数据进行序列化或验证)---->视图将数据渲染到模版中(模板字符串),有分页需求就进行分页操作---->中间件---->uwsgi---->nginx---->前端渲染成实际网页页面。

APIView

 

权限校验是在执行请求方法之前 dispatch ,APIView内重写了dispatch函数 重点在于 self.initial(request, *args, **kwargs)函数

    def initial(self, request, *args, **kwargs):
        """
        Runs anything that needs to occur prior to calling the method handler.
        """
        self.format_kwarg = self.get_format_suffix(**kwargs)

        # Perform content negotiation and store the accepted info on the request
        neg = self.perform_content_negotiation(request)
        request.accepted_renderer, request.accepted_media_type = neg

        # Determine the API version, if versioning is in use.
        version, scheme = self.determine_version(request, *args, **kwargs)
        request.version, request.versioning_scheme = version, scheme

        # Ensure that the incoming request is permitted
        self.perform_authentication(request)
        self.check_permissions(request)
        self.check_throttles(request)
perform_authentication函数内依赖 authentication_classes校验
authentication_classes = [SessionAuthentication, JSONWebTokenAuthentication]

对于像BasicAuthentication这样的类,必须实现authenticate方法,并且返回一个用户,赋值给request.user,这个request.user就是系统中进行用户认证的user对象,后续的权限验证一般都是通过判断request.user的user对象是否拥有某个权限。rest-framework默认的就是BasicAuthentication,也就是跟admin登陆用的一样的认证。

自定义校验:继承BaseAuthentication   重写authenticate函数,其中 

重点关注 authenticate_credentials方法内掉用user = authenticate(request=request, **credentials)

内部有一条user = backend.authenticate(request, **credentials)

backend就是 settings.AUTHENTICATION_BACKENDS中配置的路径,

# 自定义用户认证配置
AUTHENTICATION_BACKENDS = [
    'users.views.CustomBackend',
]
CustomBackend是自定义的认证类 
from django.contrib.auth.backends import ModelBackend
class CustomBackend(ModelBackend):
    '''自定义用户验证'''

    def authenticate(self, request, username=None, password=None, **kwargs):
        try:
            user = User.objects.get(Q(username=username) | Q(mobile=username))
            if user.check_password(password) and user.is_delete != True:
                return user
        except Exception as e:
            return None

基于rest-framework对django的RESTful API进行权限设置 | 码农家园

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值