博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
1.8 httpd的用户认证
阅读量:7008 次
发布时间:2019-06-27

本文共 4173 字,大约阅读时间需要 13 分钟。

hot3.png

1.8 httpd的用户认证

  • vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf //把111.com那个虚拟主机添加如下内容:

  <VirtualHost *:80>

   DocumentRoot "/data/wwwroot/111.com"
   ServerName www.111.com
   <Directory /data/wwwroot/111.com> //指定认证的目录
    AllowOverride AuthConfig //这个相当于打开认证的开关
    AuthName "111.com user auth" //自定义认证的名字,作用不大
    AuthType Basic //认证的类型,一般为Basic,其他类型我没用过
    AuthUserFile /data/.htpasswd //指定密码文件所在位置
    require valid-user //指定需要认证的用户为全部可用用户
   </Directory>
  </VirtualHost>

  • /usr/local/apache2.4/bin/htpasswd -c -m /data/.htpasswd aming //创建密码文件
  • 重新加载配置-t , graceful
  • 绑定hosts,浏览器测试
  • curl -x127.0.0.1:80 www.111.com //状态码为401
  • curl -x127.0.0.1:80 -uaming:passwd www.111.com //状态码为200
[root@Dasoncheng ~]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf……
# ServerAdmin webmaster@dummy-host.example.com DocumentRoot "/data/wwwroot/abc.com" ServerName abc.com ServerAlias www.abc.com 123.com ErrorLog "logs/abc.com-error_log" CustomLog "logs/abc.com-access_log" common
# ServerAdmin webmaster@dummy-host2.example.com DocumentRoot "/data/wwwroot/111.com" ServerName www.111.com ServerAlias 111.com
AllowOverride AuthConfig AuthName "111.com user auth" AuthType Basic AuthUserFile /data/.htpasswd require valid-user
ErrorLog "logs/111.com-error_log" CustomLog "logs/111.com-access_log" common
[root@Dasoncheng ~]# /usr/local/apache2.4/bin/htpasswd -c -m /data/.htpasswd zhangsanNew password: Re-type new password: Adding password for user zhangsan[root@Dasoncheng ~]# /usr/local/apache2.4/bin/apachectl -tSyntax OK[root@Dasoncheng ~]# /usr/local/apache2.4/bin/apachectl graceful

测试访问:

[root@Dasoncheng ~]# curl -x192.168.60.11:80 111.com 401 Unauthorized

Unauthorized

This server could not verify that youare authorized to access the documentrequested. Either you supplied the wrongcredentials (e.g., bad password), or yourbrowser doesn't understand how to supplythe credentials required.

[root@Dasoncheng ~]# curl -x192.168.60.11:80 111.com -IHTTP/1.1 401 Unauthorized ##遇到的第一个提示符401,需要验证账号;Date: Fri, 23 Feb 2018 06:31:34 GMTServer: Apache/2.4.27 (Unix) PHP/5.6.30WWW-Authenticate: Basic realm="111.com user auth"Content-Type: text/html; charset=iso-8859-1[root@Dasoncheng ~]# curl -x192.168.60.11:80 111.com -uzhangsan:p@ssw0rd111.com[root@Dasoncheng ~]#

mark

1.8.2 针对单个页面进行验证:

<VirtualHost *:80>

 DocumentRoot "/data/wwwroot/111.com"
 ServerName www.111.com
 <FilesMatch admin.php>
  AllowOverride AuthConfig
  AuthName "111.com user auth"
  AuthType Basic
  AuthUserFile /data/.htpasswd
  require valid-user
 </FilesMatch>
</VirtualHost>

[root@Dasoncheng ~]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf
# ServerAdmin webmaster@dummy-host2.example.com DocumentRoot "/data/wwwroot/111.com" ServerName www.111.com ServerAlias 111.com#
AllowOverride AuthConfig AuthName "Please pass the auth for this page" AuthType Basic AuthUserFile /data/.htpasswd require valid-user
#
ErrorLog "logs/111.com-error_log" CustomLog "logs/111.com-access_log" common
[root@Dasoncheng ~]# /usr/local/apache2.4/bin/apachectl -tSyntax OK[root@Dasoncheng ~]# /usr/local/apache2.4/bin/apachectl graceful[root@Dasoncheng ~]# vim /data/wwwroot/111.com/admin.php[root@Dasoncheng ~]# cat /data/wwwroot/111.com/admin.php

测试访问admin页面:

[root@Dasoncheng ~]# cat /etc/hosts127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4::1         localhost localhost.localdomain localhost6 localhost6.localdomain6192.168.60.11 www.abc.com www.111.com[root@Dasoncheng ~]# curl www.111.com111.com[root@Dasoncheng ~]# curl www.111.com/admin.php401 Unauthorized

Unauthorized

This server could not verify that youare authorized to access the documentrequested. Either you supplied the wrongcredentials (e.g., bad password), or yourbrowser doesn't understand how to supplythe credentials required.

[root@Dasoncheng ~]# curl www.111.com/admin.php -uzhangsan:p@ssw0rdWelcome to the page of admin

mark

转载于:https://my.oschina.net/u/3651233/blog/1622952

你可能感兴趣的文章
TCP/IP Troubleshooting
查看>>
js 中使用外部导入的对象
查看>>
windbg 内核调试的时候 对用户态进程下断点
查看>>
分布式系统的事务处理
查看>>
Nginx URL重写规则配置详解
查看>>
NAND闪存供过于求的情况今年会有所好转吗?
查看>>
记录一次测试上线遇到的问题
查看>>
简单实现了一个基于redis的分布式锁,存在bug...
查看>>
HashMap中的一些变量的意义
查看>>
解决MyBatis的Mapper XML错误,系统起不来,也不报错问题
查看>>
JBPM3 获得待办列表 1:未接收;2:办理中;3:已办结;4:全部工作
查看>>
zhparser在windows下的编译安装
查看>>
微信开发之网页获取用户信息
查看>>
自定义xml tag
查看>>
Linux下的find命令
查看>>
shiro favicon.ico 404
查看>>
VMware启动Ubuntu时出现错误Cannot open the disk 'xxx.vm...
查看>>
解决 HTML div css 强制 换行 不换行
查看>>
android 相关知识
查看>>
redis key过期事件实现过期提醒
查看>>