0
0

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 3 years have passed since last update.

デプロイしたアプリにBasic認証を導入

Posted at

はじめまして

私は、Herokuでアプリをデプロイした後に公開を制限する為にBasic認証を導入しました。
備忘録として残したいと思います。

Basic認証とは

HTTP通信の規格に備えつけられているユーザー認証の仕組みです。
サーバーとの通信が可能なユーザーとパスワードを予め設定しておき、それに一致したユーザーのみがWebアプリケーションを利用できるようになります。

導入

ターミナルで環境変数を設定します。

% vim ~/.zshrc
# iを押してインサートモードにします。

export BASIC_AUTH_USER='********'
export BASIC_AUTH_PASSWORD='*****'

「escキー」「:wq」で保存して終了します。

% source ~/.zshrc
# パスを通します。

Railsアプリケーション側で設定します。

app/controllers/application_controller.rb

class ApplicationController < ActionController::Base
  before_action :basic_auth


  private

  def basic_auth
    authenticate_or_request_with_http_basic do |username, password|
      username == ENV["BASIC_AUTH_USER"] && password == ENV["BASIC_AUTH_PASSWORD"]  # 環境変数を読み込む記述に変更
    end
  end
end

Heroku上にも環境変数を設定します。

先ほどローカルで設定した環境変数をherokuにも設定します。


% heroku config:set BASIC_AUTH_USER="********"
% heroku config:set BASIC_AUTH_PASSWORD="**********"

これで設定は完了です!

最後に

今回はBasic認証について勉強しました!手軽に通信に制限をかけれますが、絶対安全という訳ではないみたいです。
理由としては、「Base64エンコード」だからです。(データを64種類の文字列だけで表現するエンコーディング方式)
簡単にデコードすることも可能らしいです。
よりセキュリティを安全に使う為には、SSLやTLSも使って暗号化すると強化することができるみたいです。
herokuであればSSLになっているので問題ないと思いますが。

セキュリティに関しても勉強続けていきたいと思います。

ここまで読んでいただきありがとうございました!

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?