从 http 基本身份验证中排除特定的 cakephp 控制器

2023-11-27

我试图排除路径 (URI) 被基本 http 身份验证阻止。 路径是 /rest (http://example.com/rest) 并代表 cakephp 3 应用程序的控制器。它不是一个真实的文件,而是一个由重写条件重写并由 webroot 目录中的 index.php 处理的路径。

重写规则如下:

/var/www/.htaccess:

<IfModule mod_rewrite.c>
     RewriteEngine on
     RewriteRule    ^$    webroot/    [L]
     RewriteRule    (.*) webroot/$1    [L]
</IfModule>

/var/www/webroot/.htaccess:

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteRule ^ index.php [L]
</IfModule>

我正在运行 apache 2.4 并尝试了不同的配置:

<VirtualHost *:80>
   ServerAdmin webmaster@localhost
   DocumentRoot /var/www/webroot
<Directory /var/www>
   Options FollowSymLinks
   AllowOverride All
</Directory>
<Location "/">
           AuthType Basic
           AuthName "Keawe Development"
           AuthUserFile /host/.htpasswd
           Require valid-user
           Require expr %{REQUEST_URI} =~ m#/rest/.*#
           Require expr %{REQUEST_URI} =~ m#/index.php/rest/.*#
</Location>
   ErrorLog ${APACHE_LOG_DIR}/error.log
   CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

...改编自https://stackoverflow.com/a/33655232/1285585

<VirtualHost *:80>
   ServerAdmin webmaster@localhost
   DocumentRoot /var/www/webroot
<Directory /var/www>
   Options FollowSymLinks
   AllowOverride All
</Directory>
<Location "/">
           AuthType Basic
           AuthName "Keawe Development"
           AuthUserFile /host/.htpasswd
           Require valid-user
</Location>
<Location "/rest">
   Allow from all
   Satisfy any
</Location>
   ErrorLog ${APACHE_LOG_DIR}/error.log
   CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

... 从https://serverfault.com/a/475845/229877

<VirtualHost *:80>
  ServerAdmin webmaster@localhost
  DocumentRoot /var/www/webroot
<Directory /var/www>
  Options FollowSymLinks
  AllowOverride All
 </Directory>
 <Location "/">
   AuthType Basic
   AuthName "Keawe Development"
   AuthUserFile /host/.htpasswd
   Require valid-user
 </Location>
 <Location "/rest">
   Require all granted
 </Location>
  ErrorLog ${APACHE_LOG_DIR}/error.log
  CustomLog ${APACHE_LOG_DIR}/access.log combined
 </Virtualhost>

... 从https://www.apachelounge.com/viewtopic.php?p=30200

...
 <Location "/">
           SetEnvIf Request_URI ^/rest noauth=1
           SetEnvIf Request_URI /rest noauth=1
           SetEnvIf Request_URI ^/index.php/rest noauth=1
           SetEnvIf Request_URI /index.php/rest noauth=1

           AuthType Basic
           AuthName "Keawe Development"
           AuthUserFile /host/.htpasswd
           Order Deny,Allow
           Satisfy any
           Deny from all
           Require valid-user
           Allow from env=noauth
 </Location>

... 从https://stackoverflow.com/a/8979889/1285585

 <Location "/">
   AuthType Basic
   AuthName "Keawe Development"
   AuthUserFile /host/.htpasswd
   Require valid-user
 </Location>
 <Location ~ "/(rest|index.php/rest)">
   Satisfy Any
   Allow from all
   AuthType None
   Require all granted
 </Location>

... 从https://stackoverflow.com/a/13296294/1285585

<Location "/">
  AuthType Basic
  AuthName "Keawe Development"
  AuthUserFile /host/.htpasswd
  Require valid-user
</Location> 
<Files "index.php/rest">
   Satisfy Any
   Allow from all
</Files>
<Files "rest">
   Satisfy Any
   Allow from all
</Files>

... 从HTTP 基本身份验证排除单个文件

然而,它们似乎都不起作用。我总是使用 wget 或来自浏览器的 auth 请求收到错误 401。

问题似乎是,路径 /rest 通过了条件,但随后被重写为 index.php,它受到基本身份验证的控制(并且必须如此)。

有什么线索吗?


当我偶然发现这个答案时终于弄清楚了(https://stackoverflow.com/a/14010456/1285585)到一个相关的问题。

这是我的解决方案:

<VirtualHost *:80>
  ServerAdmin webmaster@localhost
  DocumentRoot /var/www/webroot
  <Directory /var/www>
    Options FollowSymLinks
    AllowOverride All
 </Directory>

 <Location "/">
    # Default to Basic Auth protection for any stie
    AuthType Basic
    AuthName "Keawe Development"
    AuthUserFile /host/.htpasswd
    Require valid-user

    # If the request goes to a rest page: bypass basic auth
    SetEnvIf Request_URI ^/rest/ noauth=1
    Allow from env=REDIRECT_noauth
    Allow from env=noauth

    Order Deny,Allow
    Satisfy any
    Deny from all
  </Location>

  ErrorLog ${APACHE_LOG_DIR}/error.log
  CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

从 http 基本身份验证中排除特定的 cakephp 控制器 的相关文章

随机推荐