如何使用 web.xml 阻止 IP 地址? [英] How to block a IP address using web.xml?
问题描述
如何使用 web.xml 上的一些配置来阻止 ip 地址?
How can I block a ip address with some configuration on web.xml?
我需要过滤器吗?我该如何实施?
Do I need a filter? How can I implement one?
推荐答案
你不能仅仅通过 web.xml
中的配置来做到这一点,不行.不过,servlet 过滤器是实现此类功能的好地方.
You can't do this purely through config in web.xml
, no. A servlet filter would be a good place to implement such a thing, though.
Filter
接口提供 HttpServletRequest
作为过滤器链调用的一部分,您可以从中获取客户端的 IP 地址(使用 getRemoteAddr
),并将其与您的允许地址列表进行比较.
The Filter
interface supplies the HttpServletRequest
as part of the filter chain invocation, and from that you can get the IP address of the client (using getRemoteAddr
), and compare that to your list of permitted addresses.
或者,您的特定应用服务器可能支持专有级别的 IP 过滤,但这会将您锁定在该容器中(这对您来说可能是问题,也可能不是问题).
Alternatively, your specific appserver might support IP filtering at a proprietary level, but that locks you into that container (which may or may not be a problem for you).
这篇关于如何使用 web.xml 阻止 IP 地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!