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

推荐订阅源

Simon Willison's Weblog
Simon Willison's Weblog
G
Google Developers Blog
Spread Privacy
Spread Privacy
I
InfoQ
V
V2EX
S
Schneier on Security
小众软件
小众软件
C
CERT Recently Published Vulnerability Notes
博客园 - 聂微东
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Stack Overflow Blog
Stack Overflow Blog
T
Threat Research - Cisco Blogs
L
Lohrmann on Cybersecurity
Recent Announcements
Recent Announcements
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Attack and Defense Labs
Attack and Defense Labs
云风的 BLOG
云风的 BLOG
The Hacker News
The Hacker News
S
SegmentFault 最新的问题
C
Cybersecurity and Infrastructure Security Agency CISA
NISL@THU
NISL@THU
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
GbyAI
GbyAI
Latest news
Latest news
S
Secure Thoughts
Project Zero
Project Zero
MongoDB | Blog
MongoDB | Blog
I
Intezer
Security Latest
Security Latest
Apple Machine Learning Research
Apple Machine Learning Research
Vercel News
Vercel News
N
Netflix TechBlog - Medium
V2EX - 技术
V2EX - 技术
量子位
T
Threatpost
T
The Blog of Author Tim Ferriss
Y
Y Combinator Blog
T
Tor Project blog
A
Arctic Wolf
Microsoft Security Blog
Microsoft Security Blog
T
The Exploit Database - CXSecurity.com
大猫的无限游戏
大猫的无限游戏
T
Tailwind CSS Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
C
Check Point Blog
博客园 - Franky
Google DeepMind News
Google DeepMind News
The Register - Security
The Register - Security
The GitHub Blog
The GitHub Blog
L
LINUX DO - 热门话题

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>

关注一下再走吧

公众号 小程序

赞赏支持

微信打赏 支付宝打赏