我想你不能用
authenticate_or_request_with_http_basic
方法超出控制器范围。您可以设置
before_filter
在通用控制器中进行身份验证检查。以下是以下示例
docs comments
:
class AdminController < ApplicationController
before_filter :authenticate
def authenticate
authenticate_or_request_with_http_basic('Administration') do |email, password|
email == 'foo' && password == 'bar'
end
end
end
这是我发现的
Rails Authentication Routing Constraints Considered Harmful
。
尽管如此,我认为有一种方法:
class Basic
def self.matches?(request)
if ActionController::HttpAuthentication::Basic.has_basic_credentials?(request)
credentials = ActionController::HttpAuthentication::Basic.decode_credentials(request)
email, password = credentials.split(':')
email == 'foo' && password == 'bar'
end
end
end
Here is docs on HTTP Basic authentication
举例说明