Django REST framework - 权限和限制
Django REST framework 权限和限制
(你能干什么)
与身份验证和限制一起,权限确定是应该授予还是拒绝访问请求。
在允许任何其他代码继续之前,权限检查始终在视图的最开始运行。权限检查通常使用 request.user 和 request.auth 属性中的身份验证信息来确定是否应允许传入请求。
权限用于授予或拒绝不同类别的用户访问API的不同部分。
最简单的权限类型是允许访问任何经过身份验证的用户,并拒绝访问任何未经身份验证的用户。这对应IsAuthenticated于REST框架中的类。
稍微不那么严格的权限样式是允许对经过身份验证的用户进行完全访问,但允许对未经身份验证的用户进行只读访问。这对应IsAuthenticatedOrReadOnly于REST框架中的类。
设置权限的方法
可以使用该
DEFAULT_PERMISSION_CLASSES设置全局设置默认权限策略。例如:REST_FRAMEWORK = {
'DEFAULT_PERMISSION_CLASSES': (
'rest_framework.permissions.IsAuthenticated',
)
}
如果未指定,则此设置默认允许不受限制的访问:
'DEFAULT_PERMISSION_CLASSES': (
'rest_framework.permissions.AllowAny',
)
您还可以使用
APIView基于类的视图在每个视图或每个视图集的基础上设置身份验证策略。from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from rest_framework.views import APIViewclass ExampleView(APIView):
permission_classes = (IsAuthenticated,)def get(self, request, format=None):
content = {
'status': 'request was permitted'
}
return Response(content)
或者,使用
@api_view具有基于功能的视图的装饰器。from rest_framework.decorators import api_view, permission_classes
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response @api_view(['GET'])
@permission_classes((IsAuthenticated, ))
def example_view(request, format=None):
content = {
'status': 'request was permitted'
}
return Response(content)
注意 :当您通过类属性或装饰器设置新的权限类时,您告诉视图忽略settings.py文件中的默认列表集。
如果它们继承自rest_framework.permissions.BasePermission,则可以使用标准Python按位运算符组合权限。例如,IsAuthenticatedOrReadOnly可以写成:
from rest_framework.permissions import BasePermission, IsAuthenticated, SAFE_METHODS
from rest_framework.response import Response
from rest_framework.views import APIView
class ReadOnly(BasePermission):
def has_permission(self, request, view):
return request.method in SAFE_METHODS
class ExampleView(APIView):
permission_classes = (IsAuthenticated|ReadOnly,)
def get(self, request, format=None):
content = {
'status': 'request was permitted'
}
return Response(content)
案例
此案例基于 Django REST framework 认证
第一步: 定义一个权限类
"""
自己动手写一个权限组件
"""
from rest_framework.permissions import BasePermission
class MyPermission(BasePermission):
message = '只有VIP才能访问'
def has_permission(self, request, view):
# 认证类中返回了token_obj.user, request_token
# request.auth 等价于request_token
if not request.auth:
return False
# request.user为当前用户对象
if request.user and request.user.type == 1: # 如果是VIP用户
print("requ", request.user, type(request.user))
return True
else:
return False
第二步: 使用
视图级别
class CommentViewSet(ModelViewSet):
queryset = models.Comment.objects.all()
serializer_class = app01_serializers.CommentSerializer
authentication_classes = [MyAuth, ]
permission_classes = [MyPermission, ]
全局级别设置
# 在settings.py中设置rest framework相关配置项
REST_FRAMEWORK = {
"DEFAULT_AUTHENTICATION_CLASSES": ["app01.utils.MyAuth", ],
"DEFAULT_PERMISSION_CLASSES": ["app01.utils.MyPermission", ]
}
限制
(你一分钟能干多少次?)**好像有点污~~
第一步: 自定义限制类
import time
# from rest_framework.throttling import
visit_record = {}
class MyThrottle(object):
def __init__(self):
self.history = None
def allow_request(self, request, view):
# 拿到当前的请求的ip作为访问记录的 key
ip = request.META.get('REMOTE_ADDR')
# 拿到当前请求的时间戳
now = time.time()
if ip not in visit_record:
visit_record[ip] = []
# 把当前请求的访问记录拿出来保存到一个变量中
history = visit_record[ip]
self.history = history
# 循环访问历史,把超过10秒钟的请求时间去掉
while history and now - history[-1] > 10:
history.pop()
# 此时 history中只保存了最近10秒钟的访问记录
if len(history) >= 3:
return False
else:
# 判断之前有没有访问记录(第一次来)
self.history.insert(0, now)
return True
def wait(self):
"""告诉客户端还需等待多久"""
now = time.time()
return self.history[-1] + 10 - now
# history = ['9:56:12', '9:56:10', '9:56:09', '9:56:08'] # '9:56:18' - '9:56:12'
# history = ['9:56:19', '9:56:18', '9:56:17', '9:56:08']
# 最后一项到期的时间就是下一次允许请求的时间
# 最后一项到期的时间:now - history[-1] > 10
# 最后一项还剩多少时间过期
# history[-1] + 10 - now
第二步: 使用
视图中使用
class CommentViewSet(ModelViewSet):
queryset = models.Comment.objects.all()
serializer_class = app01_serializers.CommentSerializer
throttle_classes = [MyThrottle, ]
全局中使用
# 在settings.py中设置rest framework相关配置项
REST_FRAMEWORK = {
"DEFAULT_AUTHENTICATION_CLASSES": ["app01.utils.MyAuth", ],
"DEFAULT_PERMISSION_CLASSES": ["app01.utils.MyPermission", ]
"DEFAULT_THROTTLE_CLASSES": ["app01.utils.MyThrottle", ]
}
嫌麻烦的话,还可以使用内置限制类,哈哈~
from rest_framework.throttling import SimpleRateThrottle
class VisitThrottle(SimpleRateThrottle):
scope = "xxx"
def get_cache_key(self, request, view):
return self.get_ident(request)
全局配置
# 在settings.py中设置rest framework相关配置项
REST_FRAMEWORK = {
"DEFAULT_AUTHENTICATION_CLASSES": ["app01.utils.MyAuth", ],
# "DEFAULT_PERMISSION_CLASSES": ["app01.utils.MyPermission", ]
"DEFAULT_THROTTLE_CLASSES": ["app01.utils.VisitThrottle", ],
"DEFAULT_THROTTLE_RATES": {
"xxx": "5/m",
}
}
Django REST framework - 权限和限制的更多相关文章
- Django rest framework ---- 权限
Django rest framework ---- 权限 添加权限 api/utils文件夹下新建premission.py文件,代码如下: message是当没有权限时,提示的信息 # FileN ...
- django rest framework权限和认证
Django rest framework之权限 一.Authentication用户认证配置 1.四种验证及官网描述: BasicAuthentication 此身份验证方案使用HTTP基本身份验证 ...
- Django rest framework 权限操作(源码分析)
知识回顾http://www.cnblogs.com/ctztake/p/8419059.html 这一篇是基于上一篇写的,上一篇谢了认证的具体流程,看懂了上一篇这一篇才能看懂, 当用户访问是 首先执 ...
- Django REST framework —— 权限组件源码分析
在上一篇文章中我们已经分析了认证组件源码,我们再来看看权限组件的源码,权限组件相对容易,因为只需要返回True 和False即可 代码 class ShoppingCarView(ViewSetMix ...
- Django rest framework源码分析(2)----权限
目录 Django rest framework(1)----认证 Django rest framework(2)----权限 Django rest framework(3)----节流 Djan ...
- 04 Django REST Framework 认证、权限和限制
目前,我们的API对谁可以编辑或删除代码段没有任何限制.我们希望有更高级的行为,以确保: 代码片段始终与创建者相关联. 只有通过身份验证的用户可以创建片段. 只有代码片段的创建者可以更新或删除它. 未 ...
- Django Rest Framework(认证、权限、限制访问频率)
阅读原文Django Rest Framework(认证.权限.限制访问频率) django_rest_framework doc django_redis cache doc
- Django Rest framework 之 权限
django rest framework 之 认证(一) django rest framework 之 权限(二) django rest framework 之 节流(三) django res ...
- Django Rest Framework源码剖析(二)-----权限
一.简介 在上一篇博客中已经介绍了django rest framework 对于认证的源码流程,以及实现过程,当用户经过认证之后下一步就是涉及到权限的问题.比如订单的业务只能VIP才能查看,所以这时 ...
随机推荐
- 【总结】嵌入式linux内核中Makefile、Kconfig、.config的关系及增加开机Hello World【转】
本文转载自:http://blog.csdn.net/fengyuwuzu0519/article/details/73772109 为了弄清内核的组织结构,我们先来实现下面这个简单的例子. 一.增加 ...
- system.web section group下的section
private Configuration _configuration; private ConfigurationSectionGroupCollection sectionGroups; pri ...
- bzoj3527
http://www.lydsy.com/JudgeOnline/problem.php?id=3527 今天肿么这么颓废啊...心态崩了 首先我们得出Ei=Fi/qj,然后我们设f[i]=1/i/i ...
- 46. Ext中namespace的作用(转)
转自:https://www.cnblogs.com/givemeanorange/p/5569954.html Ext中在每一个页面中添加一个namespace呢,就像下面的代码: // creat ...
- Linux系统的整体目录结构和文件解析
Linux系统目录结构 使用 ls / 查看系统的文件目录: /:根目录,根目录下一般只存放子目录,不存放文件.在linux系统中所有的文件都挂载该目录下. /bin:命令目录. 存放系统的可执行的二 ...
- Codeforces Round #239 (Div. 1)
B. Long Path time limit per test 1 second memory limit per test 256 megabytes input standard input o ...
- [Swift通天遁地]四、网络和线程-(1)线程的锁和解锁
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- HDU 1754 Java
退役后学java... 裸线段树 //By SiriusRen import java.util.*; import java.math.*; public class Main{ public st ...
- 简单 android popupwindow 实现
1. popupWindow 设置大小: popupWindow = new PopupWindow(view, ViewGroup.LayoutParams.FILL_PARENT, ViewGr ...
- mysql子查询与连接查询
表结构以及数据: CREATE TABLE `student` ( `id` ) NOT NULL AUTO_INCREMENT, `name` ) CHARACTER SET utf8 COLLAT ...