What is the difference between http_basic_authenti

2019-02-06 01:56发布

问题:

What is the difference between

http_basic_authenticate_with()

and

authenticate_or_request_with_http_basic()

methods?

Thanks for your full explanation.

回答1:

From what I can understand from the docs, http_basic_authenticate_with acts as a before filter which accepts a name and password such as

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

Whereas authenticate_or_request_with_http_basic accepts a block allowing for you to insert some code to determine whether they should be authenticated (documentation). E.g.

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