2010-06-07 46 views

回答

0

您可以通过在您的ApplicationController中使用before_filter来实现类似的功能。请注意,这只是将验证添加到所有控制器操作中,它不会保护公共的子目录,如样式表,JavaScript和图像。如果出于某种原因,您正在寻找,您应该使用htpasswd方法。

Railscasts:http://railscasts.com/episodes/82-http-basic-authentication - 只需将before_filterauthenticate东西放入您的ApplicationController中即可保护每个控制器。

+0

是否有保障等公共文件,比如样式表的方法吗? – DDDD 2014-01-17 13:58:45

0

基本身份验证,只是在生产,例如:

在application_controller.rb:

USERNAME = 'foo' 
PASSWORD = 'bar 

if RAILS_ENV['production'] 
before_filter :authenticate 
end 

私人

def authenticate 
    authenticate_or_request_with_http_basic do |user_name, password| 
     user_name == USERNAME && password == PASSWORD 
    end 
end