关于设置IP白名单相关的一些方法,整理,记录了一下。

package com.tools.iptool;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.regex.Pattern; /**
* @ClassName:IPWhiteList
* @Function: IP 白名单.
* @Reason:关于IP白名单相关.
* @Date: 2017-4-17 下午02:49:08
* @author hello_史努比
* @version
*/
public class IPWhiteList {
// 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
* @param ipConfig
* @return
*/ private static Set<String> getAvaliIpList(String allowIp) { Set<String> ipList = new HashSet<String>();
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<String> tem = new ArrayList<String>();
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 = new StringBuffer();
StringBuffer endIP = new StringBuffer();
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);
}
} } return ipList;
} /**
* 对单个IP节点进行范围限定
*
* @param arg
* @return 返回限定后的IP范围,格式为List[10;19, 100;199]
*/
private static List<String> complete(String arg) {
List<String> com = new ArrayList<String>();
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);
}
return com;
} private static String complete(String arg, int length) {
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;
} /**
* 在添加至白名单时进行格式校验
*
* @param ip
* @return
*/
private static boolean validate(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
* @param ip
* @param ipList
* @return
*/
private static boolean checkLoginIP(String ip, Set<String> ipList) {
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
* @param ip
* @param ipWhiteConfig
* @return
*/
public static boolean checkLoginIP(String ip,String ipWhiteConfig){
Set<String> ipList = getAvaliIpList(ipWhiteConfig);
return checkLoginIP(ip, ipList);
} }
package com.tools.iptool;

/**
*@ClassName:IPWhiteListTest
*@Function: 测试代码.
*@Reason: 测试代码.
*@Date: 2017-4-17 下午02:54:42
*@version
*/
public class IPWhiteListTest { /**
* main:().
* @date 2017-4-17 下午02:54:42
* @param args
*/
public static void main(String[] args) { String ipWhilte = "192.168.1.1;" + //设置单个IP的白名单
"192.168.2.*;" + //设置ip通配符,对一个ip段进行匹配
"192.168.3.17-192.168.3.38"; //设置一个IP范围
boolean flag = IPWhiteList.checkLoginIP("192.168.2.2",ipWhilte);
boolean flag2 = IPWhiteList.checkLoginIP("192.168.1.2",ipWhilte);
boolean flag3 = IPWhiteList.checkLoginIP("192.168.3.16",ipWhilte);
boolean flag4 = IPWhiteList.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
} }

最新文章

  1. Unity3d UGUI序列帧动画
  2. 早上3:30左右起来发现时候电脑在一致叫唤就是一个usb的接口可能是鼠标
  3. android:id=&quot;@id/resid&quot; , andorid:id=&quot;@+id/resid&quot; 的区别
  4. FZU 2124 bfs+vis记录
  5. sublime3快捷 输入html
  6. Create Script Template In Edit Mode
  7. Day01 - Python 基础介绍
  8. mac缺少预编译.a问题
  9. HDOJ-1009 FatMouse&#39; Trade
  10. jquery遍历筛选数组的几种方法和遍历解析json对象
  11. java 字符串全排列 和 去重
  12. python 自然语言处理(四)____词典资源
  13. 支持向量机通俗导论(理解SVM的三层境界)(ZT)
  14. ubuntu 解压命令
  15. 逆地址解析协议RARP
  16. 2016&quot;百度之星&quot; - 初赛(Astar Round2A) A.All X 矩阵快速幂
  17. 47.ActiveMQ集群
  18. Markdown基本语法总结
  19. javascript实现文本框标签验证
  20. 基于CSRF的XSS攻击

热门文章

  1. git新建分支没有master分支,其他分支也看不到
  2. bzoj2119 股市的预测
  3. JavaScript的进阶之路(三)引用类型之Object类型和Array类型
  4. URL传递中文:Server.UrlEncode与Server.UrlDecode
  5. Java 之 static的使用方法(6)
  6. centos aws 修改使用密码ssh登录
  7. linux中无法使用sudo的方法
  8. Appium的安装-Mac平台(命令行 &amp; dmg)
  9. Android(java)学习笔记211:Android线程池形态
  10. nrf52840蓝牙BLE5.0空中速率测试(nordic对nordic)