|
|
|
@ -11,6 +11,13 @@ import javax.servlet.http.HttpServletRequest;
|
|
|
|
|
*/
|
|
|
|
|
public class IpUtils
|
|
|
|
|
{
|
|
|
|
|
public final static String REGX_0_255 = "(25[0-5]|2[0-4]\\d|1\\d{2}|[1-9]\\d|\\d)";
|
|
|
|
|
// 匹配 ip
|
|
|
|
|
public final static String REGX_IP = "((" + REGX_0_255 + "\\.){3}" + REGX_0_255 + ")";
|
|
|
|
|
public final static String REGX_IP_WILDCARD = "(((\\*\\.){3}\\*)|(" + REGX_0_255 + "(\\.\\*){3})|(" + REGX_0_255 + "\\." + REGX_0_255 + ")(\\.\\*){2}" + "|((" + REGX_0_255 + "\\.){3}\\*))";
|
|
|
|
|
// 匹配网段
|
|
|
|
|
public final static String REGX_IP_SEG = "(" + REGX_IP + "\\-" + REGX_IP + ")";
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取客户端IP
|
|
|
|
|
*
|
|
|
|
@ -247,7 +254,7 @@ public class IpUtils
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return ip;
|
|
|
|
|
return StringUtils.substring(ip, 0, 255);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -260,4 +267,104 @@ public class IpUtils
|
|
|
|
|
{
|
|
|
|
|
return StringUtils.isBlank(checkString) || "unknown".equalsIgnoreCase(checkString);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 是否为IP
|
|
|
|
|
*/
|
|
|
|
|
public static boolean isIP(String ip)
|
|
|
|
|
{
|
|
|
|
|
return StringUtils.isNotBlank(ip) && ip.matches(REGX_IP);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 是否为IP,或 *为间隔的通配符地址
|
|
|
|
|
*/
|
|
|
|
|
public static boolean isIpWildCard(String ip)
|
|
|
|
|
{
|
|
|
|
|
return StringUtils.isNotBlank(ip) && ip.matches(REGX_IP_WILDCARD);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 检测参数是否在ip通配符里
|
|
|
|
|
*/
|
|
|
|
|
public static boolean ipIsInWildCardNoCheck(String ipWildCard, String ip)
|
|
|
|
|
{
|
|
|
|
|
String[] s1 = ipWildCard.split("\\.");
|
|
|
|
|
String[] s2 = ip.split("\\.");
|
|
|
|
|
boolean isMatchedSeg = true;
|
|
|
|
|
for (int i = 0; i < s1.length && !s1[i].equals("*"); i++)
|
|
|
|
|
{
|
|
|
|
|
if (!s1[i].equals(s2[i]))
|
|
|
|
|
{
|
|
|
|
|
isMatchedSeg = false;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return isMatchedSeg;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 是否为特定格式如:“10.10.10.1-10.10.10.99”的ip段字符串
|
|
|
|
|
*/
|
|
|
|
|
public static boolean isIPSegment(String ipSeg)
|
|
|
|
|
{
|
|
|
|
|
return StringUtils.isNotBlank(ipSeg) && ipSeg.matches(REGX_IP_SEG);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 判断ip是否在指定网段中
|
|
|
|
|
*/
|
|
|
|
|
public static boolean ipIsInNetNoCheck(String iparea, String ip)
|
|
|
|
|
{
|
|
|
|
|
int idx = iparea.indexOf('-');
|
|
|
|
|
String[] sips = iparea.substring(0, idx).split("\\.");
|
|
|
|
|
String[] sipe = iparea.substring(idx + 1).split("\\.");
|
|
|
|
|
String[] sipt = ip.split("\\.");
|
|
|
|
|
long ips = 0L, ipe = 0L, ipt = 0L;
|
|
|
|
|
for (int i = 0; i < 4; ++i)
|
|
|
|
|
{
|
|
|
|
|
ips = ips << 8 | Integer.parseInt(sips[i]);
|
|
|
|
|
ipe = ipe << 8 | Integer.parseInt(sipe[i]);
|
|
|
|
|
ipt = ipt << 8 | Integer.parseInt(sipt[i]);
|
|
|
|
|
}
|
|
|
|
|
if (ips > ipe)
|
|
|
|
|
{
|
|
|
|
|
long t = ips;
|
|
|
|
|
ips = ipe;
|
|
|
|
|
ipe = t;
|
|
|
|
|
}
|
|
|
|
|
return ips <= ipt && ipt <= ipe;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 校验ip是否符合过滤串规则
|
|
|
|
|
*
|
|
|
|
|
* @param filter 过滤IP列表,支持后缀'*'通配,支持网段如:`10.10.10.1-10.10.10.99`
|
|
|
|
|
* @param ip 校验IP地址
|
|
|
|
|
* @return boolean 结果
|
|
|
|
|
*/
|
|
|
|
|
public static boolean isMatchedIp(String filter, String ip)
|
|
|
|
|
{
|
|
|
|
|
if (StringUtils.isEmpty(filter) && StringUtils.isEmpty(ip))
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
String[] ips = filter.split(";");
|
|
|
|
|
for (String iStr : ips)
|
|
|
|
|
{
|
|
|
|
|
if (isIP(iStr) && iStr.equals(ip))
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
else if (isIpWildCard(iStr) && ipIsInWildCardNoCheck(iStr, ip))
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
else if (isIPSegment(iStr) && ipIsInNetNoCheck(iStr, ip))
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|