1

Ich versuche, einen Pfad (URI) von basischen HTTP-Auth blockiert werden. Der Pfad ist/rest (http://example.com/rest) und repräsentiert einen Controller einer CakePHP 3-Anwendung. Es handelt sich NICHT um eine echte Datei, sondern um einen Pfad, der von einer rewite-Bedingung umgeschrieben und von index.php im Webroot-Verzeichnis bearbeitet wurde.Ausschließen bestimmter CakePHP-Controller von http Basic Auth

Hier ist die Rewrite-Regeln:

/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> 

Ich bin mit Apache 2.4 und versucht, verschiedene Konfigurationen:

<VirtualHost *:80> 
    ServerAdmin [email protected] 
    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> 

... adaptiert von https://stackoverflow.com/a/33655232/1285585

<VirtualHost *:80> 
    ServerAdmin [email protected] 
    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> 

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

<VirtualHost *:80> 
    ServerAdmin [email protected] 
    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> 

... von 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> 

... von 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> 

... von 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> 

... von HTTP Basic Auth Exclude Single File

jedoch keiner von ihnen zu arbeiten scheinen. Ich bekomme immer Fehler 401 mit wget oder eine Auth-Anfrage von einem Browser.

Das Problem scheint zu sein, dass der Pfad/Rest die Bedingung übergibt, aber dann in index.php umgeschrieben wird, was unter der Kontrolle von basic auth steht (und sein muss).

Irgendwelche Hinweise?

Antwort

5

Endlich herausgefunden, als ich auf diese Antwort (https://stackoverflow.com/a/14010456/1285585) auf eine verwandte Frage stumbledled.

Hier ist meine Lösung:

<VirtualHost *:80> 
    ServerAdmin [email protected] 
    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> 
+0

Was bedeutet 'REDIRECT_noauth' bedeuten? – Shoan

+0

Wenn eine Umleitung erfolgt, wird die SetEnvIf-Regel zweimal angewendet: einmal vor und einmal nach der Umleitung. Die noauth-Umgebungsvariable wird gesetzt, wenn der Request_URI nach der Umleitung mit dem angegebenen Token '^/rest /' übereinstimmt, während REDIRECT_noaut gesetzt ist, wenn der Request_URI vor dem Umleiten übereinstimmt. –