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

快速入手-基于Django-rest-framework的自身组件权限认证(九)

1、在对应的视图函数里增加认证(局部起作用,不全局生效)

导入类:

from rest_framework.authentication import (
    BasicAuthentication,
    SessionAuthentication,
)
from rest_framework.permissions import IsAuthenticated, AllowAny

2、基于第八节内容增加以下权限设置内容

#定义认证类型

authentication_classes = [SessionAuthentication]

#定义权限限制

permission_classes = [IsAuthenticated]

备注说明:

AllowAny  允许所有用户,默认权限

IsAuthenticated  仅通过登录认证的用户

IsAdminUser 仅管理员用户

IsAuthenticatedOrReadOnly  已经登录认证的用户可以对数据进行CRUD操作,

                                                  没有登录认证的用户,只能查看数据

3、完整code

from django.shortcuts import render, HttpResponse
from rest_framework.response import Response
from rest_framework.decorators import action

from rest_framework.viewsets import GenericViewSet
from rest_framework.mixins import (
    ListModelMixin,
    CreateModelMixin,
    RetrieveModelMixin,
    UpdateModelMixin,
    DestroyModelMixin,
)
from rest_framework.viewsets import ModelViewSet
from rest_framework import serializers

from rest_framework.authentication import (
    BasicAuthentication,
    SessionAuthentication,
)
from rest_framework.permissions import IsAuthenticated, AllowAny


from .models import *
from api.serializer import *


# 这种写法实现所有的增删改查,不能够单独进行操作
# class Linkapi(ModelViewSet):
# 不仅可以实现所有的增删改查,而且可以单独也可以全部包含增删改查
class Linkapi(
    GenericViewSet,
    ListModelMixin,
    CreateModelMixin,
    RetrieveModelMixin,
    UpdateModelMixin,
    DestroyModelMixin,
):

    queryset = Link.objects.all()
    serializer_class = LinkSerializer
    authentication_classes = [SessionAuthentication]
    permission_classes = [IsAuthenticated]

    # 在原有的二级路由中自定义添加三级路由路径
    # 访问路径/api/linkapi/{pk}/login/
    @action(
        methods=["get", "POST"],
        detail=True,
        url_path="login",
    )
    def login(self, request, pk):
        queryset = self.get_queryset()
        serializer = self.get_serializer(queryset, many=True)
        return Response(serializer.data)

    # detail为false表示路径名格式应该为/api/linkapi/get_new_5/
    @action(
        methods=[
            "get",
        ],
        detail=False,
    )
    def get_new_2(self, request):
        obj = Link.objects.all().filter()[:2]
        serializer = self.get_serializer(instance=obj, many=True)
        return Response(serializer.data)

4、测试

5、仅允许查询,其他方式请求未授权不能访问

导入包

from rest_framework.permissions import (

    IsAuthenticated,

    AllowAny,

    IsAuthenticatedOrReadOnly,

)

修改视图类内容

# IsAuthenticated 授权登录后可以访问

# IsAuthenticatedOrReadOnly  只允许查询

permission_classes = [IsAuthenticatedOrReadOnly]

6、 

相关文章:

  • 49. 字母异位词分组
  • 【微服务架构】SpringCloud Alibaba(二):Nacos作为配置中心(Config数据模型、集成SpringBoot、动态刷新原理)
  • 2025 全球音乐 AI 大模型对决:技术革新引领商业变革浪潮
  • 在MVC框架声明视图使用 【UserAuthorize】和【Authorize】及不使用任何修饰的区别?使用场景?优缺点?
  • 【Easylive】JDBC 连接池(Connection Pool)是什么意思?
  • 【大模型】DeepSeek-R1-Distill-Qwen使用Langchain网页部署
  • 【QA】QT有哪些享元模式的设计?
  • 全文 MLIR TOY -- Chapter2: 发出基本的 MLIR——把AST变成SSA的 MLIR Dialect IR
  • 问题大集09-如何实现vite创建的react项目的配置别名路径@
  • mapreduce的工作原理
  • 【AI深度学习网络】DeepSeek的Transformer改进与优化技术:从自注意力机制到多模态突破
  • 【分布式】深入剖析 Sentinel 限流:原理、实现
  • 语音克隆(Voice Cloning)
  • vue 组件之间传递参数
  • 齐次线性方程组及python求解
  • 主成分分析(PCA)学习介绍及其高阶应用,金融风险分析
  • Sentinel实战(二)、流控规则之流控阈值类型、流控模式
  • 重建二叉树(C++)
  • Pycharm(八):字符串切片
  • python数据结构——基础、顺序表
  • 介绍个人网站的ppt怎么做/自媒体十大平台
  • 贵州新闻网站网络推广/济南疫情最新消息
  • 福州网站建站建设/百度一下首页登录入口
  • 手机网站设置方法/优化关键词的公司
  • 网站流量平台/如何进行网络营销策划
  • 网站设计论文html/如何去除痘痘有效果