Rails 3 http_basic_authenticate_with仅在生产环境中?

时间:2012-01-05 11:02:47

标签: ruby-on-rails ruby-on-rails-3 basic-authentication

有没有办法只使用

http_basic_authenticate_with :name => 'user', :password => 'secret'

当服务器在生产模式下运行时?

3 个答案:

答案 0 :(得分:33)

是的,试试:

class ApplicationController < ActionController::Base
  before_filter :authenticate

  def authenticate
    if Rails.env.production?
      authenticate_or_request_with_http_basic do |username, password|
        username == "user" && password == "%$§$§"
      end 
    end
  end
end

答案 1 :(得分:13)

只需将此添加到您的示例中

即可
http_basic_authenticate_with :name => 'user', :password => 'secret' if Rails.env.production?

答案 2 :(得分:6)

authenticate_or_request_with_http_basic do |username, password|
  username == "user" && password == "secret"
end if Rails.env.production?