【问题标题】:ActionController::InvalidAuthenticityToken Rails 5 / Devise / Audited / PaperTrail gemActionController::InvalidAuthenticityToken Rails 5 / 设计 / 审核 / PaperTrail gem
【发布时间】:2017-09-07 10:35:43
【问题描述】:

背景详情

我正在使用 Devise 进行身份验证以登录到 Rails 5 应用程序。

每当我捆绑 AuditedPaper Trail gem 时,当我尝试#create 一个新会话(通过登录表单 - /users/sign_in)时,我收到以下错误:

ActionController::InvalidAuthenticityToken

环境详情

Ruby 2.3.1

宝石:

  • 导轨 5.0.2
  • 设计 => 4.2.1
  • paper_trail => 7.0.1

复制步骤:

  1. 创建 Rails 5 应用程序
  2. 添加设计宝石
  3. 添加 Audited 或 Paper Trail gem
  4. 尝试登录

【问题讨论】:

  • 你在 application_controller 中有protect_from_forgery with: :exception 吗?
  • @whodini9 - 宾果游戏。这就是错误的原因。我把它改成这样:protect_from_forgery prepend: true 然后事情就很开心了。感谢您的帮助。

标签: ruby-on-rails ruby devise acts-as-audited


【解决方案1】:

事实证明,Devise documentation 对这个错误很有启发性:

对于 Rails 5,请注意protect_from_forgery 不再添加到 before_action 链,所以如果你之前设置了 authenticate_user protect_from_forgery,您的请求将导致“无法验证 CSRF 令牌真实性。" 要解决此问题,请更改 你打电话给他们,或使用protect_from_forgery prepend: true

解决方法是更改​​我的应用程序控制器中的代码:

 protect_from_forgery with: :exception

到这里:

 protect_from_forgery prepend: true

在我尝试添加 Audited 或 Paper Trail gem 之前,此问题并未显现。

【讨论】:

  • 为我工作,奇怪的是,如果你仍然登录,问题不会出现,所以它似乎是间歇性的。
  • @JohnLinux - 我也经历过同样的事情。我不认为它是间歇性的。我认为 Devise 在您登录时会通过不同的调用堆栈发送给您。
  • 这让我很困惑和沮丧。其他多个帖子提到“移动”它,但没有具体说明前置。谢谢。
  • 建议的更改实际上是在更改更多。你可能想要protect_from_forgery with: :exception, prepend: true,但你最终会得到protect_from_forgery with: :null_session, prepend: true,因为:null_session 是with:的默认值
【解决方案2】:

这发生在我的开发机器上。原来我在设置

Rails.application.config.session_store

用于生产中的安全目的。并且以某种方式在此代码中以开发模式运行。我必须注释掉这一行,它现在可以正常工作了。

Rails.application.config.session_store :cookie_store, key: '_my_session', secure: true, same_site: :strict

【讨论】:

    【解决方案3】:

    对于遇到此问题的任何人来说,另一件事是将以下内容添加到您的环境配置文件中:

    config.action_controller.forgery_protection_origin_check = false
    

    对我来说,生产工作正常,但暂存和开发却没有,这为我解决了问题。

    【讨论】:

      【解决方案4】:

      在我的项目中,我们遇到了这个问题,我们不能覆盖 protect_from_forgery。 建立的解决方案表明为我审计和工作的github。

      把这个放到gemfile中:

      gem "audited", github: "collectiveidea/audited"
      

      【讨论】:

      • 这似乎不是 OP 问题的答案?
      • @LethalProgrammer,对不起,我不明白 OP 问题。我的答案是无需编辑 protect_from_forgery 即可工作,因为我的项目使用 before_action 回调在设计中创建会话之前验证某些内容。抱歉,如果我的回答不清楚。
      • @msfreire - 如果您使用您发布的 github 分支,您是否建议可以在应用程序控制器中使用 protect_from_forgery with: :exception
      • @aldefouw 是的。如果您将分支放在 gem 文件中,我不需要更改为 protect_from_forgery prepend: true。我正在使用 rails 5.0.1,如果您使用 rails 5+,请尝试一下
      【解决方案5】:

      documentation中所述。

      对于 Rails 5,请注意 protect_from_forgery 不再是前置 到 before_action 链,所以如果你设置了 authenticate_user 在protect_from_forgery 之前,您的请求将导致“无法验证 CSRF 令牌真实性。”要解决此问题,请更改顺序 你称之为他们,或使用protect_from_forgery prepend: true。

      我用过类似的东西,它对我有用。

      class WelcomeController < ::Base
          protect_from_forgery with: :exception
          before_action :authenticate_model!
      end
      

      【讨论】:

        【解决方案6】:

        我的解决方案是手动转到浏览器的设置并删除缓存。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-11-02
          • 2017-11-13
          • 2016-11-14
          • 2017-04-12
          • 2018-11-27
          • 2015-11-23
          • 2014-12-17
          • 2019-09-25
          相关资源
          最近更新 更多