ruby-on-rails-3 - 使用 iframe 时,Safari 中的 ActionController::InvalidAuthenticityToken 异常

标签 ruby-on-rails-3 cookies safari cross-domain p3p

虽然我在 safari 中从 iframe 发布了一个表单,但它给出了无效的真实性 token 异常。如果我在没有 iframe 的情况下尝试,那么它工作正常。

为什么会发生?我怎样才能解决这个问题?

最佳答案

真实性 token 是 rails 用来保护用户免受 CSRF 攻击的一种机制。这是一个很好的解释,取自 Understanding the Rails Authenticity Token

When the user views a form to create, update, or destroy a resource, the rails app would create a random authenticity_token, store this token in the session, and place it in a hidden field in the form. When the user submits the form, rails would look for the authenticity_token, compare it to the one stored in the session, and if they match the request is allowed to continue.



所以基本上,对于任何会修改您的模型导轨的操作,都希望验证它是否是您发起的更改。

Rails 做到这一点(通过使用 form_forform_tag 助手)通过将 secret 真实性 token 添加到带有 html 标签的表单中,如下所示:<input name="authenticity_token" type="hidden" value="Som3Thin10ngAndUGly">
回到你的问题:我从来没有使用过 iframe,所以我不确定发生了什么,但我的猜测是你的 iframe 表单没有传递 authencity_token。如果是这种情况,解决方案很简单,只需添加一个像上面那样的隐藏输入并使用 form_authenticity_token方法来设置它的值。

关于ruby-on-rails-3 - 使用 iframe 时,Safari 中的 ActionController::InvalidAuthenticityToken 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7007742/

相关文章:

ruby-on-rails - 我如何找到属于特定用户的特定宠物的所有寄生虫?

iOS:如何为 NSHTTPCookie 设置 httponly 标志

java - 在 Yosemite OSX 的 Safari 中显示 JPG 的问题

java - getCookies() 获取 cookie 和 session

session - 通过客户端 cookie Play Framework session

html - 位置 :fixed issue in IO S safari

javascript - Safari 损坏的滚动

ruby-on-rails - 在 Rake 任务 db :schema:load 中抑制输出

ruby-on-rails - 使用 Rails Ajax 渲染不同的 .js 文件

ruby-on-rails - Rails 3 - 处理传入消息(允许用户回复电子邮件通知)