【问题标题】:Password protect a specific URL密码保护特定 URL
【发布时间】:2013-01-30 12:04:28
【问题描述】:

该网站位于共享主机上。我需要对单个 URL 进行密码保护。

http://www.example.com/pretty/url

显然这不是我要保护的物理文件路径,它只是那个特定的 URL。

.htaccess 有什么快速的解决方案吗?

【问题讨论】:

    标签: .htaccess password-protection


    【解决方案1】:

    您应该能够使用 mod_env 和 Satisfy any 指令的组合来执行此操作。您可以使用SetEnvIf 来检查Request_URI,即使它不是物理路径。然后您可以检查变量是否在Allow 语句中设置。因此,要么您需要使用密码登录,要么Allow 让您无需密码即可登录:

    # Do the regex check against the URI here, if match, set the "require_auth" var
    SetEnvIf Request_URI ^/pretty/url require_auth=true
    
    # Auth stuff
    AuthUserFile /var/www/htpasswd
    AuthName "Password Protected"
    AuthType Basic
    
    # Setup a deny/allow
    Order Deny,Allow
    # Deny from everyone
    Deny from all
    # except if either of these are satisfied
    Satisfy any
    # 1. a valid authenticated user
    Require valid-user
    # or 2. the "require_auth" var is NOT set
    Allow from env=!require_auth
    

    【讨论】:

    • 工作愉快,谢谢。在其他地方找不到类似的东西。
    • 您好,这个 sn-p 在 apache 2.2 中有效,但在 apache 2.4 中无效,关于如何将其迁移到 2.4 有什么想法吗?我的问题是在所有 URL 中都要求密码保护
    • 这里一样,到处问密码
    • 这里同样适用于“Server:Apache/2.2.15 (CentOS)”而不是“Server:Apache/2.2.3 (CentOS)”
    • @edank 不,您还必须有用户名。如果要使用特定用户名,必须将Require valid-user 更改为Require user [your username]。您不能在 .htaccess 文件中定义密码,因为它不安全。您必须使用htpasswd 在您的服务器上创建一个 .htpasswd 文件,其中包含用户及其密码。
    【解决方案2】:

    您可以在<VirtualHost> 指令中使用<LocationMatch> 或简单地使用<Location> 来执行此操作(假设您可以访问您的httpd.conf / vhost.conf - 或者您可以在您的.htaccess 中放置类似的内容如果您必须以这种方式配置您的网站,请记录根目录)。

    例如:

    <VirtualHost *:80>
      ServerName www.example.com
      DocumentRoot /var/www/blabla
      # Other usual vhost configuration here
      <Location /pretty/url>
        AuthUserFile /path/to/.htpasswd
        AuthGroupFile /dev/null
        AuthName "Password Protected"
        AuthType Basic
        require valid-user
      </Location>
    </VirtualHost>
    

    如果您想将正则表达式与漂亮的 URL 进行匹配,您可能会发现 &lt;LocationMatch&gt; 更有用。文档是here

    【讨论】:

    • 这比公认的答案更有用、更简洁、更漂亮。谢谢!
    • 如果您可以控制虚拟主机,这是最好的解决方案
    • 这适用于 apache 2.4.25!它更加优雅和直观。谢谢!
    • 这不起作用。 “AuthGroupFile /dev/null”会导致错误,没有它它不会阻止 URL
    • 不能在 apache 2.2 上工作(遗憾的是我现在坚持使用那个版本)
    【解决方案3】:

    由于 Rick 在评论中表示此问题的答案无效,因此这是我使用的 sn-p:

    AuthName "Protected Area"
    AuthType Basic
    AuthUserFile /path/to/your/.htpasswd
    AuthGroupFile /dev/null
    
    SetEnvIf Request_URI .* noauth
    SetEnvIf Request_URI the_uri_you_want_to_protect !noauth
    SetEnvIf Request_URI another_uri !noauth
    SetEnvIf Request_URI add_as_many_as_you_want !noauth
    
    <RequireAny>
      Require env noauth
      Require valid-user
    </RequireAny>
    

    如果您需要支持 Apache 2.2 和 Apache 2.4(显然有两个版本并行运行的设置...):

    AuthName "Protected Area"
    AuthType Basic
    AuthUserFile /path/to/your/.htpasswd
    AuthGroupFile /dev/null
    
    SetEnvIf Request_URI .* noauth
    SetEnvIf Request_URI the_uri_you_want_to_protect !noauth
    SetEnvIf Request_URI another_uri !noauth
    SetEnvIf Request_URI add_as_many_as_you_want !noauth
    
    <IfModule mod_authz_core.c>
        <RequireAny>
            Require env noauth
            Require valid-user
        </RequireAny>
    </IfModule>
    
    <IfModule !mod_authz_core.c>
        Order Deny,Allow
        Deny from all
        Satisfy any
        Require valid-user
        Allow from env=noauth
    </IfModule>
    

    Apache 2.2 的代码取自Jon Lin

    【讨论】:

    • 就是这样!你震撼了世界@Pharaoh(在 Apache/2.4.27 (cPanel) 上测试)
    • 这是简单问题的最佳解决方案。我有一个不同的问题,我正在使用环境变量保护一些 pdf 文件,但由于它与另一个环境变量重叠,所以很痛苦!谢谢你的回答。
    • 这是唯一对我有用的 sn-p。我认为我们可以使用 块来为每个 URL 提供不同的行为,但我认为如果它在 .htaccess 中可悲的是它不起作用(不确定,未经测试)。
    【解决方案4】:

    所有提供的解决方案都不适合我。我认为以下指令可以解决问题:

    SetEnvIf Request_URI ^/page-url auth=1
    
    AuthName "Please login"
    AuthType Basic
    AuthUserFile "/www/live.example.com/files/html/.htpasswd"
    
    # first, allow everybody
    Order Allow,Deny
    Satisfy any
    Allow from all
    Require valid-user
    # then, deny only if required
    Deny from env=auth
    

    【讨论】:

      【解决方案5】:

      我更新了Jon Lin 的代码,以保护除一个之外的所有 URL - 例如虚拟主机根目录下的 robots.txt。这些是 Apache 2.2 兼容指令。

      <ifmodule mod_setenvif.c>
      SetEnv  require_no_auth=false
      SetEnvIf Request_URI "^/robots.txt" require_no_auth=true
      
      AuthType Basic
      AuthName "Restricted"
      AuthUserFile /home/someuser/.htpasswd
      
      # Setup a deny/allow
      Order Deny,Allow
      # Deny from everyone
      Deny from all
      # 1. a valid authenticated user
      Require valid-user
      # or 2. the "require_no_auth" var is set
      Allow from env=require_no_auth
      # except if either of these are satisfied
      Satisfy any
      </ifmodule>
      

      Apache 2.4 的代码相同

      <ifmodule mod_setenvif.c>
      SetEnv  require_no_auth=false
      SetEnvIf Request_URI "^/robots.txt" require_no_auth=true
      
      AuthType Basic
      AuthBasicProvider file
      AuthName "Restricted"
      AuthUserFile /home/someuser/.htpasswd
      
      # grant access if either of these are satisfied
      # 1. a valid authenticated user
      Require valid-user
      # or 2. the "require_no_auth" var is set
      Require env require_no_auth
      </ifmodule>
      

      【讨论】:

        【解决方案6】:

        在 Apache2.4 中你可以这样做:

        <If "%{REQUEST_URI} =~ m#/pretty/url/?#i">
            AuthUserFile /var/www/htpasswd
            AuthName "Password protected"
            AuthType Basic
            Require valid-user
        </If>
        

        【讨论】:

          【解决方案7】:

          如何将用户重定向到受密码保护的子文件夹?

          .htaccess

          RewriteCond %{HTTP_COOKIE} !BadHorsie=secret_cookie_key
          RewriteRule ^(pretty/url)$ /protected/login.php?url=$1 [R=307,L]
          

          受保护的/.htaccess

          AuthUserFile /usr/www/{YOUR_PATH}/protected/.htpasswd
          AuthGroupFile /dev/null
          AuthName "Protected"
          AuthType Basic
          require user BadHorsie
          

          受保护的/.htpasswd

          BadHorsie:$apr1$fFbaaVdF$Q5ql58g7R4qlpMUDb/5A0/
          

          受保护的/login.php

          <?php
          if (isset($_GET['url']) && $_GET['url'] && $_GET['url'][0] != '/' && strpos($_GET['url'], '//') === false) {
              setcookie('BadHorsie', 'secret_cookie_key', 0, '/');
              header('Location: /' . $_GET['url'], true, 307);
              exit;
          }
          ?>
          

          会发生什么

          1. 用户请求example.com/pretty/url
          2. 307 重定向到example.com/protected/login.php?url=pretty/url
          3. 登录
          4. 成功时:用户使用密钥获取会话 cookie
          5. 307 重定向回example.com/pretty/url
          6. 用户获取机密内容

          注意:当然,“会话 cookie 和反向重定向”机制是完全可选的。最后,您可以直接通过protected/login.php 提供您的秘密内容。我展示这种方式只是为了获得灵感。

          可选:不要使用 PHP 和 set the cookie through .htaccess.

          【讨论】:

            【解决方案8】:
            1. 首先,您需要创建一个新用户/密码:

            这个命令:

            htpasswd -c /etc/apache2/.htpasswd myuser1
            

            如果您使用的是Bitnami Apps(例如Open edx),您需要遵循以下说明:

            htpasswd -c /opt/bitnami/apps/edx/conf/.htpasswd myuser1
            
            1. 如果你已经定义了一个虚拟主机,你需要在你的 apache 配置文件中添加下面几行。

            这个配置:

            <VirtualHost *:80>
            ...
            
            <Location />
                Deny from all
                #Allow from (Set IP to allow access without password)
                AuthUserFile /etc/apache2/.htpasswd
                AuthName "Restricted Area"
                AuthType Basic
                Satisfy Any
                require valid-user
            </Location>
            ...
            
            </VirtualHost>
            
            1. 对于 Bitnami 应用程序,您需要在 ‘/opt/bitnami/apps/edx/conf/httpd-lms.conf’中添加这些配置

            此配置用于保护所有 URL。:

            <Location />
                Deny from all
                #Allow from (Set IP to allow access without password)
                AuthUserFile /opt/bitnami/apps/edx/conf/.htpasswd
                AuthName "Restricted Area"
                AuthType Basic
                Satisfy Any
                require valid-user
            </Location>
            
            1. 最后,您需要重新启动 Apache 服务器。

            在 Bitnami 应用程序中:

            /opt/bitnami/ctlscript.sh restart apache
            

            【讨论】:

            • This config to protect all URLs. - 问题是关于保护单个 URL
            【解决方案9】:

            很遗憾,我无法评论 @jon-lin 的回答。因此,我创建了一个新答案。我在 apache 2.4 环境中的适应解决方案是:

            #
            # password protect /pretty/url URIs
            #
            
            AuthType Basic
            AuthName 'Authentication required'
            AuthUserFile /path/to/.htpasswd
            
            # Restrict access to some urls
            SetEnvIf Request_URI ^/pretty/url  auth=1
            
            <RequireAll>
              # require the auth variable to be set
              Require env auth
              # require an valid-user
              Require valid-user
            </RequireAll>
            

            【讨论】:

            • 有了这个,你不能打开除/pretty/url之外的任何其他url,因为如果没有设置变量,Require env auth将禁止访问
            • 这似乎是@Pharaoh 的情况。那么我们如何解决这个问题呢?此页面上的任何内容似乎都无法正常工作...(我在 Apache/2.4.27 (cPanel) 顺便说一句。)
            【解决方案10】:

            上面的解决方案太忙了,有点混乱。我喜欢这样简单明了

            <Files "protected.html">
             AuthName "Username and password required"
             AuthUserFile /home/fullpath/.htpasswd
             Require valid-user
             AuthType Basic
            </Files>
            

            以上内容将被放入您的 htaccess 文件中,其中“protected.html”是您要保护的文件(可以是您想要的任何文件,例如 myphoto.jpg 或 document.zip,只需重命名根据您的喜好)。 AuthUserFile 是密码文件的完整路径。你完成了。

            尽管您可能必须确保已设置此先决条件。 Apache 目录标签配置中需要 AllowOverride 和 AuthConfig 才能使代码正常工作,但通常它是开箱即用的,因此除非您进行自己的自定义构建,否则您应该安全无虞。

            【讨论】:

            • 问题清楚地表明这不是他们试图保护的物理 URL。所以保护一个文件在这里无济于事。
            猜你喜欢
            • 2020-09-09
            • 1970-01-01
            • 2011-07-31
            • 1970-01-01
            • 1970-01-01
            • 2018-08-29
            • 2011-08-04
            • 2015-01-26
            • 1970-01-01
            相关资源
            最近更新 更多