http_basic_authenticate_with和authenticate_or_request_with_http_basic有什么区别?它们都与http基本认证相关。

23

什么是两者之间的区别?

http_basic_authenticate_with()

authenticate_or_request_with_http_basic()

方法?

感谢您的详细说明。

1个回答

28

根据文档http_basic_authenticate_with作为一个before过滤器,接受一个名称和密码,例如:

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

而 authenticate_or_request_with_http_basic 接受一个块,允许您插入一些代码来确定是否应该进行身份验证 (文档)。例如:

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

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-Testing-protected-controllers - user664833
http_basic_authenticate_with 实际上在内部调用了 authenticate_or_request_with_http_basic。请参见源代码 - mlovic

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接