Symfony2:如何通过 IP 地址限制/拒绝访问某些路由? [英] Symfony2: How to restrict / deny access to certain routes by IP address?
本文介绍了Symfony2:如何通过 IP 地址限制/拒绝访问某些路由?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果客户端的 IP 地址已被禁止,我想禁止访问 /login
和 /register
.
I'd like to disallow access to /login
and /register
if a client's IP address has been banned.
禁止IP的(黑)名单存储在数据库中.
The (black-) list of banned IPs is stored in the database.
我该如何解决这个问题?
How can I solve this?
推荐答案
从 symfony 2.4 开始你可以使用 表达式语言组件在您的配置文件中.
Since symfony 2.4 you can use the Expression Language Component in your config-files.
现在实现简单的 IP 检查很容易:
Now implementing a simple IP check is easy:
- 使用从存储层获取被禁止 IP 列表的方法(即
getBannedIPs()
)创建服务(即access_manager
) - 在您的安全配置中添加一个表达式,将返回的数组与客户端的 IP 地址进行比较
- 就是这样.
# app/config/security.yml
security:
# ...
access_control:
- path: ^/(login|register)$
allow_if: "request.getClientIp() not in @=service('access_manager').getBannedIPs()"
这篇关于Symfony2:如何通过 IP 地址限制/拒绝访问某些路由?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文