Created
March 27, 2018 18:27
-
-
Save yanweijia/dde6d9871c3efeebbde253fce8c0c016 to your computer and use it in GitHub Desktop.
get origin ip address, 获取用户真实 ip
This file contains hidden or 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
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