【CentOS 7LAMP架構7】,Apache用戶認證#

shallow丿ovephp


httpd的用戶認證

  • vi /usr/local/apache2.4/conf/extra/httpd-vhosts.conf #把123.com虛擬主機編輯爲 <VirtualHost *:80> DocumentRoot "/data/wwwroot/www.123.com ServerName www.123.com <Directory /data/wwwroot/www.123.com> #指定認證的目錄 AllowOverride AuthConfig #這個至關於打開認證的開關 AuthName "123.com user auth" #自定義認證的名字,做用不大 AuthType Basic #認證的類型,通常爲Basic AuthUserFile /data/.htpasswd #指定密碼文件所在位置 require valid-user #指定須要認證的用戶爲所有可用用戶 </Directory> </VirtualHost>
  • /usr/local/apache2.4/bin/htpasswd -cm /data/.htpasswd user
  • 從新加載配置-t,graceful
  • 綁定hosts,瀏覽器測試
  • curl -x 127.0.0.1:80 www.123.com #狀態碼爲401
  • curl -x 127.0.0.1:80 -u user:passwd www.123.com #狀態碼爲200

將第二個虛擬主機修改成以下html

[root@localhost ~]# vi /usr/local/apache2.4/conf/httpd.conf
     31 <VirtualHost *:80>
     32     DocumentRoot "/data/wwwroot/111.com"
     33     ServerName 111.com
     34     ServerAlias www.example.com
     35     <Directory /data/wwwroot/111.com>
     36         AllowOverride AuthConfig
     37         AuthName "111.com user auth"
     38         AuthType Basic
     39         AuthUserFile /data/.htpasswd
     40         require valid-user
     41     </Directory>
     42     ErrorLog "logs/111.com-error_log"
     43     CustomLog "logs/111.com-access_log" common
     44 </VirtualHost>

-c建立密碼文件apache

[root@localhost ~]# /usr/local/apache2.4/bin/htpasswd -c -m /data/.htpasswd user
New password: 
Re-type new password: 
Adding password for user user

-m爲MD5瀏覽器

[root@localhost ~]# cat /data/.htpasswd 
user:$apr1$nz4nSFEa$xXz28yuiXSuuWb9LLAPzJ0

若要添加用戶,則不須要再次建立-ccurl

[root@localhost ~]# /usr/local/apache2.4/bin/htpasswd -m /data/.htpasswd admin
New password: 
Re-type new password: 
Adding password for user admin
[root@localhost ~]# cat /data/.htpasswd 
user:$apr1$nz4nSFEa$xXz28yuiXSuuWb9LLAPzJ0
admin:$apr1$fb45.Cr9$Wejc/XMUd10Yl3aPhRvJm0
[root@localhost ~]# /usr/local/apache2.4/bin/apachectl -t
Syntax OK
[root@localhost ~]# /usr/local/apache2.4/bin/apachectl graceful

訪問111.comide

[root@localhost ~]# curl -x 127.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>

出現了401,表示訪問頁面須要作用戶驗證測試

[root@localhost ~]# curl -x 127.0.0.1:80 111.com -I
HTTP/1.1 401 Unauthorized
Date: Sat, 04 Nov 2017 04:10:05 GMT
Server: Apache/2.4.29 (Unix) PHP/5.6.30
WWW-Authenticate: Basic realm="111.com user auth"
Content-Type: text/html; charset=iso-8859-1

Windows下在hosts文件添加111.com,而後瀏覽器訪問111.comui

輸入圖片說明

輸入用戶和密碼就能訪問111.com了url

而Linux訪問的用戶認證則須要加-u指定用戶和密碼則就能夠訪問成功code

[root@localhost ~]# curl -x 127.0.0.1:80 111.com -u user:123
hello!111.com
[root@localhost ~]# curl -x 127.0.0.1:80 111.com -u user:123 -I
HTTP/1.1 200 OK
Date: Sat, 04 Nov 2017 04:15:43 GMT
Server: Apache/2.4.29 (Unix) PHP/5.6.30
X-Powered-By: PHP/5.6.30
Content-Type: text/html; charset=UTF-8
  • 針對某一個頁面做用戶認證
  • vi /usr/local/apache2.4/conf/extra/httpd-vhosts.conf <VirtualHost *:80> DocumentRoot "/data/wwwwroot/www.123/com" ServerName www.123.com <FilesMatch user.php> AllowOverride AuthConfig AuthName "111.com user auth" AuthType Basic AuthUserFile /data/.htpasswd require valid-user </FileMatch> </VirtualHost> 針對某一個頁面做用戶認證
31 <VirtualHost *:80>
     32     DocumentRoot "/data/wwwroot/111.com"
     33     ServerName 111.com
     34     ServerAlias www.example.com
     35 #    <Directory /data/wwwroot/111.com>
     36     <FilesMatch user.php>
     37         AllowOverride AuthConfig
     38         AuthName "111.com user auth"
     39         AuthType Basic
     40         AuthUserFile /data/.htpasswd
     41         require valid-user
     42     </FilesMatch>
     43 #    </Directory>
     44     ErrorLog "logs/111.com-error_log"
     45     CustomLog "logs/111.com-access_log" common
     46 </VirtualHost>
[root@localhost ~]# /usr/local/apache2.4/bin/apachectl -t
Syntax OK
[root@localhost ~]# /usr/local/apache2.4/bin/apachectl graceful
[root@localhost ~]# vi /data/wwwroot/111.com/user.php
	<?php
		echo "hello!user"
	?>
[root@localhost ~]# curl -x 127.0.0.1:80 111.com
hello!111.com
[root@localhost ~]# curl -x 127.0.0.1:80 111.com/user.php
<!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@localhost ~]# curl -x 127.0.0.1:80 -u user:123 111.com/user.php
hello!user

而Windows下的瀏覽器訪問也一樣能夠

相關文章
相關標籤/搜索