svn白名单_Java IP白名单相关工具类

packagecom.tools.iptool;importjava.util.ArrayList;importjava.util.HashSet;importjava.util.List;importjava.util.Set;importjava.util.regex.Pattern;/*** @ClassName:IPWhiteList

* @Function: IP 白名单.

* @Reason:关于IP白名单相关.

* @Date: 2017-4-17 下午02:49:08

*@authorhello_史努比

*@version

*/

public classIPWhiteList {//IP的正则

private static Pattern pattern =Pattern

.compile("(1\\d{1,2}|2[0-4]\\d|25[0-5]|\\d{1,2})\\."

+ "(1\\d{1,2}|2[0-4]\\d|25[0-5]|\\d{1,2})\\."

+ "(1\\d{1,2}|2[0-4]\\d|25[0-5]|\\d{1,2})\\."

+ "(1\\d{1,2}|2[0-4]\\d|25[0-5]|\\d{1,2})");/***

* getAvaliIpList:(根据IP白名单设置获取可用的IP列表).

*

* @date 2017-4-17 下午02:50:20

*@paramipConfig

*@return

*/

private static SetgetAvaliIpList(String allowIp) {

Set ipList = new HashSet();for (String allow : allowIp.replaceAll("\\s", "").split(";")) {if (allow.indexOf("*") > -1) {

String[] ips= allow.split("\\.");

String[] from= new String[] { "0", "0", "0", "0"};

String[] end= new String[] { "255", "255", "255", "255"};

List tem = new ArrayList();for (int i = 0; i < ips.length; i++)if (ips[i].indexOf("*") > -1) {

tem=complete(ips[i]);

from[i]= null;

end[i]= null;

}else{

from[i]=ips[i];

end[i]=ips[i];

}

StringBuffer fromIP= newStringBuffer();

StringBuffer endIP= newStringBuffer();for (int i = 0; i < 4; i++)if (from[i] != null) {

fromIP.append(from[i]).append(".");

endIP.append(end[i]).append(".");

}else{

fromIP.append("[*].");

endIP.append("[*].");

}

fromIP.deleteCharAt(fromIP.length()- 1);

endIP.deleteCharAt(endIP.length()- 1);for(String s : tem) {

String ip= fromIP.toString().replace("[*]",

s.split(";")[0])+ "-"

+ endIP.toString().replace("[*]", s.split(";")[1]);if(validate(ip)) {

ipList.add(ip);

}

}

}else{if(validate(allow)) {

ipList.add(allow);

}

}

}returnipList;

}/*** 对单个IP节点进行范围限定

*

*@paramarg

*@return返回限定后的IP范围,格式为List[10;19, 100;199]*/

private static Listcomplete(String arg) {

List com = new ArrayList();if (arg.length() == 1) {

com.add("0;255");

}else if (arg.length() == 2) {

String s1= complete(arg, 1);if (s1 != null)

com.add(s1);

String s2= complete(arg, 2);if (s2 != null)

com.add(s2);

}else{

String s1= complete(arg, 1);if (s1 != null)

com.add(s1);

}returncom;

}private static String complete(String arg, intlength) {

String from= "";

String end= "";if (length == 1) {

from= arg.replace("*", "0");

end= arg.replace("*", "9");

}else{

from= arg.replace("*", "00");

end= arg.replace("*", "99");

}if (Integer.valueOf(from) > 255)return null;if (Integer.valueOf(end) > 255)

end= "255";return from + ";" +end;

}/*** 在添加至白名单时进行格式校验

*

*@paramip

*@return

*/

private static booleanvalidate(String ip) {for (String s : ip.split("-"))if (!pattern.matcher(s).matches()) {return false;

}return true;

}/***

* checkLoginIP:(根据IP,及可用Ip列表来判断ip是否包含在白名单之中).

* @date 2017-4-17 下午03:01:03

*@paramip

*@paramipList

*@return

*/

private static boolean checkLoginIP(String ip, SetipList) {if (ipList.isEmpty() ||ipList.contains(ip))return true;else{for(String allow : ipList) {if (allow.indexOf("-") > -1) {

String[] from= allow.split("-")[0].split("\\.");

String[] end= allow.split("-")[1].split("\\.");

String[] tag= ip.split("\\.");//对IP从左到右进行逐段匹配

boolean check = true;for (int i = 0; i < 4; i++) {int s =Integer.valueOf(from[i]);int t =Integer.valueOf(tag[i]);int e =Integer.valueOf(end[i]);if (!(s <= t && t <=e)) {

check= false;break;

}

}if(check) {return true;

}

}

}

}return false;

}/***

* checkLoginIP:(根据IP地址,及IP白名单设置规则判断IP是否包含在白名单).

* @date 2017-4-17 下午03:01:37

*@paramip

*@paramipWhiteConfig

*@return

*/

public static booleancheckLoginIP(String ip,String ipWhiteConfig){

Set ipList =getAvaliIpList(ipWhiteConfig);returncheckLoginIP(ip, ipList);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值