Railsで行うBasic認証

Rails3.1以前は authenticate_or_request_with_http_basic メソッドをbefore_filter内で呼ぶなりして実装していたがRails3.1にて http_basic_authenticate_with クラスメソッドが追加された。
#270 Authentication in Rails 3.1 - RailsCasts

ActionController::HttpAuthentication::Basic
シンプルなBasic認証を実装するのであればわざわざ認証用メソッドを作らなくても http_basic_authenticate_with を使えばbefore_actionに設定してくれる。

# File actionpack/lib/action_controller/metal/http_authentication.rb, line 68
          def http_basic_authenticate_with(options = {})
            before_action(options.except(:name, :password, :realm)) do
              authenticate_or_request_with_http_basic(options[:realm] || "Application") do |name, password|
                name == options[:name] && password == options[:password]
              end
            end
          end
http_basic_authenticate_with (ActionController::HttpAuthentication::Basic::ControllerMethods::ClassMethods) - APIdock