$Django 中间件 csrf
-中间件是什么?请求和响应之间的一道屏障
-中间件作用:控制请求和响应
-django中内置几个中间件
process_request(self,request)
process_view(self, request, callback, callback_args, callback_kwargs)
process_template_response(self,request,response)
process_exception(self, request, exception)
process_response(self, request, response)
我是 request2
我是 执行视图函数之前 1
我是 执行视图函数之前 2
我是视图函数
我是 template 2
我是 template 1
我是 异常2
我是 异常1
我是 response2
我是 response1



-自定义中间件
-1.from django.utils.deprecation import MiddlewareMixin
class MyMiddleware1(MiddlewareMixin):
def process_request(self, request):
print('MyMiddleware---->1---->process_request')
# 返回HttpRspons对象,直接返回,走自己的process_response
# 返回None的时候,继续往下走
# return HttpResponse('i am middle--1')
return None
def process_response(self, request, response):
print('MyMiddleware--->1---->process_response')
return response #这里必须返回HTTPRESPONSE对象 对视图函数的返回值response的一个操作,可以丢弃等等操作
-2.在setting中注册,是有顺序的,
MIDDLEWARE = [
'app01.mymiddelware.MyMiddleware1',
]
-中间件执行顺序:
-process_request,从上往下执行
-如果retrun HttpResponse的对象,直接返回了
-如果retrun None ,继续往下走
-process_response,从下往上执行
-必须要retrun Httpresponse的对象
csrf:跨站请求伪造
比如:转账请求:transfer?to=lqz&count=1000
-是什么?攻击者盗用了你的身份,以你的名义发送恶意请求,对服务器来说这个请求是完全合法的
-如何防范:
-通过refer
-加一个随机字符串校验(加载请求的路径里,加载请求体中)
-在请求头中加字符串校验
django中的应用:
-中间件csrf不注释掉
-以后再发post请求,必须携带那个随机字符串到服务端
-form表单形式:
<form action="" method="post">
{% csrf_token %}
<input type="text" name="name">
<input type="text" name="pwd">
<input type="submit" value="提交">
</form>
-ajax提交
data: {
'name': $('[name="name"]').val(),
'pwd': $('[name="pwd"]').val(),
//'csrfmiddlewaretoken': $('[name="csrfmiddlewaretoken"]').val()
'csrfmiddlewaretoken': '{{ csrf_token }}'
},
csrf:局部禁用,局部使用
-用装饰器:
-局部禁用,全局得使用
@csrf_exempt
def csrf_disable(request):
print(request.POST)
return HttpResponse('ok')
-局部使用,全局得禁用
@csrf_protect
def csrf_disable(request):
print(request.POST)
return HttpResponse('ok')
-cbv-->只能加在dispatch方法或者类上面
-局部禁用,全局得使用
-局部使用,全局得禁用
from django.views import View
from django.utils.decorators import method_decorator
#####################################################
@method_decorator(csrf_protect,name='dispatch')
class Csrf_disable(View):
# @method_decorator(csrf_protect)
def dispatch(self, request, *args, **kwargs):
ret=super().dispatch(request, *args, **kwargs)
return ret
def get(self,request):
return HttpResponse('ok')
return HttpResponse('post---ok')
-先导入:
-1 可以在方法上加装饰器:
@method_decorator(login_auth)
-2 可以在类上加
@method_decorator(login_auth,name='post')
@method_decorator(login_auth,name='get')
-3 可以加在dishpatch方法上
@method_decorator(login_auth)
一旦加在dishpatch,说明,所有方法都加了装饰器
from django.utils.deprecation import MiddlewareMixin
from django.utils.decorators import method_decorator
from django.shortcuts import render,HttpResponse class MyMiddleware(MiddlewareMixin):
def process_request(self,request):
print('我是 request1')
# return HttpResponse(1) # 我是 request0 我是 response0
def process_view(self, request, callback, callback_args, callback_kwargs):
print ("我是 执行视图函数之前 1")
#有异常才执行该函数,每异常不管他,必须返回Httpresponse对象(有异常 显示我的信息)
def process_exception(self, request, exception):
print ("我是 异常1")
return HttpResponse('出错了兄弟1')
# 方法对视图函数返回值有要求,必须是一个含有render方法类的对象,才会执行此方法
# 1.视图函数 必须返回 一个类(有render方法 返回Httprespons对象)
# 2.这里要返回一个 Httpresponse对象
def process_template_response(self,request,response):
print('我是 template 1')
return HttpResponse('我是 template1')
def process_response(self,request,response):
print('我是 response1')
return response
class MyMiddleware1(MiddlewareMixin):
def process_request(self,request):
print('我是 request2')
def process_view(self, request, callback, callback_args, callback_kwargs):
print ("我是 执行视图函数之前 2")
def process_exception(self, request, exception):
print ("我是 异常2")
def process_template_response(self, request, response):
print ('我是 template 2')
return HttpResponse ('我是 template2')
def process_response(self, request, response):
print ('我是 response2')
return response ###视图函数
from django.shortcuts import render,HttpResponse # Create your views here.
class GG():
def render(self):
return HttpResponse('template')
def aaa(request):
if request.method=='GET':
print('我是视图函数get')
return GG()
elif request.method=='POST':
print(request.POST,type(request.POST),'我是视图函数post')
return HttpResponse('ok')
随机推荐
- bzoj千题计划309:bzoj4332: JSOI2012 分零食(分治+FFT)
https://www.lydsy.com/JudgeOnline/problem.php?id=4332 因为如果一位小朋友得不到糖果,那么在她身后的小朋友们也都得不到糖果. 所以设g[i][j] ...
- MySQL数据库基本命令-1
第一章:数据库概述1.数据(data) 数据库(DB) 数据库管理系统(DBMS) 数据库系统(DBS)2.数据库管理系统提供的功能: (1)数据定义语言:DDL (2)数据操作语言:DML 基本的数 ...
- Ubuntu18.04中安装cuda的记录
一.参考: https://blog.csdn.net/QLULIBIN/article/details/78714596 https://www.jianshu.com/p/00c37b09f0f3 ...
- ubantu中安装TensorFlow遇到的问题
因为Ubuntu中装有python3.python2两种python环境,在装TensorFlow时需要根据版本进行适配 1.安装pip3 #在python2下安装pip sudo apt-get i ...
- Linux/Windows双系统引导修复
安装双系统建议先安装windows,然后在安装Linux,使用Linux(grub2)引导双系统 如果重新安装了windows,则无法引导进入linux,需要修复引导 在windows下安装easyB ...
- 登录mysql时的一些命令
清屏: cls 只要输入cls然后回车运行命令,可以清屏. 查看版本号: mysql -V 注意:V一定要大写,如果不大写就错了... 登录: mysql -u用户名 -p密码 -P端口号 -h数据库 ...
- 将数据以json字符串格式传到前台请求页面
1.前台ajax方法(这个是在FlowDocAction的add.jsp页面) //序列号 var preFileNo = factoryCode+deptCode+"-"+mod ...
- LOJ#2249 Luogu P2305「NOI2014」购票
几乎肝了半个下午和整个晚上 斜率优化的模型好多啊... LOJ #2249 Luogu P2305 题意 给定一棵树,第$ i$个点如果离某个祖先$ x$的距离不超过$ L_i$,可以花费$ P_i· ...
- timestamp的妙用
代码 -- 卖家(登录后台使用, 卖家登录之后可能直接采用微信扫码登录,不使用账号密码) create table `seller_info` ( `id` varchar(32) not null, ...
- 【提示框】【计时事件】【cookie】
1.提示框 1)警告框 <script>function disp_alert(){alert("我是警告框!!")}</script> 2)确认框 fun ...