日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

java ip 白名单_Java代码中对IP进行白名单验证

發(fā)布時間:2023/12/3 java 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java ip 白名单_Java代码中对IP进行白名单验证 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

public classipUtil {//IP的正則,這個正則不能驗證第一組數(shù)字為0的情況//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})");

private static Pattern pattern =Pattern

.compile("([1-9]\\d?|1\\d{2}|2[01]\\d|22[0-3])\\."

+ "(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:(根據(jù)IP白名單設(shè)置獲取可用的IP列表).

*

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

*@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) {//todo 直接用等于,不能正確獲取類似192.168.**.*這種格式的ip段

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節(jié)點進行范圍限定

*

*@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:(根據(jù)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:(根據(jù)IP地址,及IP白名單設(shè)置規(guī)則判斷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);

}public static voidmain(String[] args) {

String ipWhilte= "192.168.1.1," + //設(shè)置單個IP的白名單

"192.168.*.2," + //設(shè)置ip通配符,對一個ip段進行匹配

"192.168.3.17-192.168.3.38"; //設(shè)置一個IP范圍

boolean flag = checkLoginIP("192.168.2.2",ipWhilte);boolean flag2 = checkLoginIP("192.168.1.2",ipWhilte);boolean flag3 = checkLoginIP("192.168.3.16",ipWhilte);boolean flag4 = checkLoginIP("192.168.3.17",ipWhilte);

System.out.println(flag);//true

System.out.println(flag2); //false

System.out.println(flag3); //false

System.out.println(flag4); //true

}

}

總結(jié)

以上是生活随笔為你收集整理的java ip 白名单_Java代码中对IP进行白名单验证的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。