LoginSignup
1
0

ApacheでのBasic認証

Last updated at Posted at 2024-01-02

目次

  1. はじめに
  2. パスワードファイル作成
  3. グループファイル作成
  4. Basic認証設定
  5. 動作

1. はじめに

Basic認証の動作検証のメモ。
 ※参考:Basic認証(Apache)

.htaccessファイルでBasic認証を設定する場合、httpd.confで該当ディレクトリのAllowOverride(デフォルトはNone)をALLAuthConfigにする必要がある。
しかし、他のディレクトリで稼働中のコンテンツに対して影響を与えたくないため、
httpd.confのDirectoryディレクティブ内で記述する。

2. パスワードファイル作成

htpasswdコマンドで、パスワードファイルにユーザ名とパスワードを保存する。
コマンド実行後には2回パスワードの入力が要求される。

パスワードファイルを新規作成

htpasswd -c 保存先 ユーザ名
-------------------------------------------------
htpasswd -c /usr/local/apache/passwd/passwords user1

パスワードファイルからユーザを削除

htpasswd -D 保存先 ユーザ名
-------------------------------------------------
htpasswd -D /usr/local/apache/passwd/passwords user1

3. グループファイル作成

複数のユーザを認証させる場合はグループを使用すると便利。
(承認させるユーザを変更する際にApacheを再起動する必要がない)
以下の内容をテキストファイル/usr/local/apache/passwd/groupsに記述する。

グループ名: ユーザ名 ユーザ名 ...
-------------------------------------------------
Group1: user1 user2

4. Basic認証設定

以下の内容をhttpd.confDirectoryディレクティブ内で記述する。

ユーザを認証させる場合

AuthType Basic
AuthName "認証名"
AuthBasicProvider file
AuthUserFile パスワードファイルのパス
Require user ユーザ名
-------------------------------------------------
AuthType Basic
AuthName "Restricted Files"
AuthBasicProvider file
AuthUserFile /usr/local/apache/passwd/passwords
Require user user1

グループを認証させる場合

AuthType Basic
AuthName "認証名"
AuthBasicProvider file
AuthUserFile パスワードファイルのパス
Require group グループ名
-------------------------------------------------
AuthType Basic
AuthName "Restricted Files"
AuthBasicProvider file
AuthUserFile /usr/local/apache/passwd/passwords
AuthGroupFile /usr/local/apache/passwd/groups
Require group Group1

5. 動作

ブラウザからアクセス

ユーザ名とパスワードの入力を要求される。
image.png
・認証失敗
image.png
・認証成功
image.png

コマンドラインからアクセス

curl -u ユーザ名:パスワード URL
-------------------------------------------------
curl -u user1:password http://localhost/basictest/index.html

コマンドラインからファイルをダウンロード

wget URL --http-user="ユーザ名" --http-password="パスワード" -O ファイルの保存名
wget http://localhost/basictest/downloadtest.txt --http-user="user1" --http-password="password" -O downloadresult.txt
1
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
1
0