tags:

views:

148

answers:

3

Hi,

I'm writing a web application, I need to do a audit log for all the actions in the application. For this purpose I need to get the IP Address of the client systems.

I'm using request.getRemoteAddr() to get the remote IP Address. But this has a problem, if the client is behind a proxy this method will give the IP of the proxy system.

When I did some search I found a header attribute called 'X-FORWARDED-FOR' in the HttpRequest object.

Can somebody tell me how exactly this header property works and how can I used this header to get the IP address of the client system.

Thank you

+2  A: 

The client's proxy - typically a firewall or somesuch - will populate the x-forwarded-for header with the ip it receives from its client, which is typically, but is not required to be (in the case of a user going through multiple proxies or firewalls) the ip of the user's machine.

David Lively
+2  A: 

'X-FORWARDED-FOR' is used for identifying the originating/actual IP address of a client connecting to a web server through an HTTP proxy.

You can simply use the value for this attribute to find out the originating client IP, even if it's behind a proxy.

Suraj Chandran
+3  A: 

getRemoteIP returns the remote IP address of the user (assuming all HTTP intermediaries are well behaved wrt XFF header).

String getRemoteIP(HttpServletRequest request) {
    String xff = request.getHeader("X-Forwarded-For");
    if (xff != null) {
        return xff.split("[\\s,]+")[0];
    }
    return request.getRemoteAddr();
}
Chandra Patni