Skip to content

Instantly share code, notes, and snippets.

@jpe42
Created October 13, 2016 16:49
Show Gist options
  • Save jpe42/c4021c69595e97cd6241229f8699be85 to your computer and use it in GitHub Desktop.
Save jpe42/c4021c69595e97cd6241229f8699be85 to your computer and use it in GitHub Desktop.
Get requestor ip
import javax.servlet.http.HttpServletRequest;
public class RequestUtil {
public static String getRequestIp(final HttpServletRequest request) {
String ip = request.getHeader("X-Forwarded-For");
if (ip == null) {
return request.getRemoteAddr();
}
ip = ip.trim();
final int multipleIpsIndex = ip.lastIndexOf(',');
if (multipleIpsIndex > 0) {
ip = ip.substring(multipleIpsIndex + 1);
}
return ip.isEmpty() ? request.getRemoteAddr() : ip;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment