【发布时间】:2013-04-23 15:24:20
【问题描述】:
有没有办法只使用
http_basic_authenticate_with :name => 'user', :password => 'secret'
服务器何时以生产模式运行?
【问题讨论】:
标签: ruby-on-rails ruby-on-rails-3 basic-authentication
有没有办法只使用
http_basic_authenticate_with :name => 'user', :password => 'secret'
服务器何时以生产模式运行?
【问题讨论】:
标签: ruby-on-rails ruby-on-rails-3 basic-authentication
是的,试试:
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
【讨论】:
Rails.env.production? 的粉丝,因为无论应用程序在哪个服务器上运行,它最终都是正确的。我更喜欢将用户名/密码放在环境变量中,并根据是否设置来决定进行身份验证。但我意识到这超出了这个问题的范围。 :)
只需将此添加到您的示例中
http_basic_authenticate_with :name => 'user', :password => 'secret' if Rails.env.production?
【讨论】:
authenticate_or_request_with_http_basic do |username, password|
username == "user" && password == "secret"
end if Rails.env.production?
【讨论】: