为了方便接下来的学习,咱们建立一个新的子应用 fourpython
python manage.py startapp four
由于接下来的功能中须要使用到登录功能,因此咱们使用django内置admin站点并建立一个管理员.django
python manage.py createsuperuser

建立管理员之后,访问admin站点,先修改站点的语言配置服务器
settings.pysession
访问admin 站点效果:app
rest_framework.settings里面有默认的全局配置,不过咱们能够在本身项目的setting.py文件中进行配置覆盖学习
能够在配置文件中(项目中的setting.py)配置全局默认的认证方案spa
REST_FRAMEWORK = { 'DEFAULT_AUTHENTICATION_CLASSES': ( 'rest_framework.authentication.SessionAuthentication', # session认证 'rest_framework.authentication.BasicAuthentication', # 基本认证 ) }
也能够在每一个视图中经过设置authentication_classess属性来设置(局部)rest
from rest_framework.authentication import SessionAuthentication, BasicAuthentication from rest_framework.views import APIView class ExampleView(APIView): # 类属性 authentication_classes = [SessionAuthentication, BasicAuthentication] ...
认证失败会有两种可能的返回值:code
401 Unauthorized 未认证对象
403 Permission Denied 权限被禁止
权限控制能够限制用户对于视图的访问和对于具体数据对象的访问。
在执行视图的dispatch()方法前,会先进行视图访问权限的判断
在经过get_object()获取具体对象时,会进行模型对象访问权限的判断
能够在配置文件(项目中的setting.py)中全局设置默认的权限管理类,如
REST_FRAMEWORK = { .... 'DEFAULT_PERMISSION_CLASSES': ( 'rest_framework.permissions.IsAuthenticated', ) }
若是未指明,则采用以下(rest_framework.settings)默认配置
'DEFAULT_PERMISSION_CLASSES': ( 'rest_framework.permissions.AllowAny', )
也能够在具体的视图中经过permission_classes属性来设置(局部),如
from rest_framework.permissions import IsAuthenticated from rest_framework.views import APIView class ExampleView(APIView): permission_classes = (IsAuthenticated,) ...
AllowAny 容许全部用户
IsAuthenticated 仅经过认证的用户
IsAdminUser 仅管理员用户
IsAuthenticatedOrReadOnly 已经登录认证的用户能够对数据进行增删改操做,没有登录认证的只能查看数据。
from rest_framework.response import Response from rest_framework.views import APIView from rest_framework.authentication import SessionAuthentication from rest_framework.permissions import AllowAny,IsAdminUser,IsAuthenticated class ExampleAPIView(APIView): authentication_classes = [SessionAuthentication] permission_classes = [IsAuthenticated] def get(self,request): print( type( request.user ) ) return Response({"message":"ok"})
如需自定义权限,需继承rest_framework.permissions.BasePermission父类,并实现如下两个任何一个方法或所有
.has_permission(self, request, view)
是否能够访问视图, view表示当前视图对象
.has_object_permission(self, request, view, obj)
是否能够访问数据对象, view表示当前视图, obj为数据对象
例如:
在当前子应用下,建立一个权限文件permissions.py中声明自定义权限类:
from rest_framework.permissions import BasePermission class IsXiaoMingPermission(BasePermission): def has_permission(self, request, view): if( request.user.username == "xiaoming" ): return True
from rest_framework.response import Response from rest_framework.views import APIView from rest_framework.authentication import SessionAuthentication from rest_framework.permissions import AllowAny,IsAdminUser,IsAuthenticated from .permissions import IsXiaoMingAuthentication class ExampleAPIView(APIView): authentication_classes = [SessionAuthentication] permission_classes = [IsXiaoMingAuthentication] def get(self,request): print( type( request.user ) ) return Response({"message":"ok"})
能够对接口访问的频次进行限制,以减轻服务器压力。
通常用于付费购买次数,投票等场景使用.
能够在配置文件中,使用DEFAULT_THROTTLE_CLASSES
和 DEFAULT_THROTTLE_RATES
进行全局配置,
REST_FRAMEWORK = { # 限流[全局] 'DEFAULT_THROTTLE_CLASSES': ( 'rest_framework.throttling.AnonRateThrottle', # 匿名用户,游客 'rest_framework.throttling.UserRateThrottle' # 已经通过认证的用户 ), 'DEFAULT_THROTTLE_RATES': { 'anon': '300/minute', 'user': '1000/minute' }, }
DEFAULT_THROTTLE_RATES
可使用 second
, minute
, hour
或day
来指明周期。
也能够在具体视图中经过throttle_classess属性来配置,如
from rest_framework.throttling import UserRateThrottle from rest_framework.views import APIView class ExampleView(APIView): throttle_classes = (UserRateThrottle,) ...
1) AnonRateThrottle
限制全部匿名未认证用户,使用IP区分用户。
使用DEFAULT_THROTTLE_RATES['anon']
来设置频次
2)UserRateThrottle
限制认证用户,使用User id 来区分。
使用DEFAULT_THROTTLE_RATES['user']
来设置频次
3)ScopedRateThrottle
限制用户对于每一个视图的访问频次,使用ip或user id。
例如:
class ContactListView(APIView): throttle_scope = 'contacts' ... class ContactDetailView(APIView): throttle_scope = 'contacts' ... class UploadView(APIView): throttle_scope = 'uploads' ... REST_FRAMEWORK = { 'DEFAULT_THROTTLE_CLASSES': ( 'rest_framework.throttling.ScopedRateThrottle', ), 'DEFAULT_THROTTLE_RATES': { 'contacts': '1000/day', 'uploads': '20/day' } }
全局配置中设置访问频率
'DEFAULT_THROTTLE_RATES': { 'anon': '3/minute', 'user': '10/minute' } from rest_framework.authentication import SessionAuthentication from rest_framework.permissions import IsAuthenticated from rest_framework.generics import RetrieveAPIView from rest_framework.throttling import UserRateThrottle class StudentAPIView(RetrieveAPIView): queryset = Student.objects.all() serializer_class = StudentSerializer authentication_classes = [SessionAuthentication] permission_classes = [IsAuthenticated] throttle_classes = (UserRateThrottle,)