zoukankan      html  css  js  c++  java
  • python之路--web--2--Django-7-跨站请求伪造

    一、 跨站请求伪造

    一、简介

    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、普通表单

    1 veiw中设置返回值:
    2   return render_to_response('Account/Login.html',
                       data,
                       context_instance=RequestContext(request))   3 或者 4 return render(request, 'xxx.html', data) 5 6 html中设置Token: 7   {% csrf_token %}

    2、Ajax

    对于传统的form,可以通过表单的方式将token再次发送到服务端,而对于ajax的话,使用如下方式。

    view.py

     1 from django.template.context import RequestContext
     2 # Create your views here.
     3   
     4   
     5 def test(request):
     6   
     7     if request.method == 'POST':
     8         print request.POST
     9         return HttpResponse('ok')
    10     return  render_to_response('app01/test.html',context_instance=RequestContext(request))

    text.html

     1 <!DOCTYPE html>
     2 <html>
     3 <head lang="en">
     4     <meta charset="UTF-8">
     5     <title></title>
     6 </head>
     7 <body>
     8     {% csrf_token %}
     9   
    10     <input type="button" onclick="Do();"  value="Do it"/>
    11   
    12     <script src="/static/plugin/jquery/jquery-1.8.0.js"></script>
    13     <script src="/static/plugin/jquery/jquery.cookie.js"></script>
    14     <script type="text/javascript">
    15         var csrftoken = $.cookie('csrftoken');
    16   
    17         function csrfSafeMethod(method) {
    18             // these HTTP methods do not require CSRF protection
    19             return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
    20         }
    21         $.ajaxSetup({
    22             beforeSend: function(xhr, settings) {
    23                 if (!csrfSafeMethod(settings.type) && !this.crossDomain) {
    24                     xhr.setRequestHeader("X-CSRFToken", csrftoken);
    25                 }
    26             }
    27         });
    28         function Do(){
    29   
    30             $.ajax({
    31                 url:"/app01/test/",
    32                 data:{id:1},
    33                 type:'POST',
    34                 success:function(data){
    35                     console.log(data);
    36                 }
    37             });
    38   
    39         }
    40     </script>
    41 </body>
    42 </html>

    更多:https://docs.djangoproject.com/en/dev/ref/csrf/#ajax

  • 相关阅读:
    GYM 101572C(模拟)
    GYM 101572A(单调队列优化dp)
    Codeforces 183C(有向图上的环长度)
    Codeforces 183A(坐标系性质)
    2019湘潭校赛 G(并查集)
    2019湘潭校赛 H(dp)
    2019湘潭校赛 E(答案区间维护)
    Codeforces 1141F2(贪心、预处理)
    Codeforces Round #411(Div. 2)——ABCDEF
    基数排序学习
  • 原文地址:https://www.cnblogs.com/eaglesour/p/7988888.html
Copyright © 2011-2022 走看看