【问题标题】:Apache Basic Auth not working in .htaccess or Directory blocks; works fine in Location blocksApache Basic Auth 在 .htaccess 或 Directory 块中不起作用;在位置块中工作正常
【发布时间】:2015-06-28 14:47:42
【问题描述】:

好的,在新安装的 Ubuntu 14.04 上运行 Apache 2.4.7。

如果我将以下内容添加到/etc/apache2/sites-available 中的 .conf 文件中,则基本身份验证可以正常工作:

<Location /test>
  AuthType Basic
  AuthName ".htaccess Basic Auth Test"
  AuthUserFile "/var/www/passwords/test"
  Require valid-user
</Location>

但是,如果我只是在相关目录的.htaccess 文件中有以下内容:

AuthType Basic
AuthName ".htaccess Basic Auth Test"
AuthUserFile "/var/www/passwords/test"
Require valid-user

身份验证似乎被忽略了;没有提示,页面就好像它们不存在一样。目录块也是如此:

<Directory /var/www/default/test>
  AuthType Basic
  AuthName ".htaccess Basic Auth Test"
  AuthUserFile "/var/www/passwords/test"
  Require valid-user
</Directory>

所以,我检查了 .htaccess 文件是否真的被读取,它们是;添加

Options -Indexes

.htaccess 文件确实会删除目录上的索引列表。

我打开了调试,但这并不是特别有用:

[Tue Apr 21 13:04:14.082275 2015] [authz_core:debug] [pid 8914] mod_authz_core.c(721): [client 144.32.48.10:64109] AH01625: authorization result of <RequireAny>: granted (directive limited to other methods)
[Tue Apr 21 13:04:14.082538 2015] [authz_core:debug] [pid 8914] mod_authz_core.c(721): [client 144.32.48.10:64109] AH01625: authorization result of <RequireAny>: granted (directive limited to other methods)

/etc/apache2 中的 Grep'ing 显示没有任何限制;没有其他Auth 语句,AllowOverride All 在正确的位置(如选项测试所示;他们说All,没有其他可能限制覆盖的东西)。

我觉得我一定遗漏了一些明显的东西。这是加载的模块:

root@linode:/etc/apache2# apachectl -M
Loaded Modules:
 core_module (static)
 so_module (static)
 watchdog_module (static)
 http_module (static)
 log_config_module (static)
 logio_module (static)
 version_module (static)
 unixd_module (static)
 alias_module (shared)
 auth_basic_module (shared)
 authn_core_module (shared)
 authn_file_module (shared)
 authz_core_module (shared)
 authz_host_module (shared)
 authz_user_module (shared)
 autoindex_module (shared)
 deflate_module (shared)
 dir_module (shared)
 env_module (shared)
 filter_module (shared)
 headers_module (shared)
 mime_module (shared)
 mpm_prefork_module (shared)
 negotiation_module (shared)
 php5_module (shared)
 proxy_module (shared)
 proxy_http_module (shared)
 rewrite_module (shared)
 setenvif_module (shared)
 status_module (shared)
 substitute_module (shared)
 xml2enc_module (shared)

【问题讨论】:

  • 试一试。没有": AuthUserFile /var/www/passwords/test 因为我从不使用它们,这最终可能是绝对路径的问题......
  • @Croises 唉,不;我想我添加了它们以防它改变任何东西!
  • 您是否尝试为每个测试更改AuthName ".htaccess Basic Auth Test"?因为如果您为 2 个或更多目录请求相同的 AuthName,浏览器只会第一次询问...
  • @Croises 只是尝试了一个不同的名称,没有改变,仍然被忽略。

标签: .htaccess authentication basic-authentication apache2.4


【解决方案1】:

我知道我做了一些有趣的事情。我的服务器正在测试代理,所以一个网站建议我把它添加到apache2.conf:

<Location />
   <Limit CONNECT>
     Require all denied
   </Limit>
</Location>

删除了(一时兴起 - 看起来不会改变它)并且基本身份验证再次起作用!

为了保护站点,我将块移至默认(仅限 IP 地址)虚拟主机,因此其他的将正常运行。

【讨论】:

    【解决方案2】:

    另一种解决方案是安装WP htaccess Control plugin 并在 Custom htaccess 中添加这一行:

    SetEnvIf Authorization "(.*)" HTTP_AUTHORIZATION=$1
    

    【讨论】:

      猜你喜欢
      • 2019-12-11
      • 1970-01-01
      • 2021-02-03
      • 1970-01-01
      • 2021-12-23
      • 2017-06-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多