In tomcat under conf/server.xml. Update below code
pattern="%{X-Forwarded-For}i %h %l %u %t "%r" %s %b"
resolveHosts="false" />
Java Code
The following Java code extracts the originating IP address of an HttpServletRequest object.
public final class HTTPUtils {
private static final String HEADER_X_FORWARDED_FOR =
"X-FORWARDED-FOR";
public static String remoteAddr(HttpServletRequest request) {
String remoteAddr = request.getRemoteAddr();
String x;
if ((x = request.getHeader(HEADER_X_FORWARDED_FOR)) != null) {
remoteAddr = x;
int idx = remoteAddr.indexOf(',');
if (idx > -1) {
remoteAddr = remoteAddr.substring(0, idx);
}
}
return remoteAddr;
}
}