配置Apache服務(wù)的digest摘要認(rèn)證
TsengYia#126.com 2008-01-04
http://selinux.cublog.cn
################################################################
系統(tǒng)環(huán)境:RHEL5 [ 2.6.18-8.el5xen ]
軟件環(huán)境:
httpd-2.2.3-6.el5
目標(biāo)功能:
針對Apache網(wǎng)站目錄或URL位置實現(xiàn)用戶訪問認(rèn)證(basic基本認(rèn)證、digest摘要認(rèn)證)。
basic認(rèn)證、digest認(rèn)證任選一種即可。
注:以盡量減少對Web服務(wù)器性能的影響為出發(fā)點,僅在需要認(rèn)證的目錄(Directory)或位置(Location)范圍作認(rèn)證即可,避免在層次結(jié)構(gòu)較復(fù)雜的目錄(如網(wǎng)站根目錄)使用.htaccess文件;如果用戶認(rèn)證的配置不多,可以直接在httpd.conf文件中相關(guān)區(qū)段進行配置,無需使用單獨的.htaccess文件。
################################################################
一、安裝httpd軟件包
本處以RHEL5光盤自帶的rpm包安裝,過程略。
在httpd.conf文件中,幾個主要認(rèn)證模塊配置如下(默認(rèn)已配置):
LoadModule auth_basic_module modules/mod_auth_basic.so
LoadModule auth_digest_module modules/mod_auth_digest.so
LoadModule authn_file_module modules/mod_authn_file.so
LoadModule authn_dbm_module modules/mod_authn_dbm.so
LoadModule authn_default_module modules/mod_authn_default.so
LoadModule authz_user_module modules/mod_authz_user.so
LoadModule authz_dbm_module modules/mod_authz_dbm.so
LoadModule authz_default_module modules/mod_authz_default.so
二、修改httpd.conf文件,添加對目錄或位置的用戶驗證配置
1、創(chuàng)建網(wǎng)站測試文件
# mkdir -p /var/www/html/auth
# echo "Root Directory." > /var/www/html/index.html
# echo "Auth Directory." > /var/www/html/auth/index.html
2、修改/etc/httpd/conf/httpd.conf文件,確認(rèn)修改或添加以下內(nèi)容:
DocumentRoot "/var/www/html"
Options Indexes
AllowOverride AuthConfig #//允許讀取.htaccess文件中的認(rèn)證配置(AuthConfig),其他目錄應(yīng)按默認(rèn)值None
Order allow,deny
Allow from all
DirectoryIndex index.html index.php
3、啟動httpd服務(wù)
# /etc/init.d/httpd start
# chkconfig --level 35 httpd on
三、配置基本認(rèn)證(basic)
方式一:使用文本格式用戶數(shù)據(jù)庫
1、創(chuàng)建認(rèn)證用戶數(shù)據(jù)庫文件(文本格式)
# htpasswd -c /etc/httpd/conf/users_txt.pwd admin #//創(chuàng)建文本格式用戶文件,同時添加用戶admin
# htpasswd /etc/httpd/conf/users_txt.pwd jerry #//在數(shù)據(jù)庫文件中添加用戶jerry
2、創(chuàng)建.htaccess配置文件
# vi /var/www/html/auth/.htaccess
AuthName "Private Contents."
AuthType basic
AuthUserFile /etc/httpd/conf/users_txt.pwd
Require admin jerry #//允許用戶admin、jerry通過認(rèn)證后訪問,若用戶較多,可以改為“Require valid-user”
3、測試
在客戶端瀏覽器(必要時清空緩存)訪問http://Server_IP/auth/,以admin或jerry用戶進行認(rèn)證訪問
方式二:使用dbm格式用戶數(shù)據(jù)庫(若用戶較多,可采用dbm用戶數(shù)據(jù)庫以加快檢索速度)
1、創(chuàng)建認(rèn)證用戶數(shù)據(jù)庫文件(Berkeley DB格式)
# htdbm -TDB -c /etc/httpd/conf/users_dbm.db kitty #//創(chuàng)建DB格式(其他格式參考man)用戶文件,同時添加用戶kitty
2、修改.htaccess配置文件
# vi /var/www/html/auth/.htaccess
AuthName "Private Contents."
AuthType basic
AuthBasicProvider dbm
AuthDBMType DB
AuthDBMUserFile "/etc/httpd/conf/users_dbm.db"
Require kitty
3、測試
在客戶端瀏覽器(必要時清空緩存)訪問http://Server_IP/auth/,以kitty用戶進行認(rèn)證訪問
四、配置摘要認(rèn)證(digest)
1、創(chuàng)建digest認(rèn)證用戶數(shù)據(jù)庫文件(文本格式) #//digest認(rèn)證貌似無法使用dbm用戶文件
# htdigest -c /etc/httpd/conf/users_digest.pwd "Private Contents." kenthy
2、修改.htaccess配置文件
AuthName "Private Contents."
AuthType digest
#AuthDigestDomain /auth/ #//在目錄區(qū)段(Directory)配置digest認(rèn)證時,此行可有可無
AuthUserFile /etc/httpd/conf/users_digest.pwd
Require kenthy
3、測試
在客戶端瀏覽器(必要時清空緩存)訪問http://Server_IP/auth/,以kenthy用戶進行認(rèn)證訪問
******************************** End *********************************
本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請
點擊舉報。