【问题标题】:Rails CSFR protection: is it corrent to write before_filter?Rails CSRF 保护:写 before_filter 是否正确?
【发布时间】:2011-12-22 14:45:33
【问题描述】:

我的应用程序控制器如下所示:

class ApplicationController < ActionController::Base
  protect_from_forgery
  before_filter :check_csrf
  def check_csrf
    if not verified_request?
      redirect_to root_url, :error => "forgery protection"
      return
    end
  end
end

如果没有 check_csrf,Rails 会在错误响应时向服务器控制台写入警告,然后像往常一样继续执行。所以我不得不编写自己的 check_csrf。现在它工作正常。这是正确的吗?有没有更简单的方法来阻止错误请求的执行?

Rails 版本:3.1。

【问题讨论】:

    标签: ruby-on-rails-3.1 csrf-protection protect-from-forgery


    【解决方案1】:

    我认为你应该覆盖handle_unverified_request

    类似的东西:

    class ApplicationController < ActionController::Base
      protect_from_forgery
    
      protected
        def handle_unverified_request
          redirect_to root_url, :error => "forgery protection"
        end
    end
    

    【讨论】:

      猜你喜欢
      • 2010-12-22
      • 2011-04-29
      • 1970-01-01
      • 2011-06-27
      • 2019-05-16
      • 1970-01-01
      • 2012-05-01
      • 2014-07-31
      • 1970-01-01
      相关资源
      最近更新 更多