Django中间件

Django中间件

中间件介绍

中间件类似于Django的门户,请求来的时候和响应走的时候都必须经过它

配置文件中
MIDDLEWARE = [
	...
]

# Django 默认有七个中间件,每个中间件都有不同的功能
Django中间件里有五个需要学习的方法
	需要掌握
    	process_request
    	process_response	
    需要了解
    	process_view
    	process_exception
    	process_template_response
# Django支持用户自定义中间件
Django中间件可以用于编写全局相关的功能,例如:全局身份校验,全局防爬校验

需要掌握的方法

from django.utils.deprecation import MiddlewareMixin
class MyMiddleware1(MiddlewareMixin):
    def process_request(self,request):
        print('from 自定义MyMiddleware1 process_request方法')
        # 请求来的时候回依次(从上往下)执行配置文件中注册了的中间件里的process_request方法,如果没有该方法则直接跳过
        # 该方法如果设置了返回HTTPResponse对象,那么请求不在继续往下,而是原路返回,返回时时从同级别的process_response开始返回,而不是所有的中间件的process_response,而且不同框架情况可能不一样
        
    def process_response(self,request,response):
        print('from 自定义MyMiddleware1 process_response')
        # 响应走的时候 从下往上 依次执行配置文件中注册了的中间件里的process_response方法,如果没有改方法则跳过
        # 该方法需要将形参response返回,该response其实就是试图函数返回给浏览器的数据
        # 该方法可以拦截返回给浏览器的数据,并且支持修改返回内容

只需了解的方法

process_view
	# 路由匹配成功之后执行视图函数之前自动触发
	def process_view(self,request,view_name,*args,**kwargs):

process_exception
	# 当视图函数报错之后自动执行
    def process_exception(self,request,exception):
   
process_template_response
	def process_template_response(self,request,response):
	# 视图函数返回的对象有一个render()方法(或者表明该对象是一个TemplateResponse对象或等价方法)

csrf跨站请求伪造

典型案例:钓鱼网站

理论:

  • 做一个与正规网站一模一样的界面,让用户在上面操作,比如转账。
  • 转账请求确实是发送给了正规的网站后台,只不过收款人变成了钓鱼网站的指定用户

原理:

  • 获取用户输入的表单内容,给用户展示的是一个没有name属性的输入框,自己偷偷写了一个既有name属性又有value属性的隐藏框,那么传给后台的不再是用户输入的

csrf使用

1.form表单
	表单中直接书写下列语句即可
     {% csrf_token %}
        
2.ajax
	方式一:借助于{% csrf_token %}生成的标签(不推荐)
    data:{'csrfmiddlewaretoken':$("[name = 'csrfmiddlewaretoken']").val()}
        
    方式二:借助于模板语法直接获取
    data:{'csrfmiddlewaretoken':'{{ csrf_token }}'}
        
    方式三:借助于官方提供的js文件自动获取
    js文件导入即可  官方网址:https://docs.djangoproject.com/en/1.11/ref/csrf/

csrf相关装饰器

from django.views.decorators.csrf import csrf_exempt, csrf_protect
# csrf_exempt   局部不校验csrf
# csrf_protect  局部校验csrf

# @csrf_protect
# @csrf_exempt
def index(request):
    if request.method == 'POST':
        username = request.POST.get('username')
        money = request.POST.get('money')
        target_user = request.POST.get('target_user')
        print('%s 给 %s 转了 %s 元钱'%(username,target_user,money))
    return render(request,'index.html')

from django.utils.decorators import method_decorator

from django import views
# @method_decorator(csrf_protect,name='post')  # 第二种  行
# @method_decorator(csrf_exempt,name='post')  # 第二种  不行
class MyLogin(views.View):
    # @method_decorator(csrf_protect)  # 第三种 所有的方法都会加上该功能
    @method_decorator(csrf_exempt)  # 第三种 所有的方法都会加上该功能
    def dispatch(self, request, *args, **kwargs):
        return super().dispatch(request,*args,**kwargs)

    def get(self,request):
        return HttpResponse('from get')

    # @method_decorator(csrf_protect)  # 第一种  行
    # @method_decorator(csrf_exempt)  # 第一种  不行
    def post(self,request):
        return HttpResponse('from post')
 
"""
csrf_exempt该装饰器在CBV中只能给dispatch装才能生效
"""

posted @ 2021-06-30 20:44  zheng-sn  阅读(28)  评论(0)    收藏  举报