当前位置: 首页 > ai >正文

rest_framework学习之认证 权限

权限

DRF提供如下几种常见权限:

IsAuthenticated, 认证通过

IsAdminUser, 管理员权限

IsAuthenticatedOrReadOnly, 登录用户增删改 非登录用户只能查询

AllowAny,无需认证(默认)

在rest_framework的APIView基础类中,对认证与权限做了更高级的封装,如下:

class APIView(View):# The following policies may be set at either globally, or per-view.authentication_classes = api_settings.DEFAULT_AUTHENTICATION_CLASSESpermission_classes = api_settings.DEFAULT_PERMISSION_CLASSES

如果需要单独设置

from django.conf import settings
from rest_framework.authtoken.models import Token
from rest_framework.decorators import api_view,authentication_classes,permission_classes
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from rest_framework import status,generics,viewsets
#from rest_framework import permissions
from rest_framework.authentication import BasicAuthentication,SessionAuthentication,TokenAuthentication
from .models import Course
from .serializers import CourseSerializer
from rest_framework.views import APIView

fbv 方式

##函数式编程
@api_view(['GET','POST'])
@authentication_classes((BasicAuthentication,SessionAuthentication,TokenAuthentication))
@permission_classes((IsAuthenticated,))
def course_list(request):

cbv  gcbv viewsets 方式

# 类视图 Class Based View
class CourseList(APIView):authentication_classes = 
(BasicAuthentication,SessionAuthentication,TokenAuthentication)
permission_classes = ((IsAuthenticated))def get(self,request):print(self.request.user,self.request.auth)

自定义权限

 新建文件permissions.py

from rest_framework import permissionsclass IsOwnerReadOnly(permissions.BasePermission):#只允许对象的所有者能编辑def has_object_permission(self, request, view, obj):"""所有的request 都有读权限:param request::param view::param obj::return:"""#if request.method in ("GET","HEAD","OPTIONS"):if request.method in permissions.SAFE_METHODS:return True#对象的所有这才有写权限return obj.teacher == request.user   #gcbv

加入到views.py 文件

class GCourseDetail(generics.RetrieveUpdateDestroyAPIView):queryset = Course.objects.all()serializer_class = CourseSerializerpermission_classes = (IsAuthenticated,IsOwnerReadOnly)# DRF 视图集 viewsets
class CourseViewSet(viewsets.ModelViewSet):queryset = Course.objects.all()serializer_class = CourseSerializerpermission_classes = (IsAuthenticated, IsOwnerReadOnly)def perform_create(self, serializer):serializer.save(teacher= self.request.user)

http://www.xdnf.cn/news/4656.html

相关文章:

  • 【软件设计师:数据库】13.数据库控制与安全
  • vite 代理 websocket
  • 稳定性_李雅普诺夫——Lyapunov直接法
  • 网络靶场基础知识
  • 是更换Window资源管理器的时候了-> Files-community/Files
  • 涨薪技术|0到1学会性能测试第53课-Tomcat配置
  • Python中的re库详细用法与代码解析
  • 在Lua中使用轻量级userdata在C/C++之间传递数据和调用函数
  • 探讨关于智能体(Agent)结合 Dify、大语言模型(LLM)以及 Qwen-3 模型的项目或概念
  • C++-缺省参数
  • 如何在Jmeter中调用C程序?
  • 【软考-高级】【信息系统项目管理师】【论文基础】采购管理过程输入输出及工具技术的使用方法
  • 永久免费的小工具,内嵌微软接口
  • AWS LB target group 监听端口的增加 (TCP还是UDP)
  • Redis实现分布式获取全局唯一自增ID的案例。
  • Dify X 奇墨科技,让AI大模型从“巨头专属”变为“触手可及”
  • Windows系统下使用Kafka和Zookeeper,Python运行kafka(一)
  • 单片机嵌入式滤波算法库
  • 从颜料混色到网络安全:DH算法的跨界智慧
  • Java实现桶排序算法
  • 【Git】【commit】查看未推送的提交查看指定commit的修改内容合并不连续的commit
  • 【Ubuntu】安裝向日葵远程控制
  • 可观测性方案怎么选?SelectDB vs Elasticsearch vs ClickHouse
  • [逆向工程]什么是DLL重定向(十九)
  • 基于Stable Diffusion XL模型进行文本生成图像的训练
  • 《社交应用架构生存战:React Native与Flutter的部署容灾决胜法则》
  • k8s(11) — 探针和钩子
  • SpringBoot学生操行评分系统源码设计开发
  • C++函数传值与传引用对比分析
  • 课外活动:简单了解原生测试框架Unittest前置后置的逻辑