在 Apache 2.2 虚拟主机中组合基本身份验证和 LimitExcept

Combining Basic Authentication and LimitExcept in Apache 2.2 Virtual Host

我试图满足以下要求(在 Apache HTTPD 2.2 中):

这是我尝试过的许多事情之一,但是 none 我尝试过的事情满足了所有三个要求:

<Directory /path/to/wwwroot>
    Options FollowSymLinks
    AllowOverride FileInfo

    # Basic Authentication
    AuthType Basic
    AuthName "Enter your site username and password."
    AuthUserFile /path/to/stage.passwords
    AuthGroupFile /path/to/stage.groups
    Require group stageusers

    # there's more logic for this variable in the real virtual_host.
    # for this simplified example, manually set (using the following)
    # or unset (using !internal_user).
    SetEnv internal_user

    Order deny,allow
    Deny from all
    Allow from env=internal_user

    <LimitExcept HEAD POST GET>
        Deny from all
    </LimitExcept>

    Satisfy all

</Directory>

我已阅读有关 Satisfy、Limit、LimitExcept、Order 和基本身份验证的文档,但我无法将各个部分放在一起。

执行此操作的可行方法是什么?

A​​pache 2.2 中的 AFAICT 您需要返回到 "Satisfy Any" 方法,然后使用 mod_rewrite 处理方法检查。这是最佳途径,因为您的方法检查是完全独立的。

在2.4中,Limit/LimitExcept是由mod_allowmethodsreplaced/simplified,但是require也可以直接检查methods。那里就简单多了。

重写部分非常简单:

RewriteEngine ON
RewriteCond %{REQUEST_METHOD} !^(GET|HEAD|POST)$
RewriteRule .* - [F]

但是您需要确保它出现在可以访问目录的每个 vhost + 主服务器中,这与其他指令不同。

综合考虑

# Only allow expected HTTP methods.
RewriteCond %{REQUEST_METHOD} !^(GET|HEAD|POST)$
RewriteRule .* - [F]

<Directory /path/to/wwwroot>
    Options FollowSymLinks
    AllowOverride FileInfo

    Satisfy any

    # Basic Authentication
    AuthType Basic
    AuthName "Enter your site username and password."
    AuthUserFile /path/to/stage.passwords
    AuthGroupFile /path/to/stage.groups
    Require group stageusers

    # there's more logic for this variable in the real virtual_host.
    # for this simplified example, manually set (using the following)
    # or unset (using !internal_user).
    SetEnv internal_user

    Order deny,allow
    Deny from all
    Allow from env=internal_user

</Directory>