ruby on rails,限制访问公共目录

时间:2010-06-07 21:46:30

标签: ruby-on-rails

我需要使用.htaccess类型的机制阻止访问公共目录。这意味着,在显示其他任何内容之前,点击http://localhost:3000应该要求凭据。有可能吗?

2 个答案:

答案 0 :(得分:0)

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

Railscasts:http://railscasts.com/episodes/82-http-basic-authentication - 只需将before_filterauthenticate内容放在ApplicationController中即可保护每个控制器。

答案 1 :(得分: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