LoginSignup
0
0

More than 1 year has passed since last update.

Basic認証をRailsアプリに導入する方法

Posted at

authenticate_or_request_with_http_basicメソッド

ブロック内部でusernameとpasswordを設定し、任意の値を決めて実装します。
下記の例では、usernameに「test」、passwordに「1111」を指定しています。

authenticate_or_request_with_http_basic do |username, password|
  username == 'test' && password == '1111'
end

Basic認証をRailsアプリに導入する

メソッドをコントローラーのprivateに定義し、before_actionで呼び出します。

before_action :basic_auth

private
def basic_auth
  authenticate_or_request_with_http_basic do |username, password|
    username == 'test' && password == '1111'
  end
end

上記の記述によって、アクセスした際にBasic認証が要求されるようになります。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0