LAMP-Apache用戶認證
在某些場景下,網站頁面的內容需要特殊授權用戶才能查看。要實現這個功能,需要在Apache上做設置認證用戶。
1、編輯虛擬主機配置
[[email protected] ~]# vi /usr/local/apache2.4/conf/extra/httpd-vhosts.conf <VirtualHost *:80> DocumentRoot "/data/www/abc.com" ServerName abc.com <Directory /data/www/abc.com> ##指定認證的目錄 AllowOverride AuthConfig ##打開認證開關 AuthName "abc.com user auth" ##定義認證的名字 AuthType Basic ##指定認證的類型 AuthUserFile /data/.htpasswd ##指定密碼文件所在的位置 require valid-user ##指定需要認證的用戶 </Directory> </VirtualHost>
2、增加用戶
[[email protected] ~]# /usr/local/apache2.4/bin/htpasswd -c -m /data/.htpasswd juispan New password: ##“-c”=create “-m”=md5 Re-type new password: ##“/data/.htpasswd”=密碼存放路徑 Adding password for user juispan [[email protected] ~]# cat /data/.htpasswd juispan:$apr1$5UVKQ8Ux$8tkRftVA0ueh7qtD6tzlz1
3、檢查重新加載
[[email protected] ~]# /usr/local/apache2.4/bin/apachectl -t Syntax OK [[email protected] ~]# /usr/local/apache2.4/bin/apachectl graceful
4、測試驗證
本機驗證:
[[email protected] ~]# curl -x127.0.0.1:80 abc.com <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"> <html><head> <title>401 Unauthorized</title> ##401 未認證 </head><body> <h1>Unauthorized</h1> <p>This server could not verify that you are authorized to access the document requested. Either you supplied the wrong credentials (e.g., bad password), or your browser doesn‘t understand how to supply the credentials required.</p> </body></html> [[email protected]
遠端驗證:
輸入正確的用戶名口令後即可顯示網頁內容。
如果針對的不是整個目錄,而是單個網頁,可以使用FilesMatch替換Directory,如<FilesMatch 1.php> 。
▎參考配置:
<VirtualHost *:80> DocumentRoot "/data/www/abc.com" ServerName www.abc.com <FilesMatch 1.php> AllowOverride AuthConfig AuthName "abc.com user auth" AuthType Basic AuthUserFile /data/.htpasswd require valid-user </FilesMatch> </VirtualHost>
本文出自 “Gorilla Grodd” 博客,請務必保留此出處http://juispan.blog.51cto.com/943137/1952793
LAMP-Apache用戶認證