29

I have created web application using JSF 2.0. I have hosted it on hosting site and the server of the hosting site is based in US.

My client want the details of the user who all accessed the site. How can I find the IP address of user in JSF?

I tried with

    try {
        InetAddress thisIp = InetAddress.getLocalHost();
        System.out.println("My IP is  " + thisIp.getLocalHost().getHostAddress());
    } catch (Exception e) {
        System.out.println("exception in up addresss");
    }

however this gives me ip address of my site only i.e. server ip address.

Could someone tell me how to get IP address who accessed the website using Java?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Fahim Parkar
  • 30,974
  • 45
  • 160
  • 276

3 Answers3

64

I went ahead with

HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
String ipAddress = request.getHeader("X-FORWARDED-FOR");
if (ipAddress == null) {
    ipAddress = request.getRemoteAddr();
}
System.out.println("ipAddress:" + ipAddress);
Fahim Parkar
  • 30,974
  • 45
  • 160
  • 276
  • 8
    `X-Forwarded-For` header is in commaseparated format. You should be interested in the first part only, if any. – BalusC Mar 02 '15 at 06:16
  • Does proxy have more than 1 server include? *reason:* I am using 10.11.101.33 as proxy and my IP is 10.11.33.102 and this code returns me 10.11.101.31. Does 33 serves 31 request first? While works fine when without proxy – Sarz Nov 23 '15 at 05:34
  • Not very good for testing since you depend on FacesContext =*( – de.la.ru Feb 05 '18 at 13:22
19

A more versatile solution

Improved version of the accepted answer that works even if there are multiple IP addresses in the X-Forwarded-For header:

/**
 * Gets the remote address from a HttpServletRequest object. It prefers the 
 * `X-Forwarded-For` header, as this is the recommended way to do it (user 
 * may be behind one or more proxies).
 *
 * Taken from https://stackoverflow.com/a/38468051/778272
 *
 * @param request - the request object where to get the remote address from
 * @return a string corresponding to the IP address of the remote machine
 */
public static String getRemoteAddress(HttpServletRequest request) {
    String ipAddress = request.getHeader("X-FORWARDED-FOR");
    if (ipAddress != null) {
        // cares only about the first IP if there is a list
        ipAddress = ipAddress.replaceFirst(",.*", "");
    } else {
        ipAddress = request.getRemoteAddr();
    }
    return ipAddress;
}
Lucio Paiva
  • 19,015
  • 11
  • 82
  • 104
4

Try this...

HttpServletRequest httpServletRequest = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();  
String ip = httpServletRequest.getRemoteAddr();  
Kumar Vivek Mitra
  • 33,294
  • 6
  • 48
  • 75
  • 8
    This fails if the client (or server) uses a proxy. – BalusC Sep 07 '12 at 20:10
  • 1
    @BalusC The X-Forwarded-For (XFF) HTTP header is a de facto standard for identifying the originating IP address of a client connecting to a web server through an HTTP proxy Wiki, But if the request is coming directly from client then it can return null. Another point is that there is no guarantee that the proxy server will pass that header for you. So, the fact that the header is null, doesn't necessarily mean that the IP returned by getRemoteAddr is the actual IP of the machine that made the original request. It could still be the IP of a proxy server. – Kumar Vivek Mitra Sep 07 '12 at 20:14
  • 5
    I know that. You have yet to fix your answer based on that. By the way, why don't you use your own words? Cite the sources instead of pretending as if it are your own words. See also the cc-wiki license here on SO. – BalusC Sep 07 '12 at 20:15
  • @BalusC...i would have, but the original writer has written so beautifully that i didn't wanted to take away its essence...before i was about to say this in my another comment..u asked for it... fine..here it is...http://www.coderanch.com/t/293684/JSP/java/client-IP-address-Domain-Java – Kumar Vivek Mitra Sep 07 '12 at 20:17