gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 07:00:18 24 4
gpt4 key购买 nike

虽然我在 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com