Ipv4Util 新增方法:检测指定 IP 地址是否匹配通配符

This commit is contained in:
Looly 2023-06-27 11:10:43 +08:00
parent 47daea1650
commit 050f312c56
3 changed files with 27 additions and 24 deletions

View File

@ -7,6 +7,7 @@
### 🐣新特性
* 【core 】 list 为空时CollUtil.max等返回null而非异常pr#1027@Gitee
* 【poi 】 ExcelReader.getWriter逻辑变更当从非文件读取时获取sheet而非空表格。
* 【core 】 Ipv4Util 新增方法:检测指定 IP 地址是否匹配通配符pr#3171@Github
### 🐞Bug修复
* 【core 】 修复MapUtil工具使用filter方法构造传入参数结果问题issue#3162@Github

View File

@ -6,6 +6,7 @@ import cn.hutool.core.lang.Assert;
import cn.hutool.core.lang.PatternPool;
import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.CharUtil;
import cn.hutool.core.util.ReUtil;
import cn.hutool.core.util.StrUtil;
import java.util.ArrayList;
@ -369,24 +370,25 @@ public class Ipv4Util {
/**
* 检测指定 IP 地址是否匹配通配符 wildcard
*
* @param pattern 通配符 192.168.*.1
* @param wildcard 通配符 192.168.*.1
* @param ipAddress 待检测的 IP 地址
* @return 是否匹配
*/
public static boolean matches(String pattern, String ipAddress) {
if (!Validator.isMatchRegex(PatternPool.IPV4, ipAddress)) {
public static boolean matches(String wildcard, String ipAddress) {
if (false == ReUtil.isMatch(PatternPool.IPV4, ipAddress)) {
return false;
}
String[] patternSegments = pattern.split("\\.");
String[] ipSegments = ipAddress.split("\\.");
final String[] wildcardSegments = wildcard.split("\\.");
final String[] ipSegments = ipAddress.split("\\.");
if (patternSegments.length != ipSegments.length) {
if (wildcardSegments.length != ipSegments.length) {
return false;
}
for (int i = 0; i < patternSegments.length; i++) {
if (!"*".equals(patternSegments[i]) && !patternSegments[i].equals(ipSegments[i])) {
for (int i = 0; i < wildcardSegments.length; i++) {
if (false == "*".equals(wildcardSegments[i])
&& false == wildcardSegments[i].equals(ipSegments[i])) {
return false;
}
}

View File

@ -16,7 +16,7 @@ public class Ipv4UtilTest {
@Test
public void getMaskBitByIllegalMaskTest() {
ThrowingRunnable getMaskBitByMaskRunnable = () -> Ipv4Util.getMaskBitByMask("255.255.0.255");
final ThrowingRunnable getMaskBitByMaskRunnable = () -> Ipv4Util.getMaskBitByMask("255.255.0.255");
Assert.assertThrows("非法掩码测试", IllegalArgumentException.class, getMaskBitByMaskRunnable);
}
@ -28,15 +28,15 @@ public class Ipv4UtilTest {
@Test
public void longToIpTest() {
String ip = "192.168.1.255";
final String ip = "192.168.1.255";
final long ipLong = Ipv4Util.ipv4ToLong(ip);
String ipv4 = Ipv4Util.longToIpv4(ipLong);
final String ipv4 = Ipv4Util.longToIpv4(ipLong);
Assert.assertEquals(ip, ipv4);
}
@Test
public void getEndIpStrTest(){
String ip = "192.168.1.1";
final String ip = "192.168.1.1";
final int maskBitByMask = Ipv4Util.getMaskBitByMask("255.255.255.0");
final String endIpStr = Ipv4Util.getEndIpStr(ip, maskBitByMask);
Assert.assertEquals("192.168.1.255", endIpStr);
@ -44,7 +44,7 @@ public class Ipv4UtilTest {
@Test
public void listTest(){
int maskBit = Ipv4Util.getMaskBitByMask("255.255.255.0");
final int maskBit = Ipv4Util.getMaskBitByMask("255.255.255.0");
final List<String> list = Ipv4Util.list("192.168.100.2", maskBit, false);
Assert.assertEquals(254, list.size());
@ -83,7 +83,7 @@ public class Ipv4UtilTest {
@Test
public void isMaskValidTest() {
boolean maskValid = Ipv4Util.isMaskValid("255.255.255.0");
final boolean maskValid = Ipv4Util.isMaskValid("255.255.255.0");
Assert.assertTrue("掩码合法检验", maskValid);
}
@ -97,22 +97,22 @@ public class Ipv4UtilTest {
@Test
public void isMaskBitValidTest() {
boolean maskBitValid = Ipv4Util.isMaskBitValid(32);
final boolean maskBitValid = Ipv4Util.isMaskBitValid(32);
Assert.assertTrue("掩码位合法检验", maskBitValid);
}
@Test
public void isMaskBitInvalidTest() {
boolean maskBitValid = Ipv4Util.isMaskBitValid(33);
final boolean maskBitValid = Ipv4Util.isMaskBitValid(33);
Assert.assertFalse("掩码位非法检验", maskBitValid);
}
@Test
public void matchesTest() {
boolean matches1 = Ipv4Util.matches("127.*.*.1", "127.0.0.1");
final boolean matches1 = Ipv4Util.matches("127.*.*.1", "127.0.0.1");
Assert.assertTrue("IP地址通配符匹配1", matches1);
boolean matches2 = Ipv4Util.matches("192.168.*.1", "127.0.0.1");
final boolean matches2 = Ipv4Util.matches("192.168.*.1", "127.0.0.1");
Assert.assertFalse("IP地址通配符匹配2", matches2);
}
@ -130,14 +130,14 @@ public class Ipv4UtilTest {
@Test
public void ipv4ToLongWithDefaultTest() {
String strIP = "不正确的 IP 地址";
long defaultValue = 0L;
long ipOfLong = Ipv4Util.ipv4ToLong(strIP, defaultValue);
final String strIP = "不正确的 IP 地址";
final long defaultValue = 0L;
final long ipOfLong = Ipv4Util.ipv4ToLong(strIP, defaultValue);
Assert.assertEquals(ipOfLong, defaultValue);
String strIP2 = "255.255.255.255";
long defaultValue2 = 0L;
long ipOfLong2 = Ipv4Util.ipv4ToLong(strIP2, defaultValue2);
final String strIP2 = "255.255.255.255";
final long defaultValue2 = 0L;
final long ipOfLong2 = Ipv4Util.ipv4ToLong(strIP2, defaultValue2);
Assert.assertEquals(ipOfLong2, 4294967295L);
}
}