【问题标题】:Password protecting a directory and all of it's subfolders using .htaccess使用 .htaccess 保护目录及其所有子文件夹的密码
【发布时间】:2011-03-08 07:36:34
【问题描述】:

我正在尝试对子域及其所有子目录和文件进行密码保护,但我对此事的了解非常有限,我该怎么做?

【问题讨论】:

    标签: .htaccess password-protection


    【解决方案1】:

    这是一个简单的两步过程

    在你的 .htaccess 中

    AuthType Basic
    AuthName "restricted area"
    AuthUserFile /path/to/the/directory/you/are/protecting/.htpasswd
    require valid-user
    

    使用http://www.htaccesstools.com/htpasswd-generator/或命令行生成密码 并将其放入 .htpasswd

    注意 1:如果您使用的是 cPanel,您应该在安全部分“密码保护目录”中进行配置

    编辑:如果这不起作用,那么您可能需要对 http.conf 中的 .htaccess(或至少以前的目录)的目录执行 AllowOverride All,然后是 apache重启

    <Directory /path/to/the/directory/of/htaccess>
          Options Indexes FollowSymLinks MultiViews
          AllowOverride All
    </Directory>
    

    【讨论】:

    • 那么我是否将.htaccess 文件和.htpasswd 文件都放在sudomain 的目录中?还是将.htpasswd 文件放在我想要保护的目录中,将.htaccess 放在子域的目录中?
    • @user270311 您需要将 .htaccess 放在需要保护的目录中。所以在子域的目录中。和 .htpasswd 在目录中的任何位置,具有在 AuthUserFile 中指定的适当权限
    • 我会以不同的方式表述这一点:将 .htpasswd 放入一个不以任何方式(受保护或不受保护)暴露给网络但对 apache 可用的目录中。
    • 在 Apache 2.4 上不起作用我试过了,没有任何反应
    • 我输入了错误的密码。现在每次我重新加载它都会返回:内部服务器错误。我看不到用户名密码输入选项。即使我的网站无法正常工作,它也会返回内部服务器错误。 @Mahesh
    【解决方案2】:

    要对 Apache 提供的目录进行密码保护,您需要在要保护的目录中有一个 .htaccess 文件和一个 .htpasswd 文件,该文件可以位于系统上任何 Apache 用户可以访问的位置(但将其放在合理且合理的位置)私人的)。您很可能不想将.htpasswd.htaccess 放在同一文件夹中。

    .htaccess 文件可能已经存在。如果没有,请创建它。然后插入:

    AuthType Basic
    AuthName "Your authorization required message."
    AuthUserFile /path/to/.htpasswd
    require valid-user
    

    然后使用您想要的任何用户名和密码创建一个 .htpasswd 文件。密码应加密。如果您在 Linux 服务器上,您可以使用 htpasswd 命令为您加密密码。以下是该命令如何用于此目的:

    htpasswd -b /path/to/password/file username password

    【讨论】:

      【解决方案3】:

      只需扩展 Mahesh 的答案。

      .htaccess

      AuthType Basic
      AuthName "restricted area"
      AuthUserFile /path/to/the/directory/you/are/protecting/.htpasswd
      require valid-user
      

      如果您不想使用在线密码生成器,可以使用htpasswdopenssl

      1。使用htpasswd

      htpasswd -c /path/to/the/directory/you/are/protecting/.htpasswd my_username
      # then enter a password
      # -c means Create a new file
      

      2。使用openssl

      openssl passwd -apr1 your_password
      

      然后将生成的密码放到.htpasswd,格式为:

      username:<generated_password>
      

      例子:

      .htpasswd

      my_username:$apr1$ydbofBYx$6Zwbml/Poyb61IrWt6cxu0
      

      【讨论】:

        【解决方案4】:

        您需要生成一个密码(用户名+密码)字符串进行身份验证,将其写入文件并将其放在您要限制访问的子目录中。

        字符串看起来像,

        username:hashkey
        
        • 您可以使用HTTP password generator tool 来执行此操作。
        • 将从上述站点获得的字符串复制并粘贴到站点 webroot 之外的任何位置的新文件 (.htpasswd) 中(最好保留在用户主目录内的任何位置)。
        • 在您的 .htaccess 文件中添加以下行。
        AuthType Basic
        AuthName "Require Authentication"
        AuthUserFile [PATH_TO_FILE]/.htpasswd
        Require valid-user
        
        • 如果密码没有触发,检查.htaccess文件的权限。

        • 如果验证失败,请检查指定位置是否存在 .htpasswd 文件。 (确保您的用户帐户对 .htpasswd 文件有足够的权限来读取)

        • 您无需重新启动服务器即可实现此目的。

        如果您有任何疑问,请告诉我。

        【讨论】:

          【解决方案5】:

          您可能想要使用mod_auth_digest 模块。 Apache 提供了一个非常好的guide 来使用全系列的身份验证和授权模块。

          【讨论】:

          • 检查一下:“这个模块实现了 HTTP Digest Authentication (RFC2617),并提供了 mod_auth_basic 的替代方案,其中密码不作为明文传输。但是,这并不会带来显着的安全优势基本身份验证。另一方面,摘要式身份验证在服务器上存储的密码安全性远低于基本身份验证。因此,使用基本身份验证并使用 mod_ssl 加密整个连接是更好的选择。"
          • @biocyberman,感谢您的更正。我会留下这个帖子作为对未来的警告。
          【解决方案6】:

          要创建正确的密码,您可以创建一个 php 文件并在本地(在您的计算机上,而不是在网络服务器上)运行它,其中包含以下内容:

          <!DOCTYPE html>
          <html>
          <head>
          <meta charset="utf-8" />
          </head>
          <body>
          <form method="post" accept-charset="utf-8">
              <input type="text" name="clear"/>
              <input type="submit" name="submit" value="generate" />
          </form>
          
          <?php
          header("Content-Type: text/html; charset=utf-8");
          
          if (isset($_POST['clear']) && $_POST['clear'] != '') {
              $cl = $_POST['clear'];
              $pw = crypt($cl, base64_encode($cl));
              echo $pw;
          }
          ?>
          </body>
          </html>
          

          我通常将我的 .htpasswd 文件放在 webcontent 目录之外的名为 /htpasswd/ 的目录中,例如 AuthUserFile /home/www/usr122/files/htpasswd/.sportsbar_reports_htpasswd(而不是在 webcontent 文件夹中 /home/www/usr122/html/htpasswd/)并将 .htpasswd 文件重命名为它的用途,例如.sportsbar_reports_htpasswd

          密码文件本身应该是这样的

          testusername:dGATwCk0tMgfM
          

          用户名是testusername,密码是testuserpassword

          【讨论】:

          • 假设每个人都在自己的服务器上运行 PHP 是错误的。
          • @ArslanKhan 你是对的。但是很抱歉,此时我写了这篇文章,对我来说,apache 和 php 是网络世界中唯一存在的东西 :-)
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2014-12-16
          • 1970-01-01
          • 1970-01-01
          • 2017-04-18
          • 2022-01-07
          相关资源
          最近更新 更多