4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Railsで使用するbasic認証

Posted at

概要

開発環境でRailsアプリを作成している時に、簡単なセキュリティ対策のため、basic認証を行えないか調査しました。その結果を記載します。

詳細

実装

app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
  before_action :basic

  private
  def basic
    authenticate_or_request_with_http_basic do |name, password|
      name == "test" && password == "123456"
    end
  end
end

解説

application_controller.rb
共通のコントローラーの処理を記載するファイル。

before_action :<アクション名>

全てのアクションが実行される前に行う処理を記載する。
また、引数として渡すアクション名はシンボルとして渡す。

  private
  def アクション名
    authenticate_or_request_with_http_basic do |name, password|
      name == "test" && password == "123456"
    end
  end

nameにログインユーザ名、passwordにパスワードを記載する。

4
4
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
4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?