我的代码是这样的:
public void filter(ContainerRequestContext request) throws IOException
{
// can I get Ip from request?????
}
如何从request
获取IP地址?
最佳答案
根据这个JIRA门票,基于此 discussion ,他们添加了对将 HttpServletRequest
注入(inject)过滤器的支持,上述 JIRA 的代码片段如下所示:
public class MyRequestFilter implements ContainerRequestFilter {
@Context
private HttpServletRequest servletRequest;
然后您可以使用 HttpServletRequest
API 获取远程 IP,请参阅完整的 Javadoc here , 摘录:
getRemoteAddr
java.lang.String getRemoteAddr()
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.
Returns:
a String containing the IP address of the client that sent the request
关于java - 如何从 ContainerRequestContext JERSEY 2.1 获取 IP?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28280491/