用Apache的.htaccess文件增加用户认证
时间:2006-09-13 来源:hmilycbz
用Apache的.htaccess文件增加用户认证
有时候某些目录下的文件需要增加认证,Apache缺省的认证模块都是很完备的,以下就是一个通过.htaccess文件增加用户认证的例子。即使对于虚拟主机用户也可以通过上传一些文件来实现认证控制。
首先用htpasswd创建一个密码文件:比如文件名叫做my.passwd
/opt/apache/bin/htpasswd -c -b my.passwd myusername mypassword
如果增加帐号:
/opt/apache/bin/htpasswd -b my.passwd anotherusername anotherpassword 将my.passwd上传到一个非发布路径下。
比如你的物理WEB根目录的上一级
/webapp/wish/my.passwd 创建 .htaccess
AuthName "My Authorization Directory"
AuthType Basic
AuthUserFile /webapp/wish/my.passwd
Require valid-user 将这个文件上传到你需要进行用户认证的目录:
/webapp/wish/web/admin/.htaccess
在apache的配置文件httpd.conf中需要设置该目录
<Directory "/webapp/wish/web/admin">
Options Indexes
AllowOverride AuthConfig
Order allow,deny
Allow from all
</Directory> 这样这个目录的访问就需要认证了。
/opt/apache/bin/htpasswd -c -b my.passwd myusername mypassword
如果增加帐号:
/opt/apache/bin/htpasswd -b my.passwd anotherusername anotherpassword 将my.passwd上传到一个非发布路径下。
比如你的物理WEB根目录的上一级
/webapp/wish/my.passwd 创建 .htaccess
AuthName "My Authorization Directory"
AuthType Basic
AuthUserFile /webapp/wish/my.passwd
Require valid-user 将这个文件上传到你需要进行用户认证的目录:
/webapp/wish/web/admin/.htaccess
在apache的配置文件httpd.conf中需要设置该目录
<Directory "/webapp/wish/web/admin">
Options Indexes
AllowOverride AuthConfig
Order allow,deny
Allow from all
</Directory> 这样这个目录的访问就需要认证了。
相关阅读 更多 +