.htaccess 基本身份验证与 ip 限制相结合
.htaccess basic auth combined with ip restriction
我想使用 .htaccess 配置阻止来自我的站点的路径。这个想法是,只有一组特定的 IP 在使用基本身份验证进行身份验证后才能从 URL 访问该特定路径。
注意:是路径,不是页面或目录。我们正在尝试屏蔽 Web 服务,因此只有 post 调用 URL。
我希望根据 IP 阻止 url example.com/rest
以及 url 后面的所有内容。所以 example.com/rest/foo
和 example.com/rest/foo/bar
应该被阻止。
来自应用程序的所有其他路径应保持正常运行且无需基本身份验证。
IP屏蔽部分已经在之前的中解决了
基本配置(阻塞部分,.htaccess中有更多,但与本题无关。)你可以在下面找到。
SetEnvIf Request_URI "/rest(/.*)?$" rest_uri
# Check on what subdomain we are.
SetEnvIf Host ^local\. None_Prod_Env
# Static
SetEnvIf AH_CLIENT_IP ^123\.123\.123\.123$ Allow_Host
# Range
SetEnvIf AH_CLIENT_IP ^123\.123\.123\. Allow_Host
Order deny,allow
Deny from all
Allow from env=!rest_uri
Allow from env=Allow_Host
Allow from env=None_Prod_Env
所以上面的配置阻止所有对 /rest/* 的访问,但不阻止对非 rest 路径的访问,它允许来自 IP X 的用户(Allow_Host 变量)并且我们允许 none 生产环境在这种情况下是本地的。
我尝试使用基本身份验证扩展此功能,如下所示:
SetEnvIf Request_URI "/rest(/.*)?$" rest_uri
SetEnvIfNoCase Request_URI "/rest(/.*)?$" require_auth=true
# ... Allow Host stuff and none prod stuff ...
Order deny,allow
Deny from all
Allow from env=!rest_uri
Allow from env=Allow_Host
Allow from env=None_Prod_Env
AuthName "Password Protected"
AuthType Basic
AuthBasicProvider file
AuthUserFile /var/www/html/.htpasswd
Require valid-user
然而,这导致了所有页面的基本身份验证,而不仅仅是 /rest/* url。我玩了很多,但无法弄清楚。将 SetEnvIfNoCase
更改为 SetEnvIf
也没有帮助。
注意:我们的服务器是 运行 apache 2.2.22.
尝试将 satisfy any
添加到您的代码中。试试这个方法。
SetEnvIf Request_URI "/rest(/.*)?$" rest_uri
SetEnvIf Referer "^http://local\.example\.com/" None_Prod_Env
AuthName "Password Protected"
AuthType Basic
AuthBasicProvider file
AuthUserFile /var/www/html/.htpasswd
Require valid-user
Order deny,allow
Deny from all
Allow from env=!rest_uri
Allow from env=Allow_Host
Allow from env=None_Prod_Env
Satisfy any
您可以使用几个 Apache 指令的组合来解决这个复杂的问题,即 mod_dir
、mod_setenv
和 mod_auth_basic
:
SetEnvIf Request_URI ^/rest(/.*)?$ rest_uri
# Check on what subdomain we are.
SetEnvIf Host ^local None_Prod_Env
# Static
SetEnvIf AH_CLIENT_IP ^123\.123\.123\.123$ Allow_Host
# Range
SetEnvIf AH_CLIENT_IP ^192\.168\. Allow_Host
RewriteEngine On
# block if request is /rest/* and IP is not whitelisted and not localhost
RewriteCond %{ENV:rest_uri} =1
RewriteCond %{ENV:None_Prod_Env} !=1
RewriteCond %{ENV:Allow_Host} !=1
RewriteRule ^ - [F]
# ask auth for /rest/* && NOT localhost && whitelist IP
AuthType Basic
AuthName "Password Protected"
AuthUserFile /var/www/html/.htpasswd
Require valid-user
Order deny,allow
Deny from all
Allow from env=!rest_uri
Allow from env=!Allow_Host
Allow from env=None_Prod_Env
Satisfy any
我想使用 .htaccess 配置阻止来自我的站点的路径。这个想法是,只有一组特定的 IP 在使用基本身份验证进行身份验证后才能从 URL 访问该特定路径。
注意:是路径,不是页面或目录。我们正在尝试屏蔽 Web 服务,因此只有 post 调用 URL。
我希望根据 IP 阻止 url example.com/rest
以及 url 后面的所有内容。所以 example.com/rest/foo
和 example.com/rest/foo/bar
应该被阻止。
来自应用程序的所有其他路径应保持正常运行且无需基本身份验证。
IP屏蔽部分已经在之前的
基本配置(阻塞部分,.htaccess中有更多,但与本题无关。)你可以在下面找到。
SetEnvIf Request_URI "/rest(/.*)?$" rest_uri
# Check on what subdomain we are.
SetEnvIf Host ^local\. None_Prod_Env
# Static
SetEnvIf AH_CLIENT_IP ^123\.123\.123\.123$ Allow_Host
# Range
SetEnvIf AH_CLIENT_IP ^123\.123\.123\. Allow_Host
Order deny,allow
Deny from all
Allow from env=!rest_uri
Allow from env=Allow_Host
Allow from env=None_Prod_Env
所以上面的配置阻止所有对 /rest/* 的访问,但不阻止对非 rest 路径的访问,它允许来自 IP X 的用户(Allow_Host 变量)并且我们允许 none 生产环境在这种情况下是本地的。
我尝试使用基本身份验证扩展此功能,如下所示:
SetEnvIf Request_URI "/rest(/.*)?$" rest_uri
SetEnvIfNoCase Request_URI "/rest(/.*)?$" require_auth=true
# ... Allow Host stuff and none prod stuff ...
Order deny,allow
Deny from all
Allow from env=!rest_uri
Allow from env=Allow_Host
Allow from env=None_Prod_Env
AuthName "Password Protected"
AuthType Basic
AuthBasicProvider file
AuthUserFile /var/www/html/.htpasswd
Require valid-user
然而,这导致了所有页面的基本身份验证,而不仅仅是 /rest/* url。我玩了很多,但无法弄清楚。将 SetEnvIfNoCase
更改为 SetEnvIf
也没有帮助。
注意:我们的服务器是 运行 apache 2.2.22.
尝试将 satisfy any
添加到您的代码中。试试这个方法。
SetEnvIf Request_URI "/rest(/.*)?$" rest_uri
SetEnvIf Referer "^http://local\.example\.com/" None_Prod_Env
AuthName "Password Protected"
AuthType Basic
AuthBasicProvider file
AuthUserFile /var/www/html/.htpasswd
Require valid-user
Order deny,allow
Deny from all
Allow from env=!rest_uri
Allow from env=Allow_Host
Allow from env=None_Prod_Env
Satisfy any
您可以使用几个 Apache 指令的组合来解决这个复杂的问题,即 mod_dir
、mod_setenv
和 mod_auth_basic
:
SetEnvIf Request_URI ^/rest(/.*)?$ rest_uri
# Check on what subdomain we are.
SetEnvIf Host ^local None_Prod_Env
# Static
SetEnvIf AH_CLIENT_IP ^123\.123\.123\.123$ Allow_Host
# Range
SetEnvIf AH_CLIENT_IP ^192\.168\. Allow_Host
RewriteEngine On
# block if request is /rest/* and IP is not whitelisted and not localhost
RewriteCond %{ENV:rest_uri} =1
RewriteCond %{ENV:None_Prod_Env} !=1
RewriteCond %{ENV:Allow_Host} !=1
RewriteRule ^ - [F]
# ask auth for /rest/* && NOT localhost && whitelist IP
AuthType Basic
AuthName "Password Protected"
AuthUserFile /var/www/html/.htpasswd
Require valid-user
Order deny,allow
Deny from all
Allow from env=!rest_uri
Allow from env=!Allow_Host
Allow from env=None_Prod_Env
Satisfy any