[Ruby on Rails] How to Set Basic Authentication for Non-Development Environments

Tadashi Shigeoka ·  Sat, May 5, 2012

In Ruby on Rails 3.2.3, here’s how to configure Basic authentication for production and staging environments while avoiding Basic authentication in development environments.

It’s annoying to enter Basic authentication username and password every time in development environment.

app/controllers/application_controller.rb

class ApplicationController < ActionController::Base
 
  before_filter :password_protected unless Rails.env.development?
 
  protected
 
  def password_protected
    authenticate_or_request_with_http_basic do |username, password|
      username == "spam" && password == "spam_pw"
    end
  end
 
end

Authentication will only be applied when the execution environment is other than “development”. (Username: spam, Password: spam_pw)

That’s all from the Gemba.