2013-03-27 7 views

答えて

17

名とパスワードを受け入れる前に、フィルタなどなど

http_basic_authenticate_with :name => "dhh", :password => "secret", :except => :index 

authenticate_or_request_with_http_basicあなたは、彼らがいるかどうかを判断するためにいくつかのコードを挿入するためにできるようにブロックを受け入れるのに対し認証する必要があります(documentation)。例えば。

before_filter :authenticate 

def authenticate 
    authenticate_or_request_with_http_basic('Administration') do |username, password| 
    ActiveSupport::SecurityUtils.secure_compare(username, "admin") && 
    ActiveSupport::SecurityUtils.secure_compare(password, "password") 
    end 
end 

は(この例では、安全ではないかもしれない、注意してください。それがsecure_compare代わりのvariable_size_secure_compareを使用しているため、例えば、現在、それは安全ではありません。より安全のためのRailsの現在のバージョンからActionController::HttpAuthenticationhttp_basic_authenticate_withsource codeを参照してください。例)。

+0

これをCapybaraでテストするにはhttp://stackoverflow.com/a/7938935/664833 – user664833

+1

を参照してください**コントローラレベル**でテストするには、 '@ request.env ['HTTP_AUTHORIZATION'] = 'Basic' + Base64 :: encode64( 'username:password') 'それから' get:your_action'。参照:http://apidock.com/rails/ActionController/HttpAuthentication/Basic/ControllerMethods/authenticate_or_request_with_http_basic#197-テスト - 保護されたコントローラ – user664833

+0

'http_basic_authenticate_with'は実際に' authenticate_or_request_with_http_basic'を内部的に呼び出します。 [source](https://github.com/rails/rails/blob/master/actionpack/lib/action_controller/metal/http_authentication.rb#L69)を参照してください。 – mlovic

関連する問題