50 lines
1.3 KiB
Java
50 lines
1.3 KiB
Java
package com.adc.da.util;
|
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
|
public class IpUtil {
|
|
private IpUtil() {
|
|
throw new IllegalStateException("Utility class");
|
|
}
|
|
|
|
private static boolean checkIp(String ip) {
|
|
return ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip);
|
|
}
|
|
|
|
public static String getIpAddr(HttpServletRequest request) {
|
|
String ip = request.getHeader("X-Forwarded-For");
|
|
if (checkIp(ip)) {
|
|
ip = request.getHeader("Proxy-Client-IP");
|
|
if (checkIp(ip)) {
|
|
ip = request.getHeader("WL-Proxy-Client-IP");
|
|
}
|
|
|
|
if (checkIp(ip)) {
|
|
ip = request.getHeader("HTTP_CLIENT_IP");
|
|
}
|
|
|
|
if (checkIp(ip)) {
|
|
ip = request.getHeader("HTTP_X_FORWARDED_FOR");
|
|
}
|
|
|
|
if (checkIp(ip)) {
|
|
ip = request.getRemoteAddr();
|
|
}
|
|
} else if (ip.length() > 15) {
|
|
String[] ips = ip.split(",");
|
|
|
|
for(int index = 0; index < ips.length; ++index) {
|
|
String strIp = ips[index];
|
|
if (!"unknown".equalsIgnoreCase(strIp)) {
|
|
ip = strIp;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
return StringUtils.trim(ip);
|
|
}
|
|
}
|