工具类源码 IP辅助类 验证IP地址或地址段是否有效 验证指定的IP是否有效 即验证IP是否属于某个IP段

import org.apache.commons.lang.StringUtils;

/**
 * IP辅助类
 */
public class IpExUtil {

	/**
	 * 验证指定的IP是否有效
	 * 
	 * 即验证IP是否属于某个IP段或地址段
	 * 
	 * @case1:172.17.124.1-172.17.128.200
	 * @case2:172.*.125.*
	 * @value:172.17.125.98
	 * 
	 * @param ipSection
	 *            可能有多行,多行用\r\n隔开 1)IP号码段(如:172.17.124.1-172.17.128.200)
	 *            2)模糊匹配的IP(如:172.*.125.*)
	 * @return 是否有效
	 */
	public static boolean checkIPMutils(String ipSectionWithMutil,
			String currentIp) {

		boolean isPass = false;
		String splitStr = "\r\n";
		if (ipSectionWithMutil.indexOf(splitStr) < 0) {
			splitStr = "\n";
		}
		String[] ipSections = ipSectionWithMutil.split(splitStr);
		for (String ipSection : ipSections) {
			isPass = checkIPs(ipSection, currentIp);
			if (isPass) {
				break;
			}
		}

		return isPass;
	}

	/**
	 * 验证输入的IP地址或地址段是否有效
	 * 
	 * @case1:172.17.124.1-172.17.128.200
	 * @case2:172.*.125.*
	 * @value:172.17.125.98
	 * 
	 * @param ipSectionWhtiMutil
	 *            可能有多行,多行用\r\n隔开 1)IP号码段(如:172.17.124.1-172.17.128.200)
	 *            2)模糊匹配的IP(如:172.*.125.*)
	 * @return 是否有效
	 */
	public static boolean checkInputIPMutils(String ipSectionWithMutil) {

		boolean isPass = false;
		String splitStr = "\r\n";
		if (ipSectionWithMutil.indexOf(splitStr) < 0) {
			splitStr = "\n";
		}
		String[] ipSections = ipSectionWithMutil.split(splitStr);
		for (String ipSection : ipSections) {
			isPass = checkInputIPs(ipSection);
			if (!isPass) {
				break;
			}
		}

		return isPass;
	}

	/**
	 * 验证IP地址或地址段是否有效 验证指定的IP是否有效 即验证IP是否属于某个IP段
	 * 
	 * @case1:172.17.124.1-172.17.128.200
	 * @case2:172.*.125.*
	 * @value:172.17.125.98
	 * 
	 * @param ipSection
	 *            1)IP号码段(如:172.17.124.1-172.17.128.200)
	 *            2)模糊匹配的IP(如:172.*.125.*)
	 * @return 是否有效
	 */
	private static boolean checkIPs(String ipSection, String currentIp) {

		if (StringUtils.isBlank(ipSection) || StringUtils.isBlank(currentIp)) {
			return false;
		}

		ipSection = ipSection.trim();
		currentIp = currentIp.trim();

		String pointChr = "\\.";
		char henggChr = '-';
		String starChr = "*";

		boolean isPass = false;

		if (ipSection.indexOf(henggChr) > -1) {
			isPass = ipIsInRange(ipSection, currentIp, henggChr, pointChr);
		} else {
			isPass = ipVagueMatch(ipSection, currentIp, pointChr, starChr);
		}

		return isPass;
	}

	/**
	 * 验证IP地址或地址段是否有效
	 * 
	 * @case1:172.17.124.1-172.17.128.200
	 * @case2:172.*.125.*
	 * @value:172.17.125.98
	 * 
	 * @param ipSection
	 *            1)IP号码段(如:172.17.124.1-172.17.128.200)
	 *            2)模糊匹配的IP(如:172.*.125.*)
	 * @return 是否有效
	 */
	private static boolean checkInputIPs(String ipSection) {

		if (StringUtils.isBlank(ipSection)) {
			return false;
		}

		ipSection = ipSection.trim();

		char henggChr = '-';

		boolean isPass = false;

		if (ipSection.indexOf(henggChr) > -1) {
			isPass = checkIpSection(ipSection);
		} else {
			isPass = checkIpVagueMatch(ipSection);
		}

		return isPass;
	}

	/**
	 * 验证IP号码段是否有效
	 * 
	 * @param ipSection
	 *            IP号码段 (如:10.10.192.125-172.17.253.100)
	 * @return 是否有效
	 */
	private static boolean checkIpSection(String ipSection) {

		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;

		boolean isPass = ipSection.matches(REGX_IPB);
		return isPass;
	}

	/**
	 * 验证IP模糊匹配
	 * 
	 * @param ipSection
	 *            模糊匹配的IP(如:172.*.125.*)
	 * @return 是否有效
	 */
	private static boolean checkIpVagueMatch(String ipSection) {

		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|\\*)";

		boolean isPass = ipSection.matches(REGX_IP);
		return isPass;
	}

	/**
	 * 判断IP是否在指定IP段内
	 * 
	 * @param ipSection
	 *            IP号码段(以'-'分隔) (如:10.10.192.125-172.17.253.100)
	 * @param currentIp
	 *            当前IP地址
	 * @param henggChr
	 *            横杠字符
	 * @param pointChr
	 *            点字符串
	 * @return boolean 是否有效
	 */
	private static boolean ipIsInRange(String ipSection, String currentIp,
			char henggChr, String pointChr) {

		if (!checkIpSection(ipSection)) {
			return false;
		}

		int idx = ipSection.indexOf(henggChr);
		String[] sips = ipSection.substring(0, idx).split(pointChr);
		String[] sipe = ipSection.substring(idx + 1).split(pointChr);
		String[] sipt = currentIp.split(pointChr);
		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 ipSection
	 *            模糊匹配的IP(如:172.*.125.*)
	 * @param currentIp
	 *            当前IP地址
	 * @param pointChr
	 *            点字符串
	 * @return boolean 是否有效
	 */
	private static boolean ipVagueMatch(String ipSection, String currentIp,
			String pointChr, String starChr) {

		if (ipSection.equals(currentIp)) {
			return true;
		}

		boolean isPass = false;
		String[] sips = ipSection.split(pointChr);
		String[] sipt = currentIp.split(pointChr);
		int count = 0;
		for (int i = 0; i < 4; ++i) {
			if (sips[i].equals(starChr)) {
				count++;
				continue;
			}
			if (sips[i].equals(sipt[i])) {
				count++;
				continue;
			}
		}
		if (count >= 4) {
			isPass = true;
		}

		return isPass;
	}
}

 

你可能感兴趣的:(开发经验,解决方案)