• 开发工程师必备————【Day37】Django补充(九)


    今日内容概要

    • csrf跨站请求伪造
    • CBV添加装饰器的多种方式
    • auth认证模块
    • 基于django中间件设计项目功能

    csrf跨站请求伪造

    1.简介
    钓鱼网站:假设是一个跟银行一模一样的网址页面 用户在该页面上转账
    账户的钱会减少 但是受益人却不是自己想要转账的那个人
    2.模拟
    一台计算机上两个服务端不同端口启动 钓鱼网站提交地址改为正规网站的地址
    3.预防
    csrf策略:通过在返回的页面上添加独一无二的标识信息从而区分正规网站和钓鱼网站的请求

    csrf操作

    一共四种方法:form表单一种,ajax三种!!!
    1.form表单

    	<form action="" method="post">
       	{% csrf_token %}
    	</form>
    
    • 1
    • 2
    • 3

    2.ajax

    • 方式1:先编写csrf模板语法 然后利用标签查找和值获取 手动添加
    'csrfmiddlewaretoken':$('[name="csrfmiddlewaretoken"]').val()
    
    • 1
    • 方式2:直接利用模板语法即可
    'csrfmiddlewaretoken':'{{ csrf_token }}'
    
    • 1
    • 方式3:通用方式(js脚本)
      只需要引入js文件即可。
      扩展性最高

    csrf相关装饰器

    1.当整个网站默认都不校验csrf 但是局部视图函数需要校验 如何处理
    2.当整个网站默认都校验csrf 但是局部视图函数不需要校验 如何处理

    -----------------------------FBV------------------------------
    from django.views.decorators.csrf import csrf_protect,csrf_exempt
    """
    csrf_protect 校验csrf
    csrf_exempt  不校验csrf
    """
    # @csrf_protect
    @csrf_exempt
    def home(request):
       return HttpResponse('哈哈哈')
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    -----------------------------CBV-----------------------------
    针对CBV不能直接在方法上添加装饰器 需要借助于专门添加装饰器的方法
    需要借助模块:from django.utils.decorators import method_decorator
    
    # @method_decorator(csrf_protect, name='post')  # 方式2:指名道姓的添加
    class MyHome(views.View):
       @method_decorator(csrf_protect)  # 方式3:影响类中所有的方法
       def dispatch(self, request, *args, **kwargs):
           super(MyHome, self).dispatch(request, *args, **kwargs)
    
       def get(self, request):
           return HttpResponse('Home Get view')
    
       # @method_decorator(csrf_protect)  # 方式1:指名道姓的添加
       def post(self, request):
           return HttpResponse('Home Post view')
    
    
    针对csrf_exempt只有方式3有效 针对其他装饰器上述三种方式都有效
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    auth认证模块

    1.django执行数据库迁移命令之后会产生一个auth_user表

    • 该表可以配合auth模块做用户相关的功能:注册 登录 修改密码 注销 …
    • 该表还是django admin后台管理默认的表

    ==2.django admin后台管理员账号创建 ==

    python manage.py createsuperuser
    
    • 1

    auth模块常见功能
    1.创建用户
    from django.contrib.auth.models import User
    User.object.create_user(username,password)
    User.object.create_superuser(username,password,email) v
    2.校验用户名和密码是否正确
    from django.contrib import auth
    auth.authenticate(request,username,password)
    3.用户登录
    auth.login(request,user_obj)
    4.判断用户是否登录
    request.user.is_authecticated
    5.获取登录用户对象
    request.user
    6.校验用户登录装饰器
    from django.contrib.auth.decorators import login_required
    跳转局部配置
    login_required(login_url=‘/login/’)
    跳转全局配置
    LOGIN_URL = ‘/login/’
    7.校验密码是否正确
    request.user.check_password(old_password)
    8.修改密码
    request.user.set_password(new_passowrd)
    request.user.save()
    9.注销登录
    auth.logout(request)

    auth_user表切换

    具体步骤:
    1.models.py

    from django.contrib.auth.models import AbstractUser
       class Userinfo(AbstractUser):
           '''扩展auth_user表中没有的字段'''
           phone = models.BigIntegerField()
           desc = models.TextField()
    
    • 1
    • 2
    • 3
    • 4
    • 5

    2.settings.py
    AUTH_USER_MODEL = ‘app01.Userinfo’

    基于django中间件设计项目功能

    模块的导入
    import 句式
    from … import … 句式

    1.简单的函数式封装
    2.配置文件插拔式设计
    请添加图片描述

  • 相关阅读:
    LeetCode 283. 移动零
    跟着CTF-wiki学pwn——ret2shellcode
    RISV-V架构的寄存器介绍
    三天快速搭建一个属于自己的管理系统,包括前端、后端、部署上线,超级详细的介绍。
    在Win11上部署ChatGLM2-6B详细步骤--(下)开始部署
    【使用Cpolar和Qchan搭建自己的个人图床】
    一次服务器被入侵的处理过程分享
    尚硅谷CSS学习笔记
    Python:记录python安装 cv2报错及问题解决
    DSP2335的LED工程笔记
  • 原文地址:https://blog.csdn.net/DiligentGG/article/details/126845201