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 Viewfrom django.utils.decorators import method_decoratorfrom django.views.decorators.csrf import csrf_exemptclass 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 Viewfrom django.utils.decorators import method_decoratorfrom django.views.decorators.csrf import csrf_protectclass 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的下载地址 : https://www.getpostman.com/downloads/