Apache配置--用户认证(针对目录访问)-update2015-05-02

通过Apache配置可以限制用户对目录的访问,会弹出像phpadmin一样的登陆框。

===========================================================

1)建立需要保护的目录

①在/usr/local/apache2/htdocs/下建立目录,然后保护,或者②。

②使用别名(httpd-autoindex.php),在系统位置建立目录,然后保护。

 在httpd-autoindex.php文件中建立别名目录:

Alias /soft/  "/share/soft"      
  <Directory "/share/soft/">  
  Options Indexes FollowSymLinks
  AllowOverride None
  Order allow,deny
  Allow from all
</Directory>

2)修改配置文件的AllowOverride,允许.htaccess权限文件生效

<Directory "/share/soft/"> 
  Options Indexes FollowSymLinks
  AllowOverride All    #开启权限认证文件.htaccess
  Order allow,deny
  Allow from all
</Directory>

 

3)在指定目录建立权限文件

cd  /share/soft

vim  .htaccess    

#加入下边的四句话(不区分大小写)。
AuthName "input your pwd"        #提示信息,会显示在弹出的密码框中,可以修改。
AuthType basic                   #加密类型
AuthUserFile /share/soft/apache.passwd   #密码文件,文件名和位置自定义。(最好不要定义在soft/目录下,因为 “Options Indexes”,会看到密码文件)
require valid-user #允许密码文件中所有用户访问

4)建立密码文件,加入允许访问的用户。

/usr/local/apache2/bin/htpasswd  -c  /share/soft/apache.passwd  test1  
//此命令后会提示输入密码,-c表示建立密码文件,只有添加第一个用户时,才能-c

//再添加用户用-m
/usr/local/apache2/bin/htpasswd  -m  /share/soft/apache.passwd  test2

访问www.xxx.com/soft/时会提示:

5)不通过.htaccess权限文件配置,即直接在配置文件中配置权限也可以,就省略了3步,修改2步如下

<Directory "/share/soft/">
    Options Indexes Includes FollowSymLinks
   Order allow,deny
   Allow from all
AllowOverride None AuthUserFile /usr/share/monitorix/.passwd #就是把这四句话直接加到下边 AuthName "input your pwd" AuthType Basic Require valid-user </Directory>

6)需要验证的页面通过curl抓取

 //虚拟机地址,这里url直接写虚拟机配置的host域名会导致curl报错,找不到host。
//需要先写IP,然后在设置一个Host选项
$url = 'http://192.168.31.174:80'; 
$param = array('content'=>'fdipzone blog');  //post的数据

$ch = curl_init();  
curl_setopt($ch, CURLOPT_URL, $url);  
curl_setopt($ch, CURLOPT_POST, true);  
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($param));  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  
curl_setopt($ch,CURLOPT_HTTPHEADER,array("Host: kwht.com")); //这里制定host名称
curl_setopt($ch, CURLOPT_USERPWD, 'asd:asd'); // http auth
$ret = curl_exec($ch); 
if (false === $ret) {
     echo "curl_error".curl_error($ch);
 } 
$retinfo = curl_getinfo($ch);  
curl_close($ch);  

if($retinfo['http_code']==200){  
    $data = json_decode($ret, true);  
    print_r($data);  
}else{  
    echo 'POST Fail';  
} 

7)不通过修改配置和文件+生成密码文件的方式配置

服务端可以通过判断$_SERVER['PHP_AUTH_USER'],$_SERVER['PHP_AUTH_PW']验证账号和密码

把下边的代码加入到index.php,可以不用修改配置文件也不用生成密码文件而是通过PHP提供的$_SERVER变量来验证

<?php
if(!isset($_SERVER['PHP_AUTH_USER'])) 
{ 
    header('WWW-Authenticate: Basic realm="localhost"'); //头部返回这行就会弹出登陆框
    header("HTTP/1.0 401 Unauthorized"); 
    exit; 
}else{ 
    if (($_SERVER['PHP_AUTH_USER']!= "asd" || $_SERVER['PHP_AUTH_PW']!="asd")) {
        header('WWW-Authenticate: Basic realm="localhost"');
        header("HTTP/1.0 401 Unauthorized");
        exit;
    }
}

//验证通过才到这里来
$content = isset($_POST['content'])? $_POST['content'] : ''; header('content-type:application/json'); echo json_encode(array('content'=>$content)); ?>
原文地址:https://www.cnblogs.com/leezhxing/p/3298060.html