Skip to content

Instantly share code, notes, and snippets.

@yanweijia
Created March 27, 2018 18:27
Show Gist options
  • Save yanweijia/dde6d9871c3efeebbde253fce8c0c016 to your computer and use it in GitHub Desktop.
Save yanweijia/dde6d9871c3efeebbde253fce8c0c016 to your computer and use it in GitHub Desktop.
get origin ip address, 获取用户真实 ip
public static String getIpAddress(HttpServletRequest request) {
String 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.getHeader("HTTP_CLIENT_IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("HTTP_X_FORWARDED_FOR");
}
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