The CSRF middleware and template tag provides easy-to-use protection against Cross Site Request Forgeries. This type of attack occurs when a malicious Web site contains a link, a form button or some JavaScript that is intended to perform some action on your Web site, using the credentials of a logged-in user who visits the malicious site in their browser.
CSRF中间件以及木板标签可以很轻松的应对跨站点请求伪造攻击。这类攻击主要出现在一些恶意网站(比如B)通过链接、表单按钮或js代码借助已登陆网站A的用户的认证信息在B上所作的一些操作。
The first defense against CSRF attacks is to ensure that GET requests (and other‘safe’ methods, as defined by 9.1.1 Safe Methods, HTTP 1.1,RFC 2616) are side-effect free. Requests via ‘unsafe’ methods,such as POST, PUT and DELETE, can then be protected by following the steps below.
防御CSRF攻击首先要确保GET请求除了获取资源外,不要任何修改后台数据的操作,可以进行这些操作的方法(POST, PUT 与 DELETE)可以通过以下方法来防御。
防御CSRF攻击,请遵循以下步骤:
1、The CSRF middleware is activated by default in the MIDDLEWARE_CLASSES setting. If you override that setting, remember that 'django.middleware.csrf.CsrfViewMiddleware' should come before any view middleware that assume that CSRF attacks have been dealt with.
CSRF中间件默认是放在MIDDLEWARE_CLASSES中,已经被激活的,如果你把它改了,请确保django.middleware.csrf.CsrfViewMiddleware出现在任何可能被CSRF 攻击的中间件前面。
If you disabled it, which is not recommended, you can use csrf_protect() on particular views you want to protect (see below).
如果你把它禁止了(不推荐这么做),你可以用装饰器csrf_protect来保护指定的view。
2、In any template that uses a POST form, use the csrf_token tag inside the element if the form is for an internal URL, e.g.:
在所有包含post表单的木板页面,确保csrf_token标签已被放在了