Skip to content

Instantly share code, notes, and snippets.

@wancaibida
Created July 20, 2017 03:10
Show Gist options
  • Save wancaibida/b03e09bfd42e7d9fce3cac73f09f510c to your computer and use it in GitHub Desktop.
Save wancaibida/b03e09bfd42e7d9fce3cac73f09f510c to your computer and use it in GitHub Desktop.
Get real ip from request
public static String getClientIP(HttpServletRequest request)
{
String ip = request.getHeader("X-Real-IP");
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
ip = request.getHeader("X-Forwarded-For");
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
ip = request.getHeader("Proxy-Client-IP");
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
ip = request.getHeader("WL-Proxy-Client-IP");
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
ip = request.getRemoteAddr();
return ip;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment