如何验证是否在白名单中的IP

时间:2014-04-11 14:13:42

标签: java ip wildcard

我有一个IP白名单,它是一个字符串数组,如{“192.168.1.1”,“172.8.9.9”,“198.2.3。”,“89.90.9.9-78”},从中看到数组我们知道它包含带有通配符“”的ip,以及89.90.9.9到89.90.9.78之间的ip,表示为“89.90.9.9-78”。

问题是如何判断此数组中的ip是否可以使用正则表达式?

我正在使用java。

代码段 我写这样的代码`

String[] ipWhiteList = {"192.168.1.1" ,"172.8.9.9", "198.2.3.", "89.90.9.9-78"};

    List<String> ipWithoutWildcard = new ArrayList<String>();
    List<String> ipWithWildcard = new ArrayList<String>();
    private void analyzeIPWhiteList(String[] ipWhiteList){
        for (String ip : ipWhiteList) {if(ip.endsWith("*")){
 ipWithWildcard.add(ip.substring(0,ip.lastIndexOf(".")));
                break;
            }
            if(ip.contains("-")){
                String partionOfIP = ip.substring(0,ip.lastIndexOf("-"));
                int ipBegin = Integer.parseInt(ip.substring(ip.lastIndexOf(".")+1,ip.lastIndexOf("-")));
                int ipEnd = Integer.parseInt(ip.substring(ip.lastIndexOf("-")+1));
                for (int index=ipBegin; index<=ipEnd; index++) {
                    ipWithoutWildcard.add(partionOfIP+"."+ index);
                }
                break;
            }
            ipWithoutWildcard.add(ip);
        }
    }

    private boolean isExist(String ip,List<String> ipWithoutWildcard,List<String> ipWithWildcard){
        //compare the ip in  ipWithoutWildcard and ipWithWildcard
        return false;
    }

`

0 个答案:

没有答案