Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get IP from ContainerRequestContext JERSEY 2.1?

My code is this:

public void filter(ContainerRequestContext request) throws IOException 
  {
   // can I get Ip from request?????

 }

How can I get ip address from request?

like image 854
unknownbits Avatar asked Feb 02 '15 14:02

unknownbits


1 Answers

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;

You can then use the HttpServletRequest API to get the Remote IP, see the full Javadoc here, excerpt:

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

like image 68
Alex Avatar answered Oct 01 '22 16:10

Alex