Total Pageviews

Monday 11 December 2017

Apache下的用户认证




有的时候我们需要访问某个页面的时候浏览器弹出一个用户名/密码的框来认证,这个功能是通过配置apache来实现的,而不需要在相应webapp来做
一种简单的配置方法如下:
首先需要创建一个密码文件,记录所有允许访问的用户名和密码,比如我们要在/etc/apache2/pass中记录密码,则使用如下命令创建该文件并添加初始用户jeremy:
sudo htpasswd -c /etc/apache2/pass jeremy
此时程序会询问密码,设置完之后文件就创建好了。
以后要修改或添加用户时,只要把-c参数去掉就行了。-D为删除用户。
接下来就是使用了,在某个Directory的权限配置中加入如下几行:
AuthType basic
AuthName “name”
AuthUserFile /etc/apache2/pass
Require valid-user
其中name将出现在认证对话框中。
---------
有时候我们需要给我apache服务器下制定的目录加上用户认证,方便一些而用户进行文件的浏览。配置如下:

1 设置用户

  • 1
    
  • htpasswd -c file_path user_name
    
回车之后输入密码即可,请确保命令中的file _path有其他用户读的权限。

2 设置apache

在/etc/apache2/apache2.conf或/etc/httpd/conf/httpd.conf中添加以下内容
  • 1
    
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • var
/www/html/picture>
  • AuthName "Important Directory" #登录时提示的内容
  • AuthType Basic #认证方式
  • IndexOptions Charset=GB2312 #网页编码
  • Options Indexes FollowSymLinks MultiViews #以目录形式展示
  • AuthUserFile /opt/.apache_user #用户文件,1中file_path
  • Require valid-user
  • 若要隐藏服务器标示,请在配置文件中加入以下信息:
    • 1
      
    • 2
    • ServerSignature Off
      
    • ServerTokens Prod
    ----------------------------------------

    https://en.wikipedia.org/wiki/Basic_access_authentication

    No comments:

    Post a Comment