</VirtualHost> /usr/local/apache2.4/bin/htpasswd -cm /data/.htpasswd aming - 從新加載配置-t , graceful - 綁定hosts,瀏覽器測試 - curl -x127.0.0.1:80 www.123.com //狀態碼爲401 - curl -x127.0.0.1:80 -uaming:passwd www.123.com //狀態碼爲200php
</VirtualHost> ## httpd的用戶認證 - 瀏覽器在打開一個網站,什麼頁面都不現實,只彈出一個對話框,讓你輸入用戶名和密碼,只有輸入正確才能訪問網站的內容 - 需求 - abc.com這個網站訪問的時候,不能直接訪問,必須輸入用戶名和密碼,驗證經過以後才能訪問網站內容——>這樣作的目的是增長安全性,可是劣勢是用戶體驗不好,由於每一個人用訪問網站都必須輸入用戶名和密碼 1. 編輯vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf 文件 ``` [root@hf-01 ~]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.confhtml
在文件中編輯配置文件,將如下內容添加到第二段代碼中——>由於第一個虛擬主機是默認虛擬主機 <Directory /data/wwwroot/www.111.com> //指定認證的目錄 AllowOverride AuthConfig //這個至關於打開認證的開關,若是沒有這一行,那就至關於沒有開啓 AuthName "111.com user auth" //自定義認證的名字,做用不大 AuthType Basic //認證的類型,通常爲Basic,其餘類型幾乎沒用過 AuthUserFile /data/.htpasswd //指定密碼文件所在位置——>這裏須要指定一個用戶名的密碼文件 require valid-user //指定須要認證的用戶爲所有可用用戶 </Directory>apache
更改完的代碼 <VirtualHost *:80> DocumentRoot "/data/wwwroot/abc.com" ServerName abc.com ServerAlias www.abc.com www.123.com ErrorLog "logs/abc.com-error_log" CustomLog "logs/abc.com-access_log" common </VirtualHost>vim
<VirtualHost *:80> DocumentRoot "/data/wwwroot/111.com" ServerName 111.com ServerAlias www.example.com
<Directory /data/wwwroot/111.com> AllowOverride AuthConfig AuthName "111.com user auth" AuthType Basic AuthUserFile /data/.htpasswd
require valid-user </Directory> ErrorLog "logs/111.com-error_log" CustomLog "logs/111.com-access_log" common </VirtualHost>瀏覽器
而後保存退出安全
2. 用apache自帶的命令htpasswd建立 - /usr/local/apache2.4/bin/htpasswd -c -m /data/.htpasswd hanfeng - -c參數,就是建立 -m參數,使用MD5加密 - /data/.htpasswd,指定密碼文件所在位置 - useradd hanfeng,(這裏的useradd 是不須要寫的,直接寫用戶名就行)加一個用戶名(密碼爲hanfeng)
[root@hf-01 ~]# /usr/local/apache2.4/bin/htpasswd -c -m /data/.htpasswd hanfeng New password: Re-type new password: Adding password for user hanfeng [root@hf-01 ~]#curl
- 如果提示錯誤,那麼先查看文件是否生成,而後查看文件內容,再去排查其餘錯誤 3. 查看 /data/.htpasswd 文件內容 - 能看到文件中有一行,以 : 冒號爲分割。左邊是用戶名,右邊是MD5加密的密碼 - 由於是用 -m 指定了加密的類型
[root@hf-01 ~]# cat /data/.htpasswd hanfeng:$apr1$DAYH22/X$YbawXM95jlmckPykpfn3u/ [root@hf-01 ~]#ide
4. 再增長zhangsan用戶,就不須要去 -c參數 建立了,由於已經建立過了(密碼爲123456)
[root@hf-01 ~]# /usr/local/apache2.4/bin/htpasswd -m /data/.htpasswd zhangsan New password: Re-type new password: Adding password for user zhangsan [root@hf-01 ~]#測試
5. 查看文件內容,會發現又增長了一行密碼
[root@hf-01 ~]# cat /data/.htpasswd hanfeng:$apr1$DAYH22/X$YbawXM95jlmckPykpfn3u/ zhangsan:$apr1$NC7LZ5JQ$FBZNAIzjCTwKheTiWrtlT. [root@hf-01 ~]#網站
6. 查看配置文件是否有錯誤,並從新加載配置文件
[root@hf-01 ~]# /usr/local/apache2.4/bin/apachectl -t Syntax OK [root@hf-01 ~]# /usr/local/apache2.4/bin/apachectl graceful [root@hf-01 ~]#
7. 測試,訪問111.com的時候,會提示401狀態碼 - 401狀態碼,說明訪問的內容須要作用戶認證
[root@hf-01 ~]# curl -x127.0.0.1:80 111.com
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head> <title>401 Unauthorized</title> </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> [root@hf-01 ~]# curl -x127.0.0.1:80 111.com -I HTTP/1.1 401 Unauthorized Date: Wed, 20 Dec 2017 15:34:22 GMT Server: Apache/2.4.29 (Unix) PHP/7.1.6 WWW-Authenticate: Basic realm="111.com user auth" Content-Type: text/html; charset=iso-8859-1
[root@hf-01 ~]#
8. 一樣能夠在瀏覽器中輸入111.com——>前提是要先在物理機中hosts文件定義111.com
192.168.202.130 www.abc.com www.123.com 111.com
9. 會發現須要輸入用戶名,和密碼 ![輸入圖片說明](https://static.oschina.net/uploads/img/201712/20154417_RfRv.jpg "瀏覽器訪問111.com,會提示輸入密碼") 10. 在輸入用戶名和密碼 ![輸入圖片說明](https://static.oschina.net/uploads/img/201712/20155044_Duav.jpg "輸入驗證碼和密碼") 11. 會發現正常訪問到頁面了 ![輸入圖片說明](https://static.oschina.net/uploads/img/201712/20155247_rti9.jpg "正常訪問到頁面內容") 12. 這個就是用戶認證 ### curl輸入用戶名和密碼 - curl -x127.0.0.1:80 -uhanfeng:hanfeng 111.com -I - -u參數,而後加用戶名,再:冒號密碼
[root@hf-01 ~]# curl -x127.0.0.1:80 -uhanfeng:hanfeng 111.com -I HTTP/1.1 200 OK Date: Wed, 20 Dec 2017 15:57:06 GMT Server: Apache/2.4.29 (Unix) PHP/7.1.6 X-Powered-By: PHP/7.1.6 Content-Type: text/html; charset=UTF-8
[root@hf-01 ~]#
- 這時會發現狀態碼改變了,狀態碼變成了200(200即爲正常) - 如果輸錯密碼,那麼狀態碼又會變成401
[root@hf-01 ~]# curl -x127.0.0.1:80 -uhanfeng:feng 111.com -I HTTP/1.1 401 Unauthorized Date: Wed, 20 Dec 2017 15:59:16 GMT Server: Apache/2.4.29 (Unix) PHP/7.1.6 WWW-Authenticate: Basic realm="111.com user auth" Content-Type: text/html; charset=iso-8859-1
[root@hf-01 ~]#
## 針對某一個訪問的進行認證 - 針對 admin.php文件 只有打開這個文件纔會執行下面的操做
<VirtualHost *:80> DocumentRoot "/data/wwwroot/www.123.com" ServerName www.123.com <FilesMatch admin.php>
AllowOverride AuthConfig AuthName "123.com user auth" AuthType Basic AuthUserFile /data/.htpasswd require valid-user </FilesMatch> </VirtualHost>
- 這裏和前面的用戶認證惟一不一樣的就是,使用的是 FilesMatch ,當訪問的文件匹配到admin.php的時候,它纔去執行如下的配置,而前面的用戶認證使用的Directory,指定了一個目錄,只要是這個目錄下面的都會去認證,這裏是FilesMatch ,匹配文件的 1. 更改配置文件,註釋掉Directory,去使用FilesMatch - vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf
[root@hf-01 ~]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf
未更改前 <VirtualHost *:80> DocumentRoot "/data/wwwroot/abc.com" ServerName abc.com ServerAlias www.abc.com www.123.com ErrorLog "logs/abc.com-error_log" CustomLog "logs/abc.com-access_log" common </VirtualHost>
<VirtualHost *:80> DocumentRoot "/data/wwwroot/111.com" ServerName 111.com ServerAlias www.example.com <Directory /data/wwwroot/111.com> AllowOverride AuthConfig AuthName "111.com user auth" AuthType Basic AuthUserFile /data/.htpasswd require valid-user </Directory> ErrorLog "logs/111.com-error_log" CustomLog "logs/111.com-access_log" common </VirtualHost>
更改後 <VirtualHost *:80> DocumentRoot "/data/wwwroot/abc.com" ServerName abc.com ServerAlias www.abc.com www.123.com ErrorLog "logs/abc.com-error_log" CustomLog "logs/abc.com-access_log" common </VirtualHost>
<VirtualHost *:80> DocumentRoot "/data/wwwroot/111.com" ServerName 111.com ServerAlias www.example.com
<FilesMatch 123.php> AllowOverride AuthConfig AuthName "111.com user auth" AuthType Basic AuthUserFile /data/.htpasswd require valid-user </FilesMatch> #</Directory> ErrorLog "logs/111.com-error_log" CustomLog "logs/111.com-access_log" common
</VirtualHost>
並保存退出
2. 檢查配置文件是否存在語法錯誤,並從新加載配置文件
[root@hf-01 ~]# /usr/local/apache2.4/bin/apachectl -t Syntax OK [root@hf-01 ~]# /usr/local/apache2.4/bin/apachectl graceful [root@hf-01 ~]#
3. 編輯123.php文件
[root@hf-01 ~]# vim /data/wwwroot/111.com/123.php
在配置文件中寫入
<?php echo "123.php"; 並保存退出 ``` 4. 檢查是否能訪問網站,這裏會看到不加-u 也能訪問到網站,狀態碼也是200,而不是401了 ``` [root@hf-01 ~]# curl -x127.0.0.1:80 -uhanfeng:feng 111.com 111.com[root@hf-01 ~]# [root@hf-01 ~]# curl -x127.0.0.1:80 111.com 111.com[root@hf-01 ~]# [root@hf-01 ~]# curl -x127.0.0.1:80 111.com -I HTTP/1.1 200 OK Date: Wed, 20 Dec 2017 16:24:13 GMT Server: Apache/2.4.29 (Unix) PHP/7.1.6 X-Powered-By: PHP/7.1.6 Content-Type: text/html; charset=UTF-8 [root@hf-01 ~]# ``` 5. 可是在訪問123.php的時候,會提示401,這是由於針對123.php作了一個限制 ``` [root@hf-01 ~]# curl -x127.0.0.1:80 111.com/123.php -I HTTP/1.1 401 Unauthorized Date: Wed, 20 Dec 2017 16:25:42 GMT Server: Apache/2.4.29 (Unix) PHP/7.1.6 WWW-Authenticate: Basic realm="111.com user auth" Content-Type: text/html; charset=iso-8859-1 [root@hf-01 ~]# ``` 6. 這時候-u指定用戶名和密碼,就能夠訪問123.php了 ``` [root@hf-01 ~]# curl -x127.0.0.1:80 -uhanfeng:hanfeng 111.com/123.php -I HTTP/1.1 200 OK Date: Wed, 20 Dec 2017 16:27:11 GMT Server: Apache/2.4.29 (Unix) PHP/7.1.6 X-Powered-By: PHP/7.1.6 Content-Type: text/html; charset=UTF-8 [root@hf-01 ~]# curl -x127.0.0.1:80 -uhanfeng:hanfeng 111.com/123.php 123.php[root@hf-01 ~]# ```