Rails:csrf_meta_tag如何工作? [英] Rails: How Does csrf_meta_tag Work?
问题描述
我是一名PHP开发人员,通过阅读 Michael Hartl的教程来学习Ruby on Rails 。这是书中的引述,引用了 csrf_meta_tag
:
I'm a PHP developer learning Ruby on Rails by reading Michael Hartl's tutorial. Here is a quote from the book, referring to csrf_meta_tag
:
... Rails方法
csrf_meta_tag
[防止]跨站点请求
伪造(CSRF),这是一种恶意Web攻击。不必担心
的详细信息(我不会);只是知道Rails正在努力确保您的
应用程序安全。
...the Rails method
csrf_meta_tag
[prevents] cross-site request forgery (CSRF), a type of malicious web attack. Don’t worry about the details (I don’t); just know that Rails is working hard to keep your application secure.
问题是,我真的很好奇。插入 csrf-param
和 csrf-token
元标记如何防止CSRF?我尝试了谷歌搜索,但找不到任何东西。
The thing is, I'm genuinely curious. How does inserting csrf-param
and csrf-token
meta tags prevent CSRF? I tried Googling, but couldn't find anything.
推荐答案
csrf_meta_tag
基本上可以实现与隐藏表单域相同的功能,但是可以使不依赖表单的javascript请求成为获取令牌的简便方法。
csrf_meta_tag
is basically fulfilling the same thing as hidden form fields but is there to give javascript requests that aren't tied to a form an easy way of getting the token.
使用 jquery-ujs 库,该meta标签的内容会自动添加(作为请求标头)进行任何ajax请求。
If you use the jquery-ujs library the content of that meta tag is automatically added (as a request header) to any ajax requests made.
这篇关于Rails:csrf_meta_tag如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!