csrf跨站请求伪造
(1)介绍
(2)如何符合校验
| <h1>我是正经网站</h1> |
| <form action="" method="post"> |
| {% csrf_token %} |
| <p>username: <input type="text" name="username"></p> |
| <p>target_user: <input type="text" name="target_user"></p> |
| <p>money<input type="text" name="money"></p> |
| <input type="submit"> |
| <input type="submit" value="ajax请求"> |
| </form> |
- ajax校验方式
- 第一种 利用标签查找获取页面上的随机字符串
| <script> |
| $('#b1').click(function (){ |
| $.ajax({ |
| url: '', |
| type:'post', |
| data:{'csrfmiddlewaretoken':$('[name="csrfmiddlewaretoken"]').val()}, |
| success:function (){ |
| } |
| }) |
| }) |
| </script> |
| <script> |
| $('#b1').click(function () { |
| $.ajax({ |
| url: '', |
| type: 'post', |
| data: {'csrfmiddlewaretoken': '{{ csrf_token }}'}, |
| success: function () { |
| } |
| }) |
| }) |
| </script> |
| function getCookie(name) { |
| var cookieValue = null; |
| if (document.cookie && document.cookie !== '') { |
| var cookies = document.cookie.split(';'); |
| for (var i = 0; i < cookies.length; i++) { |
| var cookie = jQuery.trim(cookies[i]); |
| // Does this cookie string begin with the name we want? |
| if (cookie.substring(0, name.length + 1) === (name + '=')) { |
| cookieValue = decodeURIComponent(cookie.substring(name.length + 1)); |
| break; |
| } |
| } |
| } |
| return cookieValue; |
| } |
| |
| var csrftoken = getCookie('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); |
| } |
| } |
| }); |
| <!DOCTYPE html> |
| <html lang="en"> |
| <head> |
| {% load static %} |
| <meta charset="UTF-8"> |
| <title>Title</title> |
| <script src="{% static 'mysetup.js' %}"></script> |
| </head> |
| <body> |
| <h1>我是正经网站</h1> |
| <form action="" method="post"> |
| <p>username: <input type="text" name="username"></p> |
| <p>target_user: <input type="text" name="target_user"></p> |
| <p>money<input type="text" name="money"></p> |
| <input type="submit"> |
| <input type="submit" value="ajax请求" id="b1"> |
| </form> |
| </body> |
| <script> |
| $('#b1').click(function () { |
| $.ajax({ |
| url: '', |
| type: 'post', |
| data: {'username': 'heart'}, |
| success: function () { |
| } |
| }) |
| }) |
| </script> |
| </html> |
(3)csrf相关装饰器
-
网站整齐都不校验csrf,就单单几个视图需要校验
-
网站整体都校验csrf,就单单几个视图函数不校验
| from django.views.decorators.csrf import csrf_protect,csrf_exempt |
| """ |
| csrf_protect 需要校验 把中间件注释 |
| csrf_exempt 忽视校验 把中间件打开 |
| """ |
(1)csrf_protect 需要校验
| @csrf_protect |
| @csrf_exempt |
| def transefer(request): |
| if request.method == 'POST': |
| username = request.POST.get('username') |
| target_user = request.POST.get('target_user') |
| money = request.POST.get('money') |
| print(f'{username}给{target_user}转了{money}元') |
| return render(request, 'transefer.html') |
| from django.views.decorators.csrf import csrf_protect,csrf_exempt |
| from django.views import View |
| from django.utils.decorators import method_decorator |
| |
| |
| @method_decorator(csrf_protect,name='post') |
| class MyCsrfToken(View): |
| |
| def dispatch(self, request, *args, **kwargs): |
| return super(MyCsrfToken, self).dispatch(request, *args, **kwargs |
| |
| def get(self,request): |
| return HttpResponse('get') |
| |
| |
| @method_decorator(csrf_protect) |
| def post(self,request): |
| return HttpResponse('post') |
(2)csrf_exempt 忽视校验
| from django.views.decorators.csrf import csrf_protect,csrf_exempt |
| from django.views import View |
| from django.utils.decorators import method_decorator |
| |
| |
| @method_decorator(csrf_exempt,name='dispatch') |
| class MyCsrfToken(View): |
| |
| |
| @method_decorator(csrf_exempt) |
| def dispatch(self, request, *args, **kwargs): |
| return super(MyCsrfToken, self).dispatch(request, *args, **kwargs |
| |
| def get(self,request): |
| return HttpResponse('get') |
| |
| def post(self,request): |
| return HttpResponse('post') |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通