400 028 6601

建站动态

根据您的个性需求进行定制 先人一步 抢占小程序红利时代

javaWeb项目加入IP黑白名单

第一步:先在项目src文件下建立一个ipConfig.properties文件,加入黑白名单的ip

公司主营业务:网站建设、做网站、移动网站开发等业务。帮助企业客户真正实现互联网宣传,提高企业的竞争能力。创新互联公司是一支青春激扬、勤奋敬业、活力青春激扬、勤奋敬业、活力澎湃、和谐高效的团队。公司秉承以“开放、自由、严谨、自律”为核心的企业文化,感谢他们对我们的高要求,感谢他们从不同领域给我们带来的挑战,让我们激情的团队有机会用头脑与智慧不断的给客户带来惊喜。创新互联公司推出河西免费做网站回馈大家。

1)ipConfig.properties:

#单个IP地址的配置,多个之间用逗号或分好隔开
allowIP=192.168.1.15;127.0.0.1;
#IP地址区间方式的配置,多个区间用逗号或分好隔开
allowIPRange=172.20.32.10-172.20.32.11;172.20.32.88-172.20.32.89;
#通配符,多个用逗号或分好隔开
allowIPWildcard=192.168.1.*;

二,建IpFilter,过滤器文件
/**

}
//打印输出allowList
for(String str : allowList) {
System.out.println(str);
}
}

/** * 对配置文件进行校验 
  * @author hht * 
 * @serialData 2018-09-28 *
 *  @param allowIP 
 *   @param allowIPRange 
 *   @param allowIPWildcard 
 * @return */ 
public Boolean validate(String allowIP, String allowIPRange, String allowIPWildcard) {
    Boolean result = false; 
    //IP地址每一段的正则 
    String regx = "(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)"; 
    //整个ip的正则
    String ipRegx = regx + "\\." + regx + "\\."+ regx + "\\." + regx; 
    //对第一种方式进行校验 
    Pattern pattern = Pattern.compile("("+ipRegx+")|("+ipRegx+"(,|;))*"); 
    if(this.isNullorMatches(allowIP, pattern)){ 
        result = true; //匹配成功
        } else { 
        result = false; 

}
//对第二种方式进行校验
pattern = Pattern.compile("("+ipRegx+")\-("+ipRegx+")|" + "(("+ipRegx+")\-("+ipRegx+")(,|;))");
if(this.isNullorMatches(allowIPRange, pattern)){
result = true; //匹配成功
} else {
result = false;
}
//对第三种方式进行校验
pattern = Pattern.compile("("+regx+"\."+ regx+"\."+regx+"\."+ "\)|" + "("+regx+"\."+regx+"\."+regx+"\."+ "\(,|;))
");
if(this.isNullorMatches(allowIPWildcard, pattern)){
result = true; //匹配成功
} else {
result = false;
}
return result;
}

/** * 进行正则匹配 * 
 * @author hht * 
 * @serialData 2018-09-28 *
 *  @param allow * @return */ 
public Boolean isNullorMatches(String allow, Pattern pattern) {
    //如果为空,说明用户没添加该项,不做处理
    if(allow == null || "".equals(allow.trim())) { return true; } else {
    //在最后面没有,或;的给添上 
    if(!allow.endsWith(";") && !allow.endsWith(",")) { 
    allow += ";";
    }
    //如果匹配,则返回true
  if(pattern.matcher(allow).matches()) { 
    return true; 
    } 
} 
return false; 

}

}

三,在项目中web.xml配置文件中加入过滤配置



IPFilter
xxxx.IpFilter


IPFilter
/*


网页题目:javaWeb项目加入IP黑白名单
URL网址:http://mbwzsj.com/article/gegcij.html

其他资讯

让你的专属顾问为你服务