惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
SegmentFault 最新的问题
Spread Privacy
Spread Privacy
Google DeepMind News
Google DeepMind News
WordPress大学
WordPress大学
Blog — PlanetScale
Blog — PlanetScale
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Apple Machine Learning Research
Apple Machine Learning Research
SecWiki News
SecWiki News
腾讯CDC
P
Privacy International News Feed
Webroot Blog
Webroot Blog
J
Java Code Geeks
爱范儿
爱范儿
A
About on SuperTechFans
S
Secure Thoughts
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
D
DataBreaches.Net
Cloudbric
Cloudbric
Security Archives - TechRepublic
Security Archives - TechRepublic
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
C
Cyber Attacks, Cyber Crime and Cyber Security
P
Proofpoint News Feed
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Security Latest
Security Latest
Forbes - Security
Forbes - Security
小众软件
小众软件
www.infosecurity-magazine.com
www.infosecurity-magazine.com
C
Cybersecurity and Infrastructure Security Agency CISA
T
Threatpost
量子位
MongoDB | Blog
MongoDB | Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
月光博客
月光博客
W
WeLiveSecurity
P
Privacy & Cybersecurity Law Blog
Vercel News
Vercel News
Google Online Security Blog
Google Online Security Blog
云风的 BLOG
云风的 BLOG
GbyAI
GbyAI
S
Security @ Cisco Blogs
T
The Exploit Database - CXSecurity.com
Help Net Security
Help Net Security
V
Visual Studio Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
Application and Cybersecurity Blog
Application and Cybersecurity Blog
博客园 - 聂微东
P
Proofpoint News Feed
C
CERT Recently Published Vulnerability Notes
Attack and Defense Labs
Attack and Defense Labs

Ajax - 分类 - cywhat's blog

暂无文章

Django的csrf防御机制
cywhat · 2022-07-04 · via Ajax - 分类 - cywhat's blog

前景:

Html页面的表单没有完全使用Django的form进行渲染,故Js不能使用$('#ClassID').serialize()来获取CsrfData,然后报错CSRF token missing or incorrect.

原因:

Django第一次响应来自某个客户端的请求时,会在服务器端随机生成一个Token,把这个Token放在Cookie里。然后每次Post请求都会带上这个Token, 这样可以能避免被Csrf攻击。所以会在每个Html模板中增加一个 {% csrf_token %}标签。

视图函数返回Csrf的三种方式

1
2
3
4
5
return render_to_response('Account/Login.html',data,context_instance=RequestContext(request))

return HttpResponse(_template.render(context, request))

return render(request, 'xxx.html', data)

Html中的展现形式

/img/img60.png

解决

1.通过Js先获取到Csrf值

1
2
3
<script>
const csrftoken = document.querySelector('[name=csrfmiddlewaretoken]').value;
</script>

2.通过在Ajax设置请求头传递

1
2
3
4
5
<script>
    $.ajax({
        headers: {'X-CSRFToken': csrf_token},     
    })
</script>

关注一下再走吧

公众号 小程序

赞赏支持

微信打赏 支付宝打赏