如何从ContainerRequestContext JERSEY 2.1获取IP? [英] How to get IP from ContainerRequestContext JERSEY 2.1?
问题描述
我的代码是这样的:
public void filter(ContainerRequestContext request) throws IOException
{
// can I get Ip from request?????
}
如何从请求
?
推荐答案
根据这个 JIRA 票据,基于此讨论,他们添加了对将 HttpServletRequest
注入过滤器的支持,上面的JIRA看起来是一段代码片段像这样:
According to this JIRA ticket, based on this discussion, they added support for injecting the HttpServletRequest
into your filter, a code snippet from the above JIRA looks like this:
public class MyRequestFilter implements ContainerRequestFilter {
@Context
private HttpServletRequest servletRequest;
然后您可以使用 HttpServletRequest
API来获取远程IP,请参阅完整的 Javadoc ,摘录:
You can then use the HttpServletRequest
API to get the Remote IP, see the full Javadoc here, excerpt:
getRemoteAddr
getRemoteAddr
java.lang.String getRemoteAddr()
java.lang.String getRemoteAddr()
返回发送请求的客户端或最后一个代理的Internet协议(IP)地址。对于HTTP servlet,与CGI变量REMOTE_ADDR的值相同。
Returns the Internet Protocol (IP) address of the client or last proxy that sent the request. For HTTP servlets, same as the value of the CGI variable REMOTE_ADDR.
返回:
包含发送请求的客户端的IP地址
a String containing the IP address of the client that sent the request
这篇关于如何从ContainerRequestContext JERSEY 2.1获取IP?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!