I was working with Django to create a website and got some questions about CSRF. I use django.middleware.csrf.CsrfViewMiddleware and add <form action="" method="post">{% csrf_token %} in my POST form.
And when I test the website:
GET / HTTP/1.1
Host: 123.207.137.168:8000
Run Code Online (Sandbox Code Playgroud)
Then, I got cookies
Set-Cookie:csrftoken=Ev8veOH89vFDnG3a0GJUsMXA1oGZXxqXRw2nFWiKrvZ9UE10niTlZCiOxdnoKfTv; expires=Thu, 27-Dec-2018 06:37:41 GMT; Max-Age=31449600; Path=/
Run Code Online (Sandbox Code Playgroud)
But in the html:
<input type='hidden' name='csrfmiddlewaretoken' value='JswHLk4fNpxHkh0OObD1uKiOxSDUzkMDWtqzcsFR5pRdRfYEbNNs1AD23Hkjm2fb' />
Run Code Online (Sandbox Code Playgroud)
So I was wondering why the csrftoken and csrfmiddlewaretoken is different and how did the server use these two value to valid if the request is from the users or hackers?
Joh*_*los 11
文档中有一些答案,但是查看代码确实解决了这个“谜团” 基本上 django 所做的如下:
例如,现在当您发出 POST 请求时,会发生以下情况
这种带有两个令牌的方法称为Double-Submit Cookie。Django 的加盐方式允许将相同的 csrf 保密一段时间,而不必为每个请求更新密钥
| 归档时间: |
|
| 查看次数: |
8051 次 |
| 最近记录: |