源IP可能为 172.* 172.1.* 172.1.2.* 或者 172.*.2.* 之类的
目标IP 为 172.1.1.1
思路就是 根据 点 来判断 是哪一种带*的源IP 然后进行判断
对应172.*.1.2 这种的需要特殊处理 方法如下 将IP以点切割后 根据不是*的座标 将源IP和目标IP拼接新的IP进行比较
public static Boolean checkIp(String targetIP,String sourceIP){ String[] targetIPArry = targetIP.split("\\."); String[] sourceIPArry = sourceIP.split("\\."); String newSourceIP = ""; List<Integer> indexArry = new ArrayList(); for(int i = 0;i<sourceIPArry.length;i++){ if(!"*".equals(sourceIPArry[i])){ newSourceIP += sourceIPArry[i]; indexArry.add(i); } } String newTargetIP = ""; for(int j = 0;j<indexArry.size();j++){ newTargetIP += targetIPArry[indexArry.get(j)]; } if(newSourceIP.equals(newTargetIP)){ return true; } return false; }
整体如下
String[] str = whiteIpList.split(";"); List<String> list = Arrays.asList(str); //新增逻辑 支持* 如:172.* 172.1.* *.1.2.3/172.*.2.3/172.1.*.3/172.1.2.* boolean flag = false; for(int i = 0;i<list.size();i++){ String sourceIP = list.get(i).toString(); if(sourceIP.split("\\.").length < 4&&requestIp.startsWith(sourceIP.substring(0,sourceIP.indexOf(".")))){ //说明172.* 172.1.* flag = true; break; }else{ if(sourceIP.indexOf("*")>-1){ if(checkIp(requestIp,sourceIP)){ flag = true; break; } }else if(sourceIP.equals(requestIp)){ flag = true; break; } } }