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認証が要求されるようになります。