apache用戶認證

apache用戶認證

常常上網的讀者會遇到這種狀況:訪問一些網站的某些資源時,瀏覽器彈出一個對話框,要求輸入用戶名和密碼來獲取對資源的訪問。這就是用戶認證的一種技術。用戶認證是保護網絡系統資源的第一道防線,它控制着全部登陸並檢查訪問用戶的合法性,其目標是僅 讓合法用戶以合法的權限訪問網絡系統的資源。基本的用戶認證技術是「用戶名+密碼」。php

用戶認證網頁測試:
markhtml

[root@chy ~]# vi /usr/local/apache2.4/conf/extra/httpd-vhosts.conf 
(在虛擬主機配置文件裏面增長以下的配置)
<VirtualHost *:80>
    DocumentRoot "/data/wwwroot/111.com"
    ServerName 111.com
    ServerAlias www.exaple.com
    <Directory /data/wwwroot/111.com>
      AllowOverride AuthConfig
      AuthName "111.com user auth"
        AuthType Basic
        AuthUserFile /data/.htpasswd
        require valid-user
    </Directory>
    #ErrorLog "logs/dummy-host2.example.com-error_log"
    #CustomLog "logs/dummy-host2.example.com-access_log" common
</VirtualHost>
增長的用戶認證具體配置與具體的詳細說明
    <Directory /data/wwwroot/www.111.com> //指定認證的目錄
        AllowOverride AuthConfig //這個至關於打開認證的開關
        AuthName "111.com user auth" //自定義認證的名字,做用不大
        AuthType Basic //認證的類型,通常爲Basic,其餘類型阿銘沒用過
        AuthUserFile /data/.htpasswd  //指定密碼文件所在位置
        require valid-user //指定須要認證的用戶爲所有可用用戶
    </Directory>
[root@chy ~]# /usr/local/apache2.4/bin/htpasswd -c -m /data/.htpasswd aming
New password: 
Re-type new password: 
Adding password for user aming
(/usr/local/apache2.4/bin/htpasswd -c -m /data/.htpasswd aming增長用戶,並建立密碼)
[root@chy ~]# cat /data/.htpasswd 
aming:$apr1$jmWSqWJz$JSzgTrvvhpzg.KcJwZhaW/
(而且查看密碼)
[root@chy ~]# /usr/local/apache2.4/bin/htpasswd  -m /data/.htpasswd chy
New password: 
Re-type new password: 
Adding password for user chy
(當第二次再次建立用戶名是就不須要加-c直接後面跟建立密碼的格式便可)
[root@chy ~]# /usr/local/apache2.4/bin/apachectl graceful
從新加載後,開始作測試。
[root@chy ~]# curl -x127.0.0.1:80 111.com -I
HTTP/1.1 401 Unauthorized
Date: Sun, 30 Jul 2017 20:28:26 GMT
Server: Apache/2.4.27 (Unix) PHP/7.1.6
WWW-Authenticate: Basic realm="111.com user auth"
Content-Type: text/html; charset=iso-8859-1
(當出現401時
說明訪問的內容是須要作驗證的)
用網頁測試結果如截圖1
用curl測試以下:
[root@chy ~]# curl -x127.0.0.1:80 -uaming:123456789 111.com -I
HTTP/1.1 200 OK
Date: Sun, 30 Jul 2017 20:58:49 GMT
Server: Apache/2.4.27 (Unix) PHP/7.1.6
X-Powered-By: PHP/7.1.6
Content-Type: text/html; charset=UTF-8

以下是針對單個的文件進行用戶認證:apache

[root@chy ~]# vi /usr/local/apache2.4/conf/extra/httpd-vhosts.conf 
<VirtualHost *:80>
    DocumentRoot "/data/wwwroot/111.com"
    ServerName 111.com
    ServerAlias www.exaple.com
    #<Directory /data/wwwroot/111.com>
     <filesMatch 123.php>
      AllowOverride AuthConfig
      AuthName "111.com user auth"
        AuthType Basic
        AuthUserFile /data/.htpasswd
        require valid-user
      </FilesMatch>
      #</Directory>
    #ErrorLog "logs/dummy-host2.example.com-error_log"
    #CustomLog "logs/dummy-host2.example.com-access_log" common
</VirtualHost>
如上是針對單個文件的用戶認證,以下是詳細說明
<VirtualHost *:80>
    DocumentRoot "/data/wwwroot/111.com"
    ServerName 111.com
    <FilesMatch admin.php>(這裏將以前的</Directory>註釋掉換成filesMatch ,並指定須要用戶認證的文件夾)
        AllowOverride AuthConfig
        AuthName "111.com user auth"
        AuthType Basic
        AuthUserFile /data/.htpasswd
        require valid-user
    </FilesMatch>
</VirtualHost>
[root@chy ~]# vim /data/wwwroot/111.com/123.php
<?php
echo "chyloveff";
php?>
(在網站裏面編輯一個123.php文件)
curl測試結果:
[root@chy ~]# curl -x127.0.0.1:80 111.com/123.php -I
HTTP/1.1 401 Unauthorized
Date: Sun, 30 Jul 2017 21:29:51 GMT
Server: Apache/2.4.27 (Unix) PHP/7.1.6
WWW-Authenticate: Basic realm="111.com user auth"
Content-Type: text/html; charset=iso-8859-1
(如上是不加用戶與密碼訪問123.php的結果,以下是加了用戶名與密碼訪問的結果
[root@chy ~]# curl -x127.0.0.1:80  -uaming:123456789 111.com/123.php -I
HTTP/1.1 200 OK
Date: Sun, 30 Jul 2017 21:30:55 GMT
Server: Apache/2.4.27 (Unix) PHP/7.1.6
X-Powered-By: PHP/7.1.6
Content-Type: text/html; charset=UTF-8
相關文章
相關標籤/搜索