python学习

Python学习打call第六十二天:Django 启用和禁用C

2019-04-16  本文已影响0人  暖A暖

1.Django CSRF的原理

CSRF(Cross Site Request Forgery)也就是跨站请求伪造,实现的原理是CSRF攻击者在用户已经登录目标网站之后,诱使用户访问一个攻击页面,利用目标网站对用户的信任,以用户身份在攻击页面对目标网站发起伪造用户操作的请求,达到攻击目的;

2.CSRF认证

'django.middleware.csrf.CsrfViewMiddleware'
<form action="{% url 'users:image' %}" method="post" enctype="multipart/form-data">
{#  <input type="file" name="upload" accept="image/gif, image/jpeg, image/png, image/jpg">#}
    <input type="file" name="upload">
    <input type="submit" value="提交">
    {% csrf_token %}
</form>

3.CSRF局部禁用

from django.views.generic import View
from django.utils.decorators import method_decorator
from django.views.decorators.csrf import csrf_exempt

class CSRFTestView(View):
    @method_decorator(csrf_exempt)
    def dispatch(self, request, *args, **kwargs):
        return super().dispatch(request, *args, **kwargs)
    def post(self, request):
        pass
from django.views.generic import View
from django.utils.decorators import method_decorator
from django.views.decorators.csrf import csrf_protect

class CSRFTestView(View):
    @method_decorator(csrf_protect)
    def dispatch(self, request, *args, **kwargs):
        return super().dispatch(request, *args, **kwargs)
    def post(self, request):
        pass

4.Postman

Postman是一种网页调试与发送网页http请求的chrome插件,可以用来很方便的模拟get、post、put、patch、delete、copy等多种方式的请求来调试接口;
postman可用作macOS,Windows和Linux操作系统的本机应用程序。Windows系统下安装postman只需要下载安装文件,然后运行安装程序就可以了;


Postman

Postman的下载地址https://www.getpostman.com/downloads/

参考:https://www.9xkd.com/user/plan-view.html?id=1091380484

上一篇 下一篇

猜你喜欢

热点阅读