CentOS7

Webページパスワード制限(htpasswd)

Webページへのアクセスにユーザー名とパスワードによるアクセス制限をかける。

ここでは、https://hoge.com/secret/ 以下のWebページへユーザー名(hogehogeとする)とパスワード(hogepassとする)によるアクセス制限をかけるようにする。

なお、htpasswdによるユーザー名、パスワード情報はほぼ平文でネットワーク上を流れるため、盗聴に備えてSSL経由でしかアクセスできないようにする。

※Webサーバー、Webサーバー間通信内容暗号化が導入済であること

Webサーバー設定

vi /etc/httpd/conf/httpd.conf ← Webサーバー設定ファイル編集
<Directory "/var/www/html">

#
# Possible values for the Options directive are "None", "All",
# or any combination of:
# Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
#
# Note that "MultiViews" must be named *explicitly* --- "Options All"
# doesn't give it to you.
#
# The Options directive is both complicated and important. Please see
# http://httpd.apache.org/docs-2.0/mod/core.html#options
# for more information.
#
Options Includes ExecCGI FollowSymLinks

#
# AllowOverride controls what directives may be placed in .htaccess files.
# It can be "All", "None", or any combination of the keywords:
# Options FileInfo AuthConfig Limit
#
AllowOverride All ← Allにする(.htaccessを許可)
[root@localhost ~]# systemctl reload httpd ← Webサーバー設定反映

.htpasswdファイル作成

.htpasswdファイルを新規作成する場合

[root@localhost ~]# htpasswd -b -c -m /etc/httpd/conf/.htpasswd hogehoge hogepass ← .htpasswdを作成してユーザーhogehogeを登録する
Adding password for user hogehoge

既存の.htpasswdファイルへユーザーを追加する場合

[root@localhost ~]# htpasswd -b -m /etc/httpd/conf/.htpasswd hogehoge hogepass ← 既存の.htpasswdへユーザーhogehogeを登録する
Adding password for user hogehoge

ユーザー登録確認

[root@localhost ~]# cat /etc/httpd/conf/.htpasswd ← ユーザー登録確認
hogehoge:vYwnFfo59lI/c

Webページパスワード制限確認

テスト用ディレクトリ、ページ作成

[root@localhost ~]# mkdir /var/www/html/secret ← テスト用ディレクトリ作成
[root@localhost ~]# echo test > /var/www/html/secret/index.html ← テスト用ページ作成

.htaccessファイル作成

.htpasswdに登録してある全てのユーザー名で認証できるようにする場合

[root@localhost ~]# vi /var/www/html/secret/.htaccess ← テスト用ディレクトリに.htaccess作成
SSLRequireSSL
AuthUserFile /etc/httpd/conf/.htpasswd
AuthGroupFile /dev/null
AuthName "secret page"
AuthType Basic
require valid-user

.htaccessファイル作成

.htpasswdに登録してある特定のユーザー名(ここでは、認証を許可するユーザー名をhogehogeとする)でのみ認証できるようにする場合

[root@localhost ~]# vi /var/www/html/secret/.htaccess ← テスト用ディレクトリに.htaccess作成
SSLRequireSSL
AuthUserFile /etc/httpd/conf/.htpasswd
AuthGroupFile /dev/null
AuthName "secret page"
AuthType Basic
require user hogehoge ← 認証を許可するユーザー名を指定

ユーザー認証確認

Webページパスワード制限確認後始末

[root@localhost ~]# rm -rf /var/www/html/secret/ ← テスト用ディレクトリ削除
[root@localhost ~]# rm -f /etc/httpd/conf/.htpasswd ← テスト用.htpasswd削除

トップ   編集 凍結 差分 履歴 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2018-08-23 (木) 18:12:50