玩转Django的POST请求 CSRF
玩转Django的POST请求 CSRF
不少麻油们玩django都会碰到这个问题,POST请求莫名其妙的返回 403 foribidden,希望这篇博文能解答所有问题
三种方法
To enable CSRF protection for your views, follow these steps:
1. Add the middleware`django.middleware.csrf.CsrfViewMiddleware` to your list ofmiddleware classes in `setting.py`, MIDDLEWARE_CLASSES. (It should comebefore any view middleware that assume that CSRF attacks havebeen dealt with.)
Alternatively, you can use the decorator `@csrf_protect` on particular viewsyou want to protect (see below).
我尝试了@csrf_exempt也可以呢
8过@csrf_exempt的作用是对当前view方法关闭CSRF
2. In any template that uses a POST form, use the csrf_token tag insidethe <form> element if the form is for an internal URL, e.g.:
`<form action="." method="post">{% csrf_token %}`
This should not be done for POST forms that target external URLs, sincethat would cause the CSRF token to be leaked, leading to a vulnerability.
3. In the corresponding view functions, ensure that the`django.core.context_processors.csrf` context processor isbeing used. Usually, this can be done in one of two ways:
Use RequestContext, which always uses`django.core.context_processors.csrf` (no matter what yourTEMPLATE_CONTEXT_PROCESSORS setting). If you are usinggeneric views or contrib apps, you are covered already, since theseapps use RequestContext throughout.
Manually import and use the processor to generate the CSRF token andadd it to the template context. e.g.:
from django.core.context_processors import csrf
from django.shortcuts import render_to_response
def my_view(request):
c = {}
c.update(csrf(request))
# ... view code here
return render_to_response("a_template.html", c)
You may want to write your ownrender_to_response() wrapper that takes careof this step for you.
The utility script extras/csrf_migration_helper.py can help to automate thefinding of code and templates that may need these steps. It contains full helpon how to use it.
说白了就是需要这些东东
提交的时候得有个csrfmiddlewaretoken
<input type="hidden" name="csrfmiddlewaretoken" value="{{ csrf_token }}">
ajax提交的时候就需要手动添加了:
django在加载form的时候会生成token,同时加到了cookie中
var param = $.param($('#addipModal :input:not(button)'));
$.ajax({
url: "{% url 'attendence:ip_add'%}",
method: "post",
data: param + "&csrfmiddlewaretoken=" + $.cookie('csrftoken'),
success: function(data) {
$("#cancelip").click();
alert(data);
window.location.reload();
}
});
附官方文档地址:https://docs.djangoproject.com/en/dev/ref/contrib/csrf/
玩转Django的POST请求 CSRF的更多相关文章
- Django的POST请求时因为开启防止csrf,报403错误,及四种解决方法
Django默认开启防止csrf(跨站点请求伪造)攻击,在post请求时,没有上传 csrf字段,导致校验失败,报403错误 解决方法1: 注释掉此段代码,即可. 缺点:导致Django项目完全无法防 ...
- Django学习系列之CSRF
Django CSRF 什么是CSRF CSRF, Cross Site Request Forgery, 跨站点伪造请求.举例来讲,某个恶意的网站上有一个指向你的网站的链接,如果 某个用户已经登录到 ...
- token、cookie和session区别以及django中的cookie,csrf
参考:https://my.oschina.net/xianggao/blog/395675?fromerr=GC9KVenE [前言]登录时需要post的表单信息. 先跳过具体案例,讲解基础知识: ...
- Django跨域请求之JSONP和CORS
现在来新建一个Django项目server01,url配置为 url(r'^getData.html$',views.get_data) 其对应的视图函数为get_data: from django. ...
- Django的安全机制 CSRF 跨站请求访问
跨站请求伪造 一.简介 django为用户实现防止跨站请求伪造的功能,通过中间件 django.middleware.csrf.CsrfViewMiddleware 来完成.而对于django中设置防 ...
- Django后台post请求中的csrf token
使用Requests库操作自己的Django站点,post登陆admin页面返回403,serverlog显示csrf token not set. csrf token是get登陆页面时服务器放在c ...
- Django中间件如何处理请求
Django中间件 在http请求 到达视图函数之前 和视图函数return之后,django会根据自己的规则在合适的时机执行中间件中相应的方法. Django1.9版本以后中间件的执行流程 1. ...
- 10 Django之Ajax请求
一.什么是Ajax技术? 异步的JavaScript和XML.使用Javascript语言与服务器进行异步交互,传输的数据为XML(更多的使用json数据).Ajax不是一门新的编程语言,而是一种使用 ...
- python——django的post请求
两次被同一块石头绊倒简直不可原谅!第一次写django程序的时候,就因为ajax post请求折腾了整整一天,时隔两个多月昨天又被虐一整晚.叔可忍婶儿也不能忍了!!!重要的事情写下来,为以后轻松碾压p ...
随机推荐
- Java学习笔记 02 String类、StringBuilder类、字符串格式化和正则表达式
一.String类一般字符串 声明字符串 >>String str 创建字符串 >>String(char a[])方法用于将一个字符数组创建为String对象 >> ...
- 基于Chrome内核(WebKit.net)定制开发DoNet浏览器
1. 源起 a) 定制.Net浏览器 本人是一名C#开发者,而作为C#开发者,做客户端应用中最头痛的一件事就是没有一个好的UI解决方案, WinFrom嘛,效率虽然还不错,但是做一些特殊 ...
- Redis 入门练习
Redis提供了一个命令行入门练习的web:http://try.redis.io/ ///////////////////////////////////////////////////////// ...
- Ubuntu 安装mysql和简单操作
http://www.cnblogs.com/zhuyp1015/p/3561470.html ubuntu上安装mysql非常简单只需要几条命令就可以完成. 1. sudo apt-get inst ...
- ArcGIS线转面的方法
ArcGIS作为GIS软件中的龙头老大,其功能是非常强大的,但是如果作为一个初学者,其部分常用的重要功能不容易掌握,今天就讲一讲在矢量化时非常重要的功能:线转面. ArcGIS在进行大范围的矢量化时一 ...
- ok
第一个姑娘该是个爱你的人,出现在你没法区分爱和饥渴的时候.那时候你还在青春期的尾巴上,满脑子的性冲动混合着韩剧爱情幻想.你自尊脆弱而又怯懦无助,随便抓住哪一根稻草都当是救命的灵药. 她也许相貌平平,但 ...
- android开发学习之Layer List
Android中drawable分为Bitmap File.Nine-Patch File.Layer List.State List.Level List.Transition Drawable.I ...
- php-empty()
$arr = array(array(),array()); 原来empty($arr)值为true哦
- JSON 序列化和反序列化——JavaScriptSerializer实现
一. JavaScriptSerializer 类由异步通信层内部使用,用于序列化和反序列化在浏览器和 Web 服务器之间传递的数据.您无法访问序列化程序的此实例.但是,此类公开了公共 API.因此, ...
- java jdb命令详解
jdb - Java debugger 功能描述: 通过简单的命令行程序,对本地或远程jvm进程进行调试. 开启jdb会话: 有多种方式可以开启jdb会话. (1)常见的方式是采用Jdb命令打开一个新 ...