|
apache用户认证
经常上网的读者会遇到这种情况:访问一些网站的某些资源时,浏览器弹出一个对话框,要求输入用户名和密码来获取对资源的访问。这就是用户认证的一种技术。用户认证是保护网络系统资源的第一道防线,它控制着所有登录并检查访问用户的合法性,其目标是仅 让合法用户以合法的权限访问网络系统的资源。基本的用户认证技术是“用户名+密码”。
用户认证网页测试:
[iyunv@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>
[iyunv@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增加用户,并创建密码)
[iyunv@chy ~]# cat /data/.htpasswd
aming:$apr1$jmWSqWJz$JSzgTrvvhpzg.KcJwZhaW/
(并且查看密码)
[iyunv@chy ~]# /usr/local/apache2.4/bin/htpasswd -m /data/.htpasswd chy
New password:
Re-type new password:
Adding password for user chy
(当第二次再次创建用户名是就不需要加-c直接后面跟创建密码的格式即可)
[iyunv@chy ~]# /usr/local/apache2.4/bin/apachectl graceful
重新加载后,开始做测试。
[iyunv@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测试如下:
[iyunv@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
如下是针对单个的文件进行用户认证:
[iyunv@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>
[iyunv@chy ~]# vim /data/wwwroot/111.com/123.php
<?php
echo "chyloveff";
php?>
(在网站里面编辑一个123.php文件)
curl测试结果:
[iyunv@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的结果,如下是加了用户名与密码访问的结果
[iyunv@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
|
|
|
|
|
|
|