0

私のアプリケーションコントローラーは次のようになります。

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。

4

1 に答える 1

2

handle_unverified_requestをオーバーライドする必要があると思います。

そんな感じ:

class ApplicationController < ActionController::Base
  protect_from_forgery

  protected
    def handle_unverified_request
      redirect_to root_url, :error => "forgery protection"
    end
end
于 2011-11-27T12:37:55.483 に答える