htpasswd的使用

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了htpasswd的使用相关的知识,希望对你有一定的参考价值。

参考技术A nginx的源码提供了ngx_http_auth_basic_module这个模块,它可以来解决web访问认证的问题。这个模块是默认就编译进nginx的,可以直接拿来使用。

ngx_http_auth_basic_module它提供了最基本的http认证,这是http协议支持的,它会弹出一个框让你输入用户名和密码,只有用户名和密码输入正确了才能访问,这样就能保证自己的web不被任何人所访问。

ngx_http_auth_basic_module是使用文件作为存储介质的,用户名是明文存储,而密码是加密之后再存储,这样在认证框输入的用户名和密码必须和文件的信息匹配才能认证成功。这里使用htpasswd这个命令来生成存放用户名和密码的文件。

htpasswd 是开源 http 服务器 apache httpd 的一个命令工具,用于生成 http 基本认证的密码文件。

centos

yum -y  install httpd

ubuntu

sudo apt-get install apache2-utils

Usage:

htpasswd [-cimBdpsDv] [-C cost] passwordfile username

htpasswd -b[cmBdpsDv] [-C cost] passwordfile username password

htpasswd -n[imBdps] [-C cost] username

htpasswd -nb[mBdps] [-C cost] username password

参数说明:

-c 创建passwdfile.如果passwdfile 已经存在,那么它会重新写入并删去原有内容.

-n 不更新passwordfile,只将加密后的用户名密码显示在屏幕上;

-m 默认采用MD5算法对密码进行加密

-d 采用CRYPT算法对密码进行加密

-p 不对密码进行进行加密,即使用普通文本格式的密码

-s 采用SHA算法对密码进行加密

-b 命令行中一并输入用户名和密码而不是根据提示输入密码,可以看见明文,不需要交互

-D 删除指定的用户

[root@web02 /application/nginx/conf/extra]# yum install httpd-tools -y

[root@web02 /application/nginx/conf/extra]# htpasswd -cb /application/nginx/conf/htpasswd sunwenchao 123456

Adding password for user sunwenchao

[root@web02 /application/nginx/conf/extra]# chown www /application/nginx/conf/htpasswd

[root@web02 /application/nginx/conf/extra]# chmod 400 /application/nginx/conf/htpasswd

nginx配置文件内容如下

          auth_basic          "sunwenchao blog";

          auth_basic_user_file /application/nginx/conf/htpasswd;

htpasswd -c /application/nginx/conf/htpasswd  Sun

New password:

Re-type new password:

Adding password for user Javen

htpasswd -bc /application/nginx/conf/htpasswd Sun 1234565

说明:在/application/nginx/conf/目录下生成一个htpasswd文件,用户名Sun,密码:123456,默认采用MD5加密方式。

注:去掉-c选项即可在第一个用户之后添加第二个用户

htpasswd -b /application/nginx/conf/htpasswd Sun 1234565

htpasswd -nb Sun 1234565

htpasswd -D /application/nginx/conf/htpasswd Sun

htpasswd 并没有直接修改密码的函数,需要先使用htpasswd删除命令删除指定用户,再利用htpasswd添加用户命令创建用户即可实现修改密码的功能

htpasswd -D /application/nginx/conf/htpasswd Sun

htpasswd -b /application/nginx/conf/htpasswd Sun 1234565

使用 index.php 和不使用它的 url 绕过 htpasswd 身份验证

【中文标题】使用 index.php 和不使用它的 url 绕过 htpasswd 身份验证【英文标题】:Bypass htpasswd authentication for url with index.php and without it 【发布时间】:2015-05-30 12:05:07 【问题描述】:

我已经添加了使用 htaccess 文件进行身份验证的 htpasswd 保护,现在我想绕过 www.website.comwww.website.com/index.php 的身份验证,其中两个 url 都在访问 index.php 文件。通过使用下面的 htaccess 文件,我允许 index.php 并且我能够绕过 www.website.com/index.php url 但不能绕过 www.website.com

我的 htaccess 文件是:

# set an environtment variable "noauth" if the request starts with "/callbacks/"
SetEnvIf Request_URI ^/index.php noauth=1

AuthType Basic

AuthName "RESTRICTED"
#AuthName "Restricted Area"

AuthUserFile "/path/to/.htpasswd"

require valid-user

Options -Indexes
Options +FollowSymlinks
RewriteEngine On
RewriteBase /

RewriteCond %REQUEST_FILENAME !-f
RewriteCond %REQUEST_FILENAME !-d
RewriteRule ^([^.]+)$ /file.php?show=all&name=$1 [L]

# Here is where we allow/deny
Order Deny,Allow
Satisfy any
Deny from all
Require valid-user
Allow from env=noauth

【问题讨论】:

【参考方案1】:

我相信第二个参数只是使用常规正则表达式。也许你可以让 index.php 成为可选的。

 SetEnvIf Request_URI "^/(index\.php)?$" noauth=1

没有测试。

【讨论】:

是的,它起作用了,但是这个和我之前的代码有一些不同的地方。当我打开 www.website.com 或 www.website.com/index.php 时,它会继续询问用户名和密码,尽管昨天使用 index.php 的 url 工作正常,当我单击取消而不是显示需要授权时(401) 它显示来自 index.php 的内容,在 firebug 控制台内的 style/images/js 文件夹出现 401 错误。这意味着它允许显示来自 index.php 页面的内容,但为什么它要求在此页面上输入用户名和密码。 ^^^ 可能是因为它拒绝从 style/css/images/js 文件夹访问,但是如何克服这种情况。如何达到我的要求。 是的,这就是我正在寻找的内容,并且由于其他文件夹被禁止,它会显示身份验证弹出窗口。我只是允许这些文件夹以及它现在工作。

以上是关于htpasswd的使用的主要内容,如果未能解决你的问题,请参考以下文章

apache htpasswd命令

.htpasswd 在特定子域上

htpasswd命令

解密使用 htpasswd 创建的密码

使用 index.php 和不使用它的 url 绕过 htpasswd 身份验证

XAMPP 与 Apache; .htpasswd 密码不会被读取为加密