×
新网 > 虚机资讯 > 正文

Apache用户认证

  • 作者:未知
  • 来源:
  • 2018-05-02 16:42:17

httpd的用户认证目录 vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf //把123.com那个虚拟主机编辑成如下内容 DocumentRoot \"/data/wwwroot/www.123.com\" ServerName www.123.com

httpd的用户认证目录 vim /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>

5895d477-f76e-4df0-9ab4-6cfe1bce80ce.jpg

</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 //状态码为200

还可以针对单个文件进行认证 <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> ## 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.conf

在文件中编辑配置文件,将以下内容添加到第二段代码中——>因为第一个虚拟主机是默认虚拟主机 <Directory /data/wwwroot/www.111.com> //指定认证的目录 AllowOverride AuthConfig //这个相当于打开认证的开关,如果没有这一行,那就相当于没有开启 AuthName "111.com user auth" //自定义认证的名字,作用不大 AuthType Basic //认证的类型,一般为Basic,其他类型几乎没用过 AuthUserFile /data/.htpasswd //指定密码文件所在位置——>这里需要指定一个用户名的密码文件 require valid-user //指定需要认证的用户为全部可用用户 </Directory>

更改完的代码 <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>

然后保存退出

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 ~]#

- 若是提示错误,那么先查看文件是否生成,然后查看文件内容,再去排查其他错误 3. 查看 /data/.htpasswd 文件内容 - 能看到文件中有一行,以 : 冒号为分割。左边是用户名,右边是MD5加密的密码 - 因为是用 -m 指定了加密的类型

[root@hf-01 ~]# cat /data/.htpasswd hanfeng:$apr1$DAYH22/X$YbawXM95jlmckPykpfn3u/ [root@hf-01 ~]#

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

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

<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/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 ~]# ```

  • 相关专题

免责声明:本文内容由互联网用户自发贡献自行上传,本网站不拥有所有权,也不承认相关法律责任。如果您发现本社区中有涉嫌抄袭的内容,请发送邮件至:operations@xinnet.com进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。

免费咨询获取折扣

Loading