Skip to content

Instantly share code, notes, and snippets.

@Chen-tao
Last active August 29, 2015 14:13
Show Gist options
  • Save Chen-tao/9098fff8c9493d96e748 to your computer and use it in GitHub Desktop.
Save Chen-tao/9098fff8c9493d96e748 to your computer and use it in GitHub Desktop.
public String getIpAddr(HttpServletRequest request) {
String ip = request.getHeader("x-forwarded-for");
AddrLog.debug("x-forwarded-for clientIP:"+ip);
AddrLog.debug("remoteAddr:"+request.getRemoteAddr());
if(ip !=null && !ip.equals("") && ip.length() > 0){
//获取第一个ip
String[] ipList = ip.split(" ");
if(ipList.length > 0){
ip = ipList[0];
}
}
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