局域网IP和公网IP的范围校验

记录一次局域网IP和公网IP的范围校验.

还是可以继续优化的, 不过现在也能用了

判断IP是否在指定范围的方法需要搭配设置IP范围方法使用, 或者自己组合一下
/**
	 * 判断局域网网段是否符合规则
	 * 192.168.0.0~192.168.255.255 true
	 * 172.16.0.0~192.168.255.255 false
	 *
	 * @param ipSection 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;
		}
	}

	/**
	 * 判断公网网段是否符合规则
	 * @param ipSection ip~ip
	 */
	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;
	}


/**
	 * 判断IP是否在指定范围
	 * @param ipSection ip地址范围,~分隔
	 * @param ip        待校验的ip
	 */
	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;
	}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值