Created
October 13, 2016 16:49
-
-
Save jpe42/c4021c69595e97cd6241229f8699be85 to your computer and use it in GitHub Desktop.
Get requestor ip
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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