【问题标题】:Rails 5 ActionController::InvalidAuthenticityToken errorRails 5 ActionController::InvalidAuthenticityToken 错误
【发布时间】:2016-11-14 20:23:57
【问题描述】:

我有一个 rails 应用程序,我计划升级到 rails 5。我正在使用 devise(v4.2.0) 和 rails(v5.0.0)。正如设计 README.md 文件中所建议的那样,我尝试将protect_from_forgery 移到 before_filter 上方,但是当我尝试登录或更新我的错误时,我仍然收到错误ActionController::InvalidAuthenticityToken

我的Application Controller

class ApplicationController < ActionController::Base
 protect_from_forgery with: :exception, prepend: true
 before_action :configure_permitted_parameters, if: :devise_controller?

  protected

   def configure_permitted_parameters
     devise_parameter_sanitizer.permit(:sign_up, keys: [:name])
     devise_parameter_sanitizer.permit(:account_update, keys: [:name])
   end

end

而我的另一个BugController

class BugsController < ApplicationController
  protect_from_forgery prepend: true, with: :exception
  before_action :authenticate_user!
  before_action :set_bug, only: [:show, :edit, :update]

    def update
      respond_to do |format|
      if @bug.update(bug_params)
        format.html { redirect_to @bug, notice: 'Bug was successfully updated.' }
        format.json { render :show, status: :ok, location: @bug }
     else
        format.html { render :edit }
        format.json { render json: @bug.errors, status: :unprocessable_entity }
     end
     end
   end

private
def bug_params
  params.require(:bug).permit(:product, :component, :title, :description, :status_id, :created_by_id, :assigned_to_id)
end


end

【问题讨论】:

    标签: ruby-on-rails ruby devise ruby-on-rails-5


    【解决方案1】:

    这个决定帮助了我。我 [从这里] [1] 做出了决定。就我而言,该主题的不幸名称,使用错误的关键字我没有到达那里,所以我会在这个线程中给出,因为这是错误的确切名称。 就我而言,我在application_controller.rb 文件中“添加”了以下行:

    protect_from_forgery with:: null_session
    

    有一个解决方案,上面写着“REPLACE”行protect_from_forgery with:: exception,如果存在的话,就是我上面引用的那个

       [1]:Rails 4 Authenticity Token

    【讨论】:

      【解决方案2】:

      注意:虽然此答案具有预期的效果,但它会降低整体安全性。 Alon 的以下回答更正确,并维护了网站的安全性。

      class BugsController < ApplicationController
      skip_before_filter :verify_authenticity_token
      protect_from_forgery prepend: true, with: :exception
      before_action :authenticate_user!
      before_action :set_bug, only: [:show, :edit, :update]
      end
      

      喜欢这个

      【讨论】:

      • 我尝试将skip_before_filter :verify_authenticity_token 放入错误控制器,但仍然无法正常工作。
      • Bugs controllerprotect_from_forgery prepend: true, with: :exception before_action :authenticate_user! before_action :set_bug, only: [:show, :edit, :update]
      • 尝试了您提供的答案,但我仍然收到错误ActionController::InvalidAuthenticityToken in BugsController#update
      • 把它放到应用控制器上
      • 最好使用skip_before_action而不是skip_before_filter...
      【解决方案3】:

      我最近以相当大的方式遇到了这个问题,我发现我的错误是我的应用程序的域名最近更改了,但我忘记更新 session_store.rb。这可能不是每个人的问题,但它会将此报告为 CSRF 错误。所以请查看 config/session_store.rb

      【讨论】:

      • 我相信它是 config/initializers/session_store.rb
      【解决方案4】:

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

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

      【讨论】:

        【解决方案5】:

        Devise documentation Rails 5 注释中所述

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

        【讨论】:

        • 这似乎是比跳过真实性令牌验证更好的选择!!
        • 这应该是选择的答案
        • 这个解决方案对我也有帮助。我有带有 SSL 和 Route53 的 AWS CloudFront,我遇到了类似的问题:我正在向 rails 应用程序发送发布请求(没有登录的反馈表单),并且通过 http 没有 CSRF 令牌一切正常,但是通过 SSL 的pitost 没有工作。我需要在云端添加自定义原始标头以解决问题Origin header didn't match request.base_url,然后我遇到问题Can't verify CSRF token authenticity,当我向请求添加令牌并添加标头'X-CSRF-Token': authenticity_token 时我遇到问题InvalidAuthenticityToken,这个解决方案帮助了我
        • 谢谢!这很简单。
        • 这也适用于 rails 6 吗?
        猜你喜欢
        • 2018-11-02
        • 2015-11-23
        • 2017-11-13
        • 1970-01-01
        • 2017-04-12
        • 2018-11-27
        • 2017-09-07
        • 2019-09-25
        • 2015-05-21
        相关资源
        最近更新 更多