init
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
package com.eactive.eai.util;
|
||||
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class IpUtil {
|
||||
/**
|
||||
* matchIps에 ip 목록을 컴마(,)구분자로 나열 하고 requestIp와 맞는지 비교
|
||||
*
|
||||
* @param matchIps 컴마구분자 matchIp 목록 예제) 192.168.0.1,192.168.0.0/24,192.168.1.*
|
||||
* @param requestIp 원격IP
|
||||
* @return
|
||||
*/
|
||||
public static boolean isMatchIp(String matchIps, String requestIp) {
|
||||
String[] matchIpArr = org.springframework.util.StringUtils.tokenizeToStringArray(matchIps, ",");
|
||||
for (String match : matchIpArr) {
|
||||
if (match.indexOf('*') >= 0) {
|
||||
String matchRegexp = match.replace("\\*", "[0-9]+").trim();
|
||||
Matcher matcher = Pattern.compile(matchRegexp, Pattern.CASE_INSENSITIVE).matcher(requestIp);
|
||||
if (matcher.matches()) {
|
||||
return true;
|
||||
}
|
||||
} else if (match.indexOf('/') >= 0) { // CIDR 표기법 "192.168.0.0/24"
|
||||
org.apache.commons.net.util.SubnetUtils utils = new org.apache.commons.net.util.SubnetUtils(match);
|
||||
if (utils.getInfo().isInRange(requestIp)) {
|
||||
return true;
|
||||
}
|
||||
} else {
|
||||
if (match.equals(requestIp)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
String matchdIps = "192.168.0.1,192.168.0.0/24,192.168.1.*";
|
||||
String requestIp = "192.168.1.2";
|
||||
if (IpUtil.isMatchIp(matchdIps, requestIp)) {
|
||||
System.out.println(true);
|
||||
} else {
|
||||
System.out.println(false);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user