知识回顾http://www.cnblogs.com/ctztake/p/8419059.html

这一篇是基于上一篇写的,上一篇谢了认证的具体流程,看懂了上一篇这一篇才能看懂,

当用户访问是 首先执行dispatch函数,当执行当第二部时:

   #2.处理版本信息 处理认证信息 处理权限信息 对用户的访问频率进行限制
self.initial(request, *args, **kwargs)

进入到initial方法:

 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.
#2.1处理版本信息
version, scheme = self.determine_version(request, *args, **kwargs)
request.version, request.versioning_scheme = version, scheme # Ensure that the incoming request is permitted
#2.2处理认证信息
self.perform_authentication(request)
#2.3处理权限信息
self.check_permissions(request)
#2.4对用户的访问频率进行限制
self.check_throttles(request)
 #2.3处理权限信息
self.check_permissions(request)

下面 开始 权限的具体分析:

进入到check_permissions函数中

 #检查权限
def check_permissions(self, request):
"""
Check if the request should be permitted.
Raises an appropriate exception if the request is not permitted.
"""
#elf.get_permissions()得到的是一个权限对象列表
for permission in self.get_permissions():
#在自定义的Permission中has_permission方法是必须要有的
#判断当前has_permission返回的是True,False,还是抛出异常
#如果是True则表示权限通过,False执行下面代码
if not permission.has_permission(request, self):
#为False的话则抛出异常,当然这个异常返回的提示信息是英文的,如果我们想让他显示我们自定义的提示信息
#我们重写permission_denied方法即可
self.permission_denied(
#从自定义的Permission类中获取message(权限错误提示信息),一般自定义的话都建议写上,如果没有则为默认的(英文提示)
request, message=getattr(permission, 'message', None)
)

查看permission_denied方法(如果has_permission返回True则不执行该方法)

 def permission_denied(self, request, message=None):
"""
If request is not permitted, determine what kind of exception to raise.
"""
if request.authenticators and not request.successful_authenticator:
#没有登录提示的错误信息
raise exceptions.NotAuthenticated()
#一般是登陆了但是没有权限提示
raise exceptions.PermissionDenied(detail=message)

举例:

from django.db import models

# Create your models here.
class Userinfo(models.Model):
name=models.CharField(max_length=32,verbose_name='用户名')
pwd=models.CharField(max_length=32,verbose_name='密码')
token=models.CharField(max_length=64,null=True) def __str__(self):
return self.name

models

urlpatterns = [
url(r'^p/', app02_views.Pview.as_view()),
url(r'^mp/', app02_views.Aview.as_view()),
url(r'^jp/', app02_views.Jview.as_view()),
]

urls

from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework.authentication import BaseAuthentication
from rest_framework.permissions import BasePermission
from rest_framework.response import Response
from rest_framework import exceptions from app01 import models
# Create your views here. class MyAuthentication(BaseAuthentication): def authenticate(self, request):
token=request.query_params.get('token')
user=models.Userinfo.objects.filter(token=token).first()
if user:
return (user.name,user)
return None class MyPermission(object):
message = '登录才可以访问'
def has_permission(self,request, view):
if request.user:
return True
return False class AdminPermission(object):
message = '会员才可以访问'
def has_permission(self,request,view):
if request.user=='ctz':
return True
return False class Pview(APIView):
'''
所有人都可以看
'''
authentication_classes = [MyAuthentication,]
permission_classes = []
def get(self,request):
return Response('图片列表')
def post(self,request):
pass class Aview(APIView):
'''
登录的人可以看
'''
authentication_classes = [MyAuthentication,]
permission_classes = [MyPermission,]
def get(self,request):
return Response('美国电影列表')
def post(self,request):
pass def permission_denied(self, request, message=None):
"""
If request is not permitted, determine what kind of exception to raise.
""" if request.authenticators and not request.successful_authenticator:
raise exceptions.NotAuthenticated('无权访问')
raise exceptions.PermissionDenied(detail=message) class Jview(APIView):
'''
会员才可以看
'''
authentication_classes = [MyAuthentication,]
permission_classes = [MyPermission,AdminPermission,]
def get(self,request):
return Response('日本电影列表')
def post(self,request):
pass def permission_denied(self, request, message=None):
"""
If request is not permitted, determine what kind of exception to raise.
""" if request.authenticators and not request.successful_authenticator:
raise exceptions.NotAuthenticated('无权访问')
raise exceptions.PermissionDenied(detail=message)

Views

上面的是局部的只能在当前类中可以用,如果想在全局中用:只需要在settings中配置即可:

from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework.authentication import BaseAuthentication
from rest_framework.permissions import BasePermission
from rest_framework.response import Response
from rest_framework import exceptions from app02.utils import MyPermission
#
#
from app01 import models
# # Create your views here.
#
# class MyAuthentication(BaseAuthentication):
#
# def authenticate(self, request):
# token=request.query_params.get('token')
# user=models.Userinfo.objects.filter(token=token).first()
# if user:
# return (user.name,user)
# return None
#
# class MyPermission(object):
# message = '登录才可以访问'
# def has_permission(self,request, view):
# if request.user:
# return True
# return False
#
# class AdminPermission(object):
# message = '会员才可以访问'
# def has_permission(self,request,view):
# if request.user=='ctz':
# return True
# return False class Pview(APIView):
'''
所有人都可以看
''' permission_classes = []
def get(self,request):
return Response('图片列表')
def post(self,request):
pass class Aview(APIView):
'''
登录的人可以看
'''
# authentication_classes = [MyAuthentication,]
permission_classes = [MyPermission,]
def get(self,request):
return Response('美国电影列表')
def post(self,request):
pass def permission_denied(self, request, message=None):
"""
If request is not permitted, determine what kind of exception to raise.
""" if request.authenticators and not request.successful_authenticator:
raise exceptions.NotAuthenticated('无权访问')
raise exceptions.PermissionDenied(detail=message) class Jview(APIView):
'''
会员才可以看
'''
# authentication_classes = [MyAuthentication,]
# permission_classes = [MyPermission,AdminPermission,]
def get(self,request):
return Response('日本电影列表')
def post(self,request):
pass def permission_denied(self, request, message=None):
"""
If request is not permitted, determine what kind of exception to raise.
""" if request.authenticators and not request.successful_authenticator:
raise exceptions.NotAuthenticated('无权访问')
raise exceptions.PermissionDenied(detail=message)

Views

from django.shortcuts import render

from rest_framework.authentication import BaseAuthentication
from rest_framework.permissions import BasePermission
from rest_framework.response import Response
from rest_framework import exceptions
from rest_framework.exceptions import APIException from app01 import models
# Create your views here. class MyAuthentication(BaseAuthentication): def authenticate(self, request):
token=request.query_params.get('token')
user=models.Userinfo.objects.filter(token=token).first()
if user:
return (user.name,user)
return None class MyPermission(object):
message = '登录才可以访问'
def has_permission(self,request, view):
if request.user:
return True
return False class AdminPermission(object):
message = '会员才可以访问'
def has_permission(self,request,view):
if request.user=='ctz':
return True
return False

utils

REST_FRAMEWORK = {
'UNAUTHENTICATED_USER': None,
'UNAUTHENTICATED_TOKEN': None,
"DEFAULT_AUTHENTICATION_CLASSES": [
# "app01.utils.MyAuthentication",
"app02.utils.MyAuthentication",
],
"DEFAULT_PERMISSION_CLASSES":[
"app02.utils.MyPermission",
"app02.utils.AdminPermission",
],
}

settings

Django rest framework 权限操作(源码分析)的更多相关文章

  1. Django REST framework —— 权限组件源码分析

    在上一篇文章中我们已经分析了认证组件源码,我们再来看看权限组件的源码,权限组件相对容易,因为只需要返回True 和False即可 代码 class ShoppingCarView(ViewSetMix ...

  2. Django REST framework —— 认证组件源码分析

    我在前面的博客里已经讲过了,我们一般编写API的时候用的方式 class CoursesView(ViewSetMixin,APIView): pass 这种方式的有点是,灵活性比较大,可以根据自己的 ...

  3. Django rest framework框架——APIview源码分析

    一.什么是rest REST其实是一种组织Web服务的架构,而并不是我们想象的那样是实现Web服务的一种新的技术,更没有要求一定要使用HTTP.其目标是为了创建具有良好扩展性的分布式系统. 可用一句话 ...

  4. DRF框架(一)——restful接口规范、基于规范下使用原生django接口查询和增加、原生Django CBV请求生命周期源码分析、drf请求生命周期源码分析、请求模块request、渲染模块render

    DRF框架    全称:django-rest framework 知识点 1.接口:什么是接口.restful接口规范 2.CBV生命周期源码 - 基于restful规范下的CBV接口 3.请求组件 ...

  5. ElasticSearch Index操作源码分析

    ElasticSearch Index操作源码分析 本文记录ElasticSearch创建索引执行源码流程.从执行流程角度看一下创建索引会涉及到哪些服务(比如AllocationService.Mas ...

  6. Django的settings文件部分源码分析

    Django的settings文件部分源码分析 在编写Django项目的过程中, 其中一个非常强大的功能就是我们可以在settings文件配置许多选项来完成我们预期的功能, 并且这些配置还必须大写, ...

  7. Django(60)Django内置User模型源码分析及自定义User

    前言 Django为我们提供了内置的User模型,不需要我们再额外定义用户模型,建立用户体系了.它的完整的路径是在django.contrib.auth.models.User. User模型源码分析 ...

  8. django的RestFramework模块的源码分析

    一.APIView源码分析 查看源码的前提要知道,找函数方法必须先在自己的类中找,没有再往父类找,一层一层网上找,不能直接按ctrl点击 在我们自己定义的类中没有as_view方法的函数,所以肯定是继 ...

  9. Django——基于类的视图源码分析 二

    源码分析 抽象类和常用视图(base.py) 这个文件包含视图的顶级抽象类(View),基于模板的工具类(TemplateResponseMixin),模板视图(TemplateView)和重定向视图 ...

随机推荐

  1. Python基础教程系列目录,最全的Python入门系列教程!

    Python是一个高层次的结合了解释性.编译性.互动性和面向对象的脚本语言. 在现在的工作及开发当中,Python的使用越来越广泛,为了方便大家的学习,Linux大学 特推出了 <Python基 ...

  2. [转]matlab中squeeze函数的用法,numel的用法

    squeeze的作用是移除单一维. 如果矩阵哪一个维数是1,B=squeeze(A)就将这个维数移除. 考虑2-by-1-by-3 数组Y = rand(2,1,3). 这个数组有单一维 —就是每页仅 ...

  3. BZOJ 1854 游戏(二分图匹配或并查集)

    此题的二分图匹配做法很容易想,就是把属性当做s集,武器当做t集,如果该武器拥有该武器则连一条边. 那么答案就是求该二分图的最大前i个匹配.将匈牙利算法改一改,当前找不到增广路就break. 但是过这个 ...

  4. P3074 [USACO13FEB]牛奶调度Milk Scheduling

    题目描述 Farmer John's N cows (1 <= N <= 10,000) are conveniently numbered 1..N. Each cow i takes ...

  5. 如何正确实现Page接口分页,用PageImpl 自定义分页

    /** * Constructor of {@code PageImpl}. * * @param content the content of this page, must not be {@li ...

  6. 【刷题】BZOJ 5154 [Tjoi2014]匹配

    Description 有N个单身的男孩和N个单身女孩,男孩i和女孩j在一起得到的幸福值为Hij.一个匹配即对这N个男孩女孩的安排: 每个男孩恰好有一个女朋友,每个女孩恰好有一个男朋友.一个匹配的幸福 ...

  7. BZOJ5011 & 洛谷4065 & LOJ2275:[JXOI2017]颜色——题解

    https://www.lydsy.com/JudgeOnline/problem.php?id=5011 https://www.luogu.org/problemnew/show/P4065 ht ...

  8. BZOJ4566:[HAOI2016]找相同字符——题解

    https://www.lydsy.com/JudgeOnline/problem.php?id=4566 https://www.luogu.org/problemnew/show/P3181 给定 ...

  9. 洛谷3258:[USACO2012 MAR]Flowerpot 花盆——题解

    https://www.luogu.org/problemnew/show/P2698#sub 老板需要你帮忙浇花.给出N滴水的坐标,y表示水滴的高度,x表示它下落到x轴的位置. 每滴水以每秒1个单位 ...

  10. Linux实验二

    一        第一个实验 Linux基础 1 通过娄老师关于分析学霸学渣的前言 明白了真正的学习一门功课应该是思考本质 而不是纯属记忆 2 全部的命令如下 Linux命令格式:command [o ...