Apache 配置 Basic 认证

/*
 * 环境:WAMP( Windows7 + WampServer2.2(Apache 2.2.21)) 
 */

配置过程:

① 生成用户文件,文件路径可以使用绝对路径,也可以使用相对路径

进入 apache 安装目录,使用 htpasswd.exe 创建用户 Admin(密码:123456),保存在 user.txt 中

C:UsersAdministrator>D:
D:>cd wamp/bin/apache/Apache2.2.21/bin
D
:wampinapacheApache2.2.21in>htpasswd.exe -c D:user.txt Admin
New password: ******
Re-type new password: ******
Adding password for user Admin

也可以使用相对路径:

D:wampinapacheApache2.2.21in>htpasswd.exe -c ./user.txt Admin
New password: ******
Re-type new password: ******
Adding password for user Admin

此时在 d 盘下生成了 user.txt:

 

② 配置 httpd.conf,在 httpd.conf 的最后加上一段(只有 d:\practiseup 目录下的项目需要进行认证):

Alias /up "d:\practiseup"
<Directory "d:\practiseup">
    Options FollowSymLinks 
    allowoverride authconfig
    order allow,deny 
    allow from all
    AuthName "Login"
    AuthType basic
    AuthUserFile "d:\user.txt"
    require valid-user 
</Directory>

其中,allowoverride authconfig 表示进行身份认证

AuthName 表示弹出框给出的提示文字,自己定义即可

AuthType 表示认证方式,这里是 basic 认证

AuthUserFile 表示认证用户文件的路径

重启 apache。 

此时访问本机的一个虚拟站点 127.0.0.29(对应的项目路径为 D:/practise/up)

出现了登录框。

如果输入用户名或者密码错误,登录框会再次弹出。

如果点击取消,则会显示 Authorization Required,同时 http 的状态码是 401:

如果输入用户名和密码都正确,则 http 返回 200 OK:

参考:

http://www.pooy.net/apache-allowoverride-authconfig.html

http://m.blog.csdn.net/blog/yupei881027/27559609

http://www.linuxidc.com/Linux/2013-04/82422.htm

http://www.cnblogs.com/bourneli/archive/2012/11/13/2767522.html

原文地址:https://www.cnblogs.com/dee0912/p/4755320.html