繁体   English   中英

.htaccess基本身份验证和IP限制

[英].htaccess basic auth combined with ip restriction

我想使用.htaccess配置阻止来自我网站的路径。 这个想法是,在使用基本身份验证进行身份验证之后,只有一组特定的IP才能从URL访问该特定路径。

注意:这是路径,而不是页面或目录。 我们正在尝试屏蔽Web服务,以便仅发布对URL的调用。

我希望网址example.com/rest被阻止,并且该网址后面的所有内容均基于IP。 因此, example.com/rest/foo / example.com/rest/foo/bar example.com/rest/fooexample.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 / *的所有访问,但阻止了对非休息路径的所有访问,它允许来自IP X的用户(Allow_Host变量),并且在这种情况下,我们不允许本地生产环境。

我试图通过基本身份验证来扩展此功能,如下所示:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM