У меня есть статический HTML-сайт, который обслуживается Apache 2.4 с использованием SSI. Я использовал базовую проверку подлинности для управления доступом к подмножеству файлов, и она перестала работать должным образом. По сути, все, что я хочу сделать, это потребовать имя пользователя / пароль для некоторых частей веб-сайта. Я включил то, что я считаю соответствующей конфигурацией, с измененными именами для защиты конфиденциальности.
/etc/apache2/sites-enabled/example.conf
<VirtualHost *:80>
ServerName site.example.com:80
ServerAdmin webmaster@localhost
DocumentRoot /var/www/site
<Directory /var/www/site/>
Options Includes Indexes FollowSymLinks MultiViews
AllowOverride All
Require all granted
</Directory>
ErrorLog /var/log/apache2/error.log
</VirtualHost>
/var/www/site/.htaccess
AuthType Basic
AuthName "Site Access Control"
AuthBasicProvider file
AuthUserFile /var/www/site/passwords
/var/www/site/dir1/dir2/protected-dir/.htaccess
require valid-user
Насколько я понимаю, я настроил каталог сайта для своего виртуального хоста. В каталоге верхнего уровня я настроил базовую аутентификацию. В тех каталогах, где я хочу контролировать доступ, мне нужен действующий пользователь. Файлы .htaccess должны соответствующим образом объединиться.
Когда я просматриваю:
site.example.com/dir1/dir2/protected-dir
Мне предоставлен доступ к странице, и ее содержимое видно. Я не ожидал этого. В журнале ошибок Apache2 я получаю следующее:
[Sat Jul 04 11:03:12.073970 2015] [deflate:debug] [pid 19576] mod_deflate.c(855): [client 192.168.50.242:63254] AH01384: Zlib: Compressed 3036 to 656 : URL /dir1/dir2/protected-dir/index.shtml, referer: http://site.example.com/dir1/dir2/
[Sat Jul 04 11:03:12.095014 2015] [authz_core:debug] [pid 19576] mod_authz_core.c(802): [client 192.168.50.242:63254] AH01626: authorization result of Require valid-user : denied (no authenticated user yet), referer: http://site.example.com/dir1/dir2/protected-dir/
[Sat Jul 04 11:03:12.095044 2015] [authz_core:debug] [pid 19576] mod_authz_core.c(802): [client 192.168.50.242:63254] AH01626: authorization result of <RequireAny>: denied (no authenticated user yet), referer: http://site.example.com/dir1/dir2/protected-dir/
[Sat Jul 04 11:03:12.095721 2015] [authz_core:debug] [pid 19576] mod_authz_core.c(802): [client 192.168.50.242:63254] AH01626: authorization result of Require valid-user : granted, referer: http://site.example.com/dir1/dir2/protected-dir/
[Sat Jul 04 11:03:12.095741 2015] [authz_core:debug] [pid 19576] mod_authz_core.c(802): [client 192.168.50.242:63254] AH01626: authorization result of <RequireAny>: granted, referer: http://site.example.com/dir1/dir2/protected-dir/
[Sat Jul 04 11:03:12.095994 2015] [deflate:debug] [pid 19576] mod_deflate.c(855): [client 192.168.50.242:63254] AH01384: Zlib: Compressed 1397 to 481 : URL /dir1/dir2/protected-dir/style.css, referer: http://site.example.com/dir1/dir2/protected-dir/
Можете ли вы определить, что не так с моей конфигурацией?
I am granted access to the page and its contents are visible. This is not what I expect.
Однако конфигурация содержит Require all granted
поэтому ожидается, что доступ к странице и ее содержимому будет предоставлен.
Объяснение
The all provider mimics the functionality that was previously provided by the
'Allow from all' and 'Deny from all' directives. This provider can take one of two arguments
which are 'granted' or 'denied'. The following examples will grant or deny access to all
requests.
Require all granted
Require all denied
Как решить вопрос
Вы могли бы использовать mod_authn_core
Создание псевдонимов поставщика аутентификации
Extended authentication providers can be created within the configuration file and assigned
an alias name. The alias providers can then be referenced through the directives
AuthBasicProvider or AuthDigestProvider in the same way as a base authentication provider.
Besides the ability to create and alias an extended provider, it also allows the same
extended authentication provider to be reference by multiple locations.
Examples
This example checks for passwords in two different text files.
Checking multiple text password files
# Check here first
<AuthnProviderAlias file file1>
AuthUserFile "/www/conf/passwords1"
</AuthnProviderAlias>
# Then check here
<AuthnProviderAlias file file2>
AuthUserFile "/www/conf/passwords2"
</AuthnProviderAlias>
<Directory "/var/web/pages/secure">
AuthBasicProvider file1 file2
AuthType Basic
AuthName "Protected Area"
Require valid-user
</Directory>
Основываясь на другом ответе, я обнаружил, что объединение двух файлов .htaccess в один и размещение его в каталогах, которые я хотел защитить, помогло. Вот окончательный файл:
AuthType Basic
AuthName "Site Access Control"
AuthBasicProvider file
AuthUserFile /var/www/site/passwords
require valid-user