简体   繁体   English

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

[英].htaccess basic auth combined with ip restriction

I would like to block a path from my site using the .htaccess configuration. 我想使用.htaccess配置阻止来自我网站的路径。 The idea is that only a specific set of IP's can access that specific path from the URL after they authenticated using basic auth. 这个想法是,在使用基本身份验证进行身份验证之后,只有一组特定的IP才能从URL访问该特定路径。

Note: It's a path, not a page or directory. 注意:这是路径,而不是页面或目录。 We are trying to shield off a web-service so there will be only post calls to the URL's. 我们正在尝试屏蔽Web服务,以便仅发布对URL的调用。

I would like the url example.com/rest to be blocked and everything behind that url based on IP. 我希望网址example.com/rest被阻止,并且该网址后面的所有内容均基于IP。 So example.com/rest/foo and example.com/rest/foo/bar should be blocked. 因此, example.com/rest/foo / example.com/rest/foo/bar example.com/rest/fooexample.com/rest/foo/bar应该被阻止。

All other paths from the application should remain functional and without basic auth. 来自应用程序的所有其他路径应保持正常运行,并且没有基本身份验证。

The IP blocking part has been resolved in a previous question I asked. 我之前提出的问题已解决了IP阻止部分。

The basic configuration (the blocking part, there is more in the .htaccess but is not relevant to this question.) you can find below. 您可以在下面找到基本配置(阻止部分,.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

So the configuration above blocks all access to /rest/* but not to non rest paths, it allows a user coming from IP X (Allow_Host variable) and we allow none production environments in this case local. 因此,上面的配置阻止了对/ rest / *的所有访问,但阻止了对非休息路径的所有访问,它允许来自IP X的用户(Allow_Host变量),并且在这种情况下,我们不允许本地生产环境。

I tried to extend this functionality with basic auth like so: 我试图通过基本身份验证来扩展此功能,如下所示:

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

However this resulted in a basic auth on all pages and not only for the /rest/* url. 但是,这导致了所有页面上的基本身份验证,而不仅仅是/ rest / * URL。 I played a lot with it but couldn't figure it out. 我玩了很多,但无法弄清楚。 Changing SetEnvIfNoCase to SetEnvIf also didn't help. SetEnvIfNoCase更改为SetEnvIf也没有帮助。

Note: Our server is running apache 2.2.22. 注意:我们的服务器正在运行apache 2.2.22。

Try and add satisfy any to your code. 尝试添加satisfy any的代码。 Give it a try this way. 尝试这种方式。

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

You can solve this complex problem using a combination of few Apache directives ie mod_dir , mod_setenv and mod_auth_basic : 您可以结合使用几个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