一.基本的Apache用户认证方法:
若对某一目录下的文件如/home/ftp/pub需要做到用户认证,则在httpd.conf
中加入下面的行
<>
options indexes followsymlinks
allowoverride authconfig
order allow,deny
allow from all
<>
用在目录/home/ftp/pub下放文件.htaccess,内容如下:
authname "shared files"
authtype basic
authuserfile /etc/.passwd
require valid-user
用随Apache来的程序htpasswd 生成文件/etc/.passwd,每行一个用户名:密码
首先用htpasswd创建一个密码文件:比如文件名叫做my.passwd
/home/apache/bin/htpasswd -c -b my.passwd myusername mypassword
如果增加帐号:
/home/apache/bin/htpasswd -b my.passwd anotherusername anotherpassword
将my.passwd上传到一个非发布路径下。
比如你的物理WEB根目录的上一级
/home/apache/conf/my.passwd
创建 .htaccess
AuthName "My Authorization Directory"
AuthType Basic
AuthUserFile /home/apache/conf/my.passwd
Require valid-user
将这个文件上传到你需要进行用户认证的目录:
/home/apache/htdocs/admin/.htaccess
这样这个目录的访问就需要认证了。
只要能提供正确的用户名和密码对,就允许登录访问,这是针对任何地址来的
请求都要求提供用户名和密码认证。
二.针对部分网段或地址要求认证。
若公司LAN所在网段为192.168.0.0/24,且有一防火墙专线接入Internet,
内部网卡的地址为192.168.0.1/32,则现在希望所有通过拨本地163通过
防火墙上的apache反向代理向LAN上的另一WWW服务器访问时需要认证,而本地
LAN上的用户不需认证。可以在httpd.conf中放入:
〈Directory /home/ftp/pub>
Options Indexes FollowSymLinks
AllowOverride AuthConfig
order deny,allow
deny from 192.168.0.1
〈/Directory>
且在/home/ftp/pub/.htaccess中放入:
AuthName "shared files"
AuthType Basic
AuthUserFile /etc/.passwd
require valid-user
satisfy any
三.对同一目录及其下的子目录有不同的权限,仅某些人可以存取一目录下的
子目录。
如有一目录/home/ftp/pub/sales,有三个用户user1,user2,user3都需要用户名
和密码进入/home/ftp/pub,但仅user1,user2能进入/home/ftp/pub/sales.则
放下面的行到httpd.conf
〈Directory /home/ftp/pub>
Options Indexes
AllowOverride AuthConfig
order allow,deny
allow from all
〈/Directory>
〈Directory /home/ftp/pub/sales>
Options Indexes
AllowOverride AuthConfig
order allow,deny
allow from all
〈/Directory>
且看/home/ftp/pub/.htaccess为:
AuthName "shared files"
AuthType Basic
AuthUserFile /etc/.passwd
require valid-user
且看/home/ftp/pub/sales/.htaccess
AuthName "shared files"
AuthType Basic
AuthUserFile /etc/.passwd
AuthGroupFile /etc/.salesgroup
require group manager
且文件/etc/.passwd内容为:
user1:passwd1
user2:passwd2
user3:passwd3
且文件/etc/.salesgroup内容为:
manager: user1 user2
在Apache中使用.htaccess配置文件"
在配置Apache时,除了可以在主配置文件(http.conf)中配置访问控制之外,还可以使用.htaccess 文件配置对指定目录的访问控制。使用.htaccess文件可以改变主配置文件中的配置,但是它只能设置对指定目录的访问控制,这个目录就是. htaccess文件存放的目录。在一个目录下设置了.htaccess文件之后,当用户使用浏览器访问此目录时,Apache会读取该文件的配置来覆盖主配置文件(http.conf)的配置。
|
|