Apache基于用户或组对访问的限制(五)

一、简介

** 如果使用Order或Require只对IP或者主机名做限制的话,这两种都是可变的,所以不够安全,是在客户端进行验证的,而如果想要更安全就需要在服务器端进行验证,就是使用用户名或者组让用户在登录网页时输入密码和用户名来进行登录。
这样虽然保证了网页的安全性,但是却复杂了访问网页的操作所以一般只用在局域网,或者一些只有VIP才可以登录的网页等…**

二、实验部分

实验环境:一台服务器IP为10.0.0.2,一台客户端

实验要求:创建两个用户(Tom、Eily),让Tom可以访问页面,而Eily不可以访问

1.基于用户的限制

1)创建站点目录以及主页文件
[root@localhost ~] mkdir -p /web/admin
[root@localhost ~] echo "<h1>This is the admin file</h1>" > /web/admin/index.html
[root@localhost ~] cat /web/admin/index.html 
	<h1>This is the admin file</h1>

2)创建虚拟用户文件
#使用htpasswd创建一个记录虚拟用户文件以及密码的文件
#虚拟用户:该用户不与系统用户发生关系,只是作用于httpd服务中的用户
[root@localhost ~] htpasswd -c /etc/httpd/conf.d/.htuser Eily
#-c创建一个虚拟用户文件
	New password: 
	Re-type new password: 
	Adding password for user Eily
[root@localhost ~] htpasswd  /etc/httpd/conf.d/.htuser Tom
	New password: 
	Re-type new password: 
	Adding password for user Tom
[root@localhost ~] cat /etc/httpd/conf.d/.htuser 
	Eily:$apr1$xZGm6w9Y$0yDdHIE6S5obZ4soLPK1w1
	Tom:$apr1$n2H2Hxb1$m/6rDiNbKZHWK563Ynkd70

3)发布站点并编辑虚拟用户权限
[root@localhost ~] vim /etc/httpd/conf.d/htadmin.conf 
添加:
	DocumentRoot "/web"
	<Directory "/web/admin">
	        Options None
	        AllowOverride None
	        AuthType Basic
	        #使用Basic加密
	        AuthUserFile "/etc/httpd/conf.d/.htuser"
	        #虚拟用户文件的路径
	        AuthName "This is AdminFile"
	        #用户在登录网页时的提示信息
	        Require user Tom
	        #使用Require允许Tom进行访问
	        #Require valid-user			——允许所有用户访问
	</Directory>
[root@localhost ~] systemctl restart httpd

4)使用用户Tom登录,而Eily是没有权限登录的
在这里插入图片描述
在这里插入图片描述
登录成功!!!

2.基于组的限制

#基于组的限制只需要更改虚拟用户文件以及发布站点的字段即可
根据上面的基于用户限制的字段更改后如下

[root@localhost ~] vim /etc/httpd/conf.d/.htgroup
	admingroup:Tom Eily
#这是将Tom于Eily同时加入到admingroup组中

[root@localhost ~] vim /etc/httpd/conf.d/htadmin.conf 
DocumentRoot "/web"
<Directory "/web/admin">
        Options None
        AllowOverride None
        AuthType Basic
        AuthGroupFile "/etc/httpd/conf.d/.htgroup"
        #定义group的组文件路径
        AuthUserFile "/etc/httpd/conf.d/.htuser"
        AuthName "This is AdminFile"
        Require group admingroup
      	#允许admingroup组访问
</Directory>
原创文章 20 获赞 7 访问量 706

猜你喜欢

转载自blog.csdn.net/adsedfewf/article/details/105764983
今日推荐