1.CSRF定義
- 偽裝來自受信任用戶的請求來訪問受信任的網站,(攻擊者盜用了你的身份,以你的名義發送惡意請求)
- 產生條件
1、用戶要登錄受信任的網站,並在本地生成cookie
2、在不退出安全網站的情況下,訪問危險網站
(1)驗證 HTTP Referer 字段;查看請求來源的地址
(2)在請求地址中添加 token 並驗證;
(3)在 HTTP 頭中自定義屬性並驗證
(4)在表單中添加from.csrf_token
2. 跨域問題
- 前端處理Jsonp
- 后台簡單,非簡單請求預檢(options)
- response['Access-Control-Allow-Methods'] 指定cent_type,token
from django.middleware.security import MiddlewareMixin
class MyMiddle(MiddlewareMixin ):
def process_response(self,request,response):
if request .method=='OPTIONS':
response['Access-Control-Allow-Methods'] = '*'
response['Access-Control-Allow-Headers'] = '*'
response['Access-Control-Allow-Origin'] = '*'
return response
# 在settings里配置
# 'app01.utils.myMiddle.MyMiddle'