Django学习笔记(4)
為什么80%的碼農(nóng)都做不了架構(gòu)師?>>> ??
首先提一個(gè)問(wèn)題:在Django中如何處理CRSF(Cross-site request forgery)?
先看一下CSRF原理。
其實(shí)就是惡意網(wǎng)站利用正常網(wǎng)站的cookie去非法請(qǐng)求。
##Java處理方式##
一般做法需要后臺(tái)和前端配合采取策略去防止CRSF。
1 前端頁(yè)面請(qǐng)求后臺(tái)CGI的時(shí)候帶上一個(gè)參數(shù)tk,這個(gè)tk是將cookie經(jīng)過(guò)time33算法簽名算得的一個(gè)參數(shù)。
假設(shè)cookie為"thiscookie",tk=time33("thiscookie")=fg2hgasdf;
這樣在CGI上添加tk=fg2hgasdf,當(dāng)然請(qǐng)求時(shí)還會(huì)帶上"thiscookie"。所以GET請(qǐng)求可能是這樣的
my.oschina.net/anti-csrf?name=huangyi&tk=fg2hgasdf
在Request Headers中有一個(gè)Cookie:cookie="thiscookie"
三方網(wǎng)站是無(wú)法獲取cookie計(jì)算這個(gè)tk簽名的。
2 后臺(tái)程序在request中獲取到cookie,也經(jīng)過(guò)time33算法簽名,即token=time33("thiscookie") 。然后與url中帶過(guò)來(lái)的tk進(jìn)行比較,如果token==tk,則認(rèn)為是正常的請(qǐng)求。這一步通常是寫(xiě)在攔截器中的。
Django解決方法
Django自帶CRSF的解決方法
Thankfully, you don’t have to worry too hard, because Django comes with a very easy-to-use system for protecting against it. In short, all POST forms that are targeted at internal URLs should use the {% csrf_token %} template tag。
django 第一次響應(yīng)來(lái)自某個(gè)客戶端的請(qǐng)求時(shí),會(huì)在服務(wù)器端隨機(jī)生成一個(gè) token,把這個(gè) token 放在 客戶端的cookie 里。
客戶端提交所有的 POST 表單時(shí),必須包含一個(gè) csrfmiddlewaretoken 字段 (只需要在模板里加一個(gè) tag {% csrf_token %}, django 就會(huì)自動(dòng)生成),每次POST請(qǐng)求也會(huì)帶上1中的cookie。
服務(wù)器端在處理 POST 請(qǐng)求之前,Django 會(huì)驗(yàn)證這個(gè)請(qǐng)求cookie 里的 csrftoken 字段的值和提交的表單里的 csrfmiddlewaretoken 字段的值是否一樣。如果一樣,則表明這是一個(gè)合法的請(qǐng)求。
在所有 ajax POST 請(qǐng)求里,添加一個(gè) X-CSRFTOKEN header,其值為 cookie 里的 csrftoken 的值
前端頁(yè)面
<h1>{{ question.question_text }}</h1>{% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}<form action="{% url 'polls:vote' question.id %}" method="post"> {% csrf_token %} {% for choice in question.choice_set.all %}<input type="radio" name="choice" id="choice{{ forloop.counter }}" value="{{ choice.id }}" /><label for="choice{{ forloop.counter }}">{{ choice.choice_text }}</label><br /> {% endfor %} <input type="submit" value="submit to Vote" /> </form>只需要在表單中加入{% csrf_token %}即可,作用是生成表單的csrfmiddlewaretoken字段。
在請(qǐng)求的頭中可以看見(jiàn)Cookie有兩個(gè)元素
Cookie:sessionid=fg2xeknnq2f37a0yvz9rpmmmu78lk4vi; csrftoken=aFuwdo1WRnnwN9KlWK4oVZF1PO6DtDFG表單FormData中含有一個(gè) csrfmiddlewaretoken 字段。
后臺(tái)服務(wù)主要是驗(yàn)證cookie中的csrftoken與csrfmiddlewaretoken字段是否相等,在CsrfViewMiddleware中實(shí)現(xiàn)。
同時(shí)對(duì)應(yīng){% csrf_token %}模板寫(xiě)法,需要在views函數(shù)中加入
from django.shortcuts import render_to_response from django.template.context_processors import csrfdef my_view(request):c = {}c.update(csrf(request))# ... view code herereturn render_to_response("a_template.html", c)##參考
https://www.ibm.com/developerworks/cn/web/1102_niugang_csrf/
https://docs.djangoproject.com/en/1.8/ref/csrf/
轉(zhuǎn)載于:https://my.oschina.net/lvyi/blog/504509
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來(lái)咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的Django学习笔记(4)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
 
                            
                        - 上一篇: FGUI使用方法(四):List列表的详
- 下一篇: Linux 应用---make及make
