Cross-site request forgery:
Cross-site request forgery, also known as a one-click attack or session riding and
abbreviated as CSRF or XSRF, is a type of malicious exploit of a website whereby
unauthorized commands are transmitted from a user that the website trusts.Unlike cross-
site scripting (XSS), which exploits the trust a user has for a particular site, CSRF
exploits the trust that a site has in a user's browser.
使用秘密 cookie
Remember that all cookies, even the secret ones, will be submitted with every request.
All authentication tokens will be submitted regardless of whether or not the end-user
was tricked into submitting the request. Furthermore, session identifiers are simply
used by the application container to associate the request with a specific session
object. The session identifier does not verify that the end-user intended to submit
the request.
只接受 POST 请求
Applications can be developed to only accept POST requests for the execution of business
logic. The misconception is that since the attacker cannot construct a malicious link,
a CSRF attack cannot be executed. Unfortunately, this logic is incorrect. There are
numerous methods in which an attacker can trick a victim into submitting a forged POST
request, such as a simple form hosted in attacker's website with hidden values. This
form can be triggered automatically by JavaScript or can be triggered by the victim who
thinks form will do something else.
Reference link
Django 每次请求服务器时都会设置 csrftoken cookie,并且当您将数据从客户端发布到服务器时,此令牌与该令牌匹配,如果它不匹配任何概率,如果不匹配则抛出错误,这是恶意请求。
如果您可以使用 csrf_exempt 装饰器来禁用特定视图的 CSRF 保护。
from django.views.decorators.csrf import csrf_exempt
然后在你的视图前写上@csrf_exempt