记录一次局域网IP和公网IP的范围校验.
还是可以继续优化的, 不过现在也能用了
判断IP是否在指定范围的方法需要搭配设置IP范围方法使用, 或者自己组合一下
public static boolean ipIsSpecifyRange(String ipSection) {
if (ipSection == null) {
return false;
}
ipSection = ipSection.trim();
if (!ipSection.contains("~")){
return false;
}
String[] split = ipSection.split("~");
if (split.length != 2){
return false;
}
String ip0 = split[0];
String ip1 = split[1];
if (StringUtils.isNotEmpty(ip0) && StringUtils.isNotEmpty(ip1)) {
String REGX_IP = "^(10\\.\\d{1,3}\\.\\d{1,3}\\.((0\\/([89]|1[0-9]|2\\d|3[012]))|(\\d{1,3})))|(172\\.(1[6789]|2\\\\d|3[01])\\.\\d{1,3}\\" +
".\\d{1,3}(\\/(1[6789]|2\\d|3[012]))?)|(192\\.168\\.\\d{1,3}\\.\\d{1,3}(\\/(1[6789]|2\\d|3[012]))?)$";
if (!ip0.matches(REGX_IP) || !ip1.matches(REGX_IP)) {
return false;
}
if (ip0.startsWith("10") && !ip1.startsWith("10")) {
return false;
} else if (!ip0.substring(0, 3).equals(ip1.substring(0, 3))) {
return false;
}
String REGX_IP2 = "((25[0-5]|2[0-4]\\d|1\\d{2}|[1-9]\\d|\\d)\\.){3}(25[0-5]|2[0-4]\\d|1\\d{2}|[1-9]\\d|\\d)";
String REGX_IPB = REGX_IP2 + "~" + REGX_IP2;
boolean matches = ipSection.matches(REGX_IPB);
if (matches) {
String[] split0 = ip0.split("\\.");
String[] split1 = ip1.split("\\.");
for (int i = 0; i < split0.length; i++) {
int sp0 = Integer.parseInt(split0[i]);
int sp1 = Integer.parseInt(split1[i]);
if (sp0 > sp1) {
return false;
}
}
}
return true;
} else {
return false;
}
}
public static boolean ipIsSpecifyRangePublic(String ipSection) {
if (ipSection == null) {
return false;
}
if (!ipIsSpecifyRange(ipSection)) {
ipSection = ipSection.trim();
String[] split = ipSection.split("~");
String ip0 = split[0];
String ip1 = split[1];
if (StringUtils.isNotEmpty(ip0) && StringUtils.isNotEmpty(ip1)) {
int num = ip0.indexOf(".");
if (!ip0.substring(0, num).equals(ip1.substring(0, num))) {
return false;
}
String REGX_IP = "((2(5[0-5]|[0-4]\\d))|[0-1]?\\d{1,2})(\\.((2(5[0-5]|[0-4]\\d))|[0-1]?\\d{1,2})){3}";
if (!ip0.matches(REGX_IP) || !ip1.matches(REGX_IP)) {
return false;
}
String REGX_IP2 = "((25[0-5]|2[0-4]\\d|1\\d{2}|[1-9]\\d|\\d)\\.){3}(25[0-5]|2[0-4]\\d|1\\d{2}|[1-9]\\d|\\d)";
String REGX_IPB = REGX_IP2 + "~" + REGX_IP2;
boolean matches = ipSection.matches(REGX_IPB);
if (matches) {
String[] split0 = ip0.split("\\.");
String[] split1 = ip1.split("\\.");
for (int i = 0; i < split0.length; i++) {
int sp0 = Integer.parseInt(split0[i]);
int sp1 = Integer.parseInt(split1[i]);
if (sp0 > sp1) {
return false;
}
}
}
return true;
}
} else {
return false;
}
return false;
}
public static boolean ipIsSpecifyRange(String ipSection, String ip) {
if (ipSection == null){
throw new NullPointerException("IP段不能为空!");
}
if (ip == null){
throw new NullPointerException("IP不能为空!");
}
if (ip.startsWith("0:0:0:0:0:0:0:1") || ip.startsWith("127.0.0.") || ip.startsWith("localhost")){
return true;
}
ipSection = ipSection.trim();
ip = ip.trim();
String REGX_IP = "((25[0-5]|2[0-4]\\d|1\\d{2}|[1-9]\\d|\\d)\\.){3}(25[0-5]|2[0-4]\\d|1\\d{2}|[1-9]\\d|\\d)";
String REGX_IPB = REGX_IP + "~" + REGX_IP;
if (!ipSection.matches(REGX_IPB) || !ip.matches(REGX_IP)) {
return false;
}
int idx = ipSection.indexOf('~');
String[] sips = ipSection.substring(0, idx).split("\\.");
String[] sipe = ipSection.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;
}