【问题标题】:Rails 3 http_basic_authenticate_with only in production environment?Rails 3 http_basic_authenticate_with 仅在生产环境中?
【发布时间】:2013-04-23 15:24:20
【问题描述】:

有没有办法只使用

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

服务器何时以生产模式运行?

【问题讨论】:

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


    【解决方案1】:

    是的,试试:

    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? 的粉丝,因为无论应用程序在哪个服务器上运行,它最终都是正确的。我更喜欢将用户名/密码放在环境变量中,并根据是否设置来决定进行身份验证。但我意识到这超出了这个问题的范围。 :)
    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:
      authenticate_or_request_with_http_basic do |username, password|
        username == "user" && password == "secret"
      end if Rails.env.production?
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-04-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-01-05
        • 2011-04-14
        相关资源
        最近更新 更多