보안상 특정IP만 허용 또는 제한할때 유용한 로직입니다.

와일드카드문자 또는 범위로 지정할수 있습니다.


/**
* check if IP address match pattern
*
* @param pattern
* *.*.*.* , 192.168.1.0-255 , *
* @param address
* - 192.168.1.1
* address = 10.2.88.12 pattern = *.*.*.* result: true
* address = 10.2.88.12 pattern = * result: true
* address = 10.2.88.12 pattern = 10.2.88.12-13 result: true
* address = 10.2.88.12 pattern = 10.2.88.13-125 result: false
* @return true if address match pattern
*/
public static boolean checkIPMatching(String pattern, String address)
{
if (pattern.equals("*.*.*.*") || pattern.equals("*"))
return true;

String[] mask = pattern.split("\\.");
String[] ip_address = address.split("\\.");
for (int i = 0; i < mask.length; i++)
{
if (mask[i].equals("*") || mask[i].equals(ip_address[i]))
continue;
else if (mask[i].contains("-"))
{
byte min = Byte.parseByte(mask[i].split("-")[0]);
byte max = Byte.parseByte(mask[i].split("-")[1]);
byte ip = Byte.parseByte(ip_address[i]);
if (ip < min || ip > max)
return false;
}
else
return false;
}
return true;
}


출처: http://www.java2s.com/Code/Java/Network-Protocol/CheckifIPaddressmatchpattern.htm

+ Recent posts