工具類源碼 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;
	}
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章