Django之CSRF

Django之CSRF

  CSRF 跨站请求伪造。在GET请求中,生成随机字符串。

   在没有生成csrf安全认证字符串的时候的实例:

  url:

    url('csrf1/', views.csrf1),

  views:

def csrf1(request):
    if request.method == "GET":
        return render(request,"csrf1.html")

  csrf1.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <form method="post" action="/csrf1/">
        <input type="text" name="user">
        <input type="submit" value="提交">
    </form>
</body>
</html>

   在不生成随机字符串的时候,提交的话,csrf会做阻拦。

  那么如何处理呐?在前端代码块中加上 {%csrf_token%},便会生成一串随机字符串。

    在csrf1.html页面中加入的话,在提交前,可以查看一下页面的源码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <form method="post" action="/csrf1/">
        {% csrf_token %}
        <input type="text" name="user">
        <input type="submit" value="提交">
    </form>
</body>
</html>

 

   这里便会生成一串value的随机字符串。这个字符串是每次访问都是不一样的。

   views.py

def csrf1(request):
    if request.method == "GET":
        return render(request,"csrf1.html")
    else:
        return HttpResponse("ok")

  这样就可以正常的进行跳转。

    但是csrf_token 并不是单单的页面生成一个随机字符串,他还在Cookie里生成里一个csrftoken

 

  局部禁用csrf:

  我们也可以指定那些函数可以不用csrf机制检测,

  导入 from django.views.decorators.csrf import csrf_exempt 就可以是csrf1函数不做csrf检测。

  局部禁用csrf 检测:用 csrf_exempt。

from django.views.decorators.csrf import csrf_exempt

@csrf_exempt
def csrf1(request):
    if request.method == "GET":
        return render(request,"csrf1.html")
    else:
        return HttpResponse("ok")

  局部使用:

  局部使用csrf检测:用 csrf_protect。

from django.views.decorators.csrf import csrf_protect

@csrf_protect
def csrf1(request):
    if request.method == "GET":
        return render(request,"csrf1.html")
    else:
        return HttpResponse("ok")

  注意:

  在CBV中加csrf是不一样的,他目前只能通过导入 from django.utils.decorators import method_decorator 用 method_decorator()的方式将csrf当参数一样用。

from django.views import View
from django.utils.decorators import method_decorator

@method_decorator(csrf_protect)
class Foo(View):

    def get(self,request):
        pass
    def post(self,request):
        pass

  这样加上去的话,是将这个类都加上了这个装饰器。

  如果还是CBV,只想对get函数加装饰器,也是如此的加法:

from django.views import View
from django.utils.decorators import method_decorator

class Foo(View):

    @method_decorator(csrf_protect)
    def get(self,request):
        pass
    
    def post(self,request):
        pass

  这里注意,这个装饰器不单单是加csrf是这样去加,而是所有的CBV的类的装饰器都是这样加,参数可以是任何函数。

from django.views import View
from django.utils.decorators import method_decorator

class Foo(View):

    @method_decorator(wrapper)
    def get(self,request):
        pass

    def post(self,request):
        pass

  可以是wrapper,也可以加在类上,并且指定给那个函数加:

from django.views import View
from django.utils.decorators import method_decorator

@method_decorator(wrapper,name="post")
@method_decorator(wrapper,name="get")
class Foo(View):

    def get(self,request):
        pass

    def post(self,request):
        pass

  这样分别给post和get函数加上了wrapper ,用name可以指定。

  也可以加在dispatch上:加在dispatch上也就是给这个类全加上了。

  dispatch的作用:因为CBV的请求来了,会先到dispatch里面,dispatch会根据反射在执行post和get操作

from django.views import View
from django.utils.decorators import method_decorator

@method_decorator(wrapper,name="dispatch")
class Foo(View):
    def dispatch(self, request, *args, **kwargs):
        return xxx

    def get(self,request):
        pass

    def post(self,request):
        pass

  

  以上就是csrf的添加操作及关于CBV的装饰器的操作。

--------- END --------

原文地址:https://www.cnblogs.com/george92/p/11340794.html