
我試圖限制包含靜態報告的應用程式的每個客戶端子目錄,使用以程式設計方式產生的.htaccess 文件,其中包含每個客戶的用戶列表,但雖然我可以證明.htaccess 文件正在被解析(如果包含,則為500 ISE)垃圾,AllowOverride 正在工作)沒有任何更具限制性的 Require 語句會覆蓋更高級別 Location 指令中的 Require valid-user。
例如。 /etc/httpd/conf.d/app.conf
<VirtualHost *:80>
ServerName app
RewriteEngine on
RewriteRule ^/cgi-bin\/api\/(\w+)$ /var/www/cgi-bin/App.cgi?mode=json&request=$1 [QSA,H=cgi-script]
<Directory "/var/www/cgi-bin/">
Options ExecCGI
</Directory>
<Directory "/var/www/cgi-bin/api/reports/">
AllowOverride FileInfo AuthConfig
Options +Indexes +FollowSymLinks -ExecCGI
IndexOptions +NameWidth=*
SetHandler default-handler
LogLevel trace8
</Directory>
<Location /cgi-bin/>
AuthType Basic
AuthName "App Server"
AuthUserFile /etc/httpd/conf.d/app.passwd
require valid-user
</Location>
<Location /cgi-bin/api/>
AuthType Basic
AuthName "App API"
AuthUserFile /etc/httpd/conf.d/app-api.passwd
Require valid-user
LogLevel trace8
</Location>
</VirtualHost>
/var/www/cgi-bin/api/reports/customer1/.htaccess
Require user CUSTOMER1
我為報表目錄開啟了 LogLevel trace8,我看到
AH01626: authorization result of Require valid-user : denied (no authenticated user yet)
AH01626: authorization result of <RequireAny>: denied (no authenticated user yet)
AH01626: authorization result of Require valid-user : granted,
AH01626: authorization result of <RequireAny>: granted
Response sent with status 200
我在.htaccess 檔案中放入的任何內容似乎都沒有被引用,將Require 語句包裝在RequireAll 中不會執行任何操作,設定Require all returned 不會執行任何操作,Apache 不會將.htaccess 指令視為比中的Location 指令更具體URI 的(上、左)部分的配置。
編輯:嘗試新增 FileInfo 但沒有效果。更新了範例以使其更加完整。設定位置 /cgi-bin/api/reports/customer1 需要使用者 CUSTOMER1 在設定中運作良好,我只是試圖避免每次作業執行時間都需要重新載入網頁伺服器並在某些作業執行時重建 .htaccess AuthZ 檔案每分鐘,我不想花費精力建立一個更完整的工具,只有在發生重大AuthZ 更改時才能重新加載httpd,因為理論上.htaccess 應該能夠使apache 在每次訪問時檢查AuthZ 更改,而無需特權重新加載。
答案1
在最初的問題之後回答
<Directory>
和<Location>
指令不允許在.htaccess文件(參見情境部分)。
只需放置一個單獨的.htaccess文件內的/foo/bar/報告/包含Auth*
和Require
指令的資料夾。確保您的路徑.htpasswd文件有效(應首選絕對路徑)。
如果仍然不起作用,請檢查AllowOverride
指令設定正確(即在<Directory>
指令)內虛擬主機/伺服器配置(.conf文件內的/etc/apache2/資料夾)。
問題第一次編輯後回答
A.htaccessfile 相當於<Directory>
a 中的指令.conf文件。目前您正在混合<Directory>
和<Location>
指令進行身份驗證。請您<Location>
從您的.conf將您的指示歸檔並替換<Directory>
為以下內容,同時讓.htaccess未受影響的文件:
<Directory "/var/www/cgi-bin/">
Options ExecCGI
AuthType Basic
AuthName "App Server"
AuthUserFile /etc/httpd/conf.d/app.passwd
require valid-user
</Directory>
<Directory "/var/www/cgi-bin/api/">
AuthType Basic
AuthName "App API"
AuthUserFile /etc/httpd/conf.d/app-api.passwd
Require valid-user
LogLevel trace8
</Directory>
<Directory "/var/www/cgi-bin/api/reports/">
AllowOverride FileInfo AuthConfig
Options +Indexes +FollowSymLinks -ExecCGI
IndexOptions +NameWidth=*
SetHandler default-handler
LogLevel trace8
</Directory>
在您編輯問題之前,我假設這些<Location>
指令被放置在.htaccess文件並將指向虛擬資料夾而不是實際的檔案系統位置。但是,那手冊明確指出:
<Location>
當嘗試限制對檔案系統中物件的存取時,切勿使用,這一點很重要。這是因為許多不同的網路空間位置 (URL) 可以對應到相同的檔案系統位置,這允許您繞過限制。