目录
一、简介
二、应用
三、官方示例
一、简介
django为用户实现防止跨站请求伪造的功能,通过中间件django.middleware.csrf.CsrfViewMiddleware来完成。而对于django中设置防跨站请求伪造功能有分为全局和局部。
全局:
中间件 django.middleware.csrf.CsrfViewMiddleware
局部:
@csrf_protect,为当前函数强制设置防跨站请求伪造功能,即便settings中没有设置全局中间件。
@csrf_exempt,取消当前函数防跨站请求伪造功能,即便settings中设置了全局中间件。
注:from django.views.decorators.csrf import csrf_exempt,csrf_protect
二、应用
1.form表单
<form action="/login/" method="POST"> {% csrf_token %} <input type="text" name="user"/> <input type="password" name="pwd"/> <input type="checkbox" name="rmb" value="1"> 10秒免登录 <input type="submit" name="提交"/> </form>
2.Ajax
给某个ajax单独添加
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <form action="/login/" method="POST"> {% csrf_token %} <input type="text" name="user"/> <input type="password" name="pwd"/> <input type="checkbox" name="rmb" value="1"> 10秒免登录 <input type="submit" name="提交"/> <input id="btn1" type="button" value="按钮"/> <input id="btn2" type="button" value="按钮"/> </form> <script src="/static/jquery-1.12.4.js"></script> <script src="/static/jquery.cookie.js"></script> <script> $(function () { $("#btn1").click(function () { $.ajax({ url: "/login/", type: "POST", data: {"user": "root", "pwd": "123"}, headers: {"X-CSRFtoken": $.cookie("csrftoken")}, success: function (arg) { } }); }); }); </script> </body> </html>
给所有ajax添加
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <form action="/login/" method="POST"> {% csrf_token %} <input type="text" name="user"/> <input type="password" name="pwd"/> <input type="checkbox" name="rmb" value="1"> 10秒免登录 <input type="submit" name="提交"/> <input id="btn1" type="button" value="按钮"/> <input id="btn2" type="button" value="按钮"/> </form> <script src="/static/jquery-1.12.4.js"></script> <script src="/static/jquery.cookie.js"></script> <script> $(function () { {# XMLHttpRequest#} $.ajaxSetup({ beforeSend: function (xhr, settings) { xhr.setRequestHeader("X-CSRFtoken", $.cookie("csrftoken")); } }); $("#btn1").click(function () { $.ajax({ url: "/login/", type: "POST", data: {"user": "root", "pwd": "123"}, {# headers: {"X-CSRFtoken": $.cookie("csrftoken")},#} success: function (arg) { } }); }); $("#btn2").click(function () { $.ajax({ url: "/login/", type: "POST", data: {"user": "root", "pwd": "123"}, {# headers: {"X-CSRFtoken": $.cookie("csrftoken")},#} success: function (arg) { } }); }); }); </script> </body> </html>
三、官方示例
test.html
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title></title> </head> <body> {% csrf_token %} <input type="button" onclick="Do();" value="Do it"/> <script src="/static/plugin/jquery/jquery-1.8.0.js"></script> <script src="/static/plugin/jquery/jquery.cookie.js"></script> <script type="text/javascript"> var csrftoken = $.cookie(‘csrftoken‘); function csrfSafeMethod(method) { // these HTTP methods do not require CSRF protection return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method)); } $.ajaxSetup({ beforeSend: function(xhr, settings) { if (!csrfSafeMethod(settings.type) && !this.crossDomain) { xhr.setRequestHeader("X-CSRFToken", csrftoken); } } }); function Do(){ $.ajax({ url:"/app01/test/", data:{id:1}, type:‘POST‘, success:function(data){ console.log(data); } }); } </script> </body> </html>
时间: 2024-10-15 00:17:52