密码保护rails登台环境
我正在尝试找出保护我的临时环境的最佳方法。 目前我正在同一台服务器上运行暂存和生产。
我能想到的两个选择是:
使用rails摘要身份validation
我可以在application_controller.rb中放置这样的东西
# Password protection for staging environment if RAILS_ENV == 'staging' before_filter :authenticate_for_staging end def authenticate_for_staging success = authenticate_or_request_with_http_digest("Staging") do |username| if username == "staging" "staging_password" end end unless success request_http_digest_authentication("Admin", "Authentication failed") end end
这是从Ryan Daigle的博客中扯下来的 。 我正在运行最新的Rails 2.3,所以我应该摆脱他们对此的安全问题。
使用Web服务器validation
我也可以使用.htaccess或apache权限实现这一点,但是它使我的服务器配置稍微复杂一些(我使用Chef,并且需要不同的apache配置用于登台/生产)。
现在我有第一个实现和工作,你看到它的问题吗? 我错过了一些明显的东西吗 提前致谢!
碰到这个,以帮助其他人,像我自己读到这个,然后再解决类似但更清洁的解决方案。
# config/environments/staging.rb MyApp::Application.configure do config.middleware.insert_after(::Rack::Lock, "::Rack::Auth::Basic", "Staging") do |u, p| [u, p] == ['username', 'password'] end #... other config end
我写了一篇关于它的短篇博文 。
如果要使用多阶段环境进行部署,并且拥有生产环境和暂存环境,则只需将这些行添加到config / environments / staging.rb
MyApp::Application.configure do # RESTRICTING ACCESS TO THE STAGE ENVIRONMENT config.middleware.insert_before(::Rack::Runtime, "::Rack::Auth::Basic", "Staging") do |u, p| u == 'tester' && p == 'secret' end ... end
通过这样做,您不需要配置Apache。
我使用Ruby 2和Rails 4,它就像一个魅力!
我会使用http基本身份validation,我发现它没有固有的问题。