mirror of
https://gitee.com/dromara/hutool.git
synced 2025-04-05 17:37:59 +08:00
Ipv4Util 新增方法:检测指定 IP 地址是否匹配通配符
This commit is contained in:
parent
47daea1650
commit
050f312c56
@ -7,6 +7,7 @@
|
|||||||
### 🐣新特性
|
### 🐣新特性
|
||||||
* 【core 】 list 为空时,CollUtil.max等返回null而非异常(pr#1027@Gitee)
|
* 【core 】 list 为空时,CollUtil.max等返回null而非异常(pr#1027@Gitee)
|
||||||
* 【poi 】 ExcelReader.getWriter逻辑变更,当从非文件读取时,获取sheet,而非空表格。
|
* 【poi 】 ExcelReader.getWriter逻辑变更,当从非文件读取时,获取sheet,而非空表格。
|
||||||
|
* 【core 】 Ipv4Util 新增方法:检测指定 IP 地址是否匹配通配符(pr#3171@Github)
|
||||||
|
|
||||||
### 🐞Bug修复
|
### 🐞Bug修复
|
||||||
* 【core 】 修复MapUtil工具使用filter方法构造传入参数结果问题(issue#3162@Github)
|
* 【core 】 修复MapUtil工具使用filter方法构造传入参数结果问题(issue#3162@Github)
|
||||||
|
@ -6,6 +6,7 @@ import cn.hutool.core.lang.Assert;
|
|||||||
import cn.hutool.core.lang.PatternPool;
|
import cn.hutool.core.lang.PatternPool;
|
||||||
import cn.hutool.core.lang.Validator;
|
import cn.hutool.core.lang.Validator;
|
||||||
import cn.hutool.core.util.CharUtil;
|
import cn.hutool.core.util.CharUtil;
|
||||||
|
import cn.hutool.core.util.ReUtil;
|
||||||
import cn.hutool.core.util.StrUtil;
|
import cn.hutool.core.util.StrUtil;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
@ -369,24 +370,25 @@ public class Ipv4Util {
|
|||||||
/**
|
/**
|
||||||
* 检测指定 IP 地址是否匹配通配符 wildcard
|
* 检测指定 IP 地址是否匹配通配符 wildcard
|
||||||
*
|
*
|
||||||
* @param pattern 通配符,如 192.168.*.1
|
* @param wildcard 通配符,如 192.168.*.1
|
||||||
* @param ipAddress 待检测的 IP 地址
|
* @param ipAddress 待检测的 IP 地址
|
||||||
* @return 是否匹配
|
* @return 是否匹配
|
||||||
*/
|
*/
|
||||||
public static boolean matches(String pattern, String ipAddress) {
|
public static boolean matches(String wildcard, String ipAddress) {
|
||||||
if (!Validator.isMatchRegex(PatternPool.IPV4, ipAddress)) {
|
if (false == ReUtil.isMatch(PatternPool.IPV4, ipAddress)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
String[] patternSegments = pattern.split("\\.");
|
final String[] wildcardSegments = wildcard.split("\\.");
|
||||||
String[] ipSegments = ipAddress.split("\\.");
|
final String[] ipSegments = ipAddress.split("\\.");
|
||||||
|
|
||||||
if (patternSegments.length != ipSegments.length) {
|
if (wildcardSegments.length != ipSegments.length) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int i = 0; i < patternSegments.length; i++) {
|
for (int i = 0; i < wildcardSegments.length; i++) {
|
||||||
if (!"*".equals(patternSegments[i]) && !patternSegments[i].equals(ipSegments[i])) {
|
if (false == "*".equals(wildcardSegments[i])
|
||||||
|
&& false == wildcardSegments[i].equals(ipSegments[i])) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -16,7 +16,7 @@ public class Ipv4UtilTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void getMaskBitByIllegalMaskTest() {
|
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);
|
Assert.assertThrows("非法掩码测试", IllegalArgumentException.class, getMaskBitByMaskRunnable);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -28,15 +28,15 @@ public class Ipv4UtilTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void longToIpTest() {
|
public void longToIpTest() {
|
||||||
String ip = "192.168.1.255";
|
final String ip = "192.168.1.255";
|
||||||
final long ipLong = Ipv4Util.ipv4ToLong(ip);
|
final long ipLong = Ipv4Util.ipv4ToLong(ip);
|
||||||
String ipv4 = Ipv4Util.longToIpv4(ipLong);
|
final String ipv4 = Ipv4Util.longToIpv4(ipLong);
|
||||||
Assert.assertEquals(ip, ipv4);
|
Assert.assertEquals(ip, ipv4);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void getEndIpStrTest(){
|
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 int maskBitByMask = Ipv4Util.getMaskBitByMask("255.255.255.0");
|
||||||
final String endIpStr = Ipv4Util.getEndIpStr(ip, maskBitByMask);
|
final String endIpStr = Ipv4Util.getEndIpStr(ip, maskBitByMask);
|
||||||
Assert.assertEquals("192.168.1.255", endIpStr);
|
Assert.assertEquals("192.168.1.255", endIpStr);
|
||||||
@ -44,7 +44,7 @@ public class Ipv4UtilTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void listTest(){
|
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);
|
final List<String> list = Ipv4Util.list("192.168.100.2", maskBit, false);
|
||||||
Assert.assertEquals(254, list.size());
|
Assert.assertEquals(254, list.size());
|
||||||
|
|
||||||
@ -83,7 +83,7 @@ public class Ipv4UtilTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void isMaskValidTest() {
|
public void isMaskValidTest() {
|
||||||
boolean maskValid = Ipv4Util.isMaskValid("255.255.255.0");
|
final boolean maskValid = Ipv4Util.isMaskValid("255.255.255.0");
|
||||||
Assert.assertTrue("掩码合法检验", maskValid);
|
Assert.assertTrue("掩码合法检验", maskValid);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -97,22 +97,22 @@ public class Ipv4UtilTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void isMaskBitValidTest() {
|
public void isMaskBitValidTest() {
|
||||||
boolean maskBitValid = Ipv4Util.isMaskBitValid(32);
|
final boolean maskBitValid = Ipv4Util.isMaskBitValid(32);
|
||||||
Assert.assertTrue("掩码位合法检验", maskBitValid);
|
Assert.assertTrue("掩码位合法检验", maskBitValid);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void isMaskBitInvalidTest() {
|
public void isMaskBitInvalidTest() {
|
||||||
boolean maskBitValid = Ipv4Util.isMaskBitValid(33);
|
final boolean maskBitValid = Ipv4Util.isMaskBitValid(33);
|
||||||
Assert.assertFalse("掩码位非法检验", maskBitValid);
|
Assert.assertFalse("掩码位非法检验", maskBitValid);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void matchesTest() {
|
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);
|
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);
|
Assert.assertFalse("IP地址通配符匹配2", matches2);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -130,14 +130,14 @@ public class Ipv4UtilTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void ipv4ToLongWithDefaultTest() {
|
public void ipv4ToLongWithDefaultTest() {
|
||||||
String strIP = "不正确的 IP 地址";
|
final String strIP = "不正确的 IP 地址";
|
||||||
long defaultValue = 0L;
|
final long defaultValue = 0L;
|
||||||
long ipOfLong = Ipv4Util.ipv4ToLong(strIP, defaultValue);
|
final long ipOfLong = Ipv4Util.ipv4ToLong(strIP, defaultValue);
|
||||||
Assert.assertEquals(ipOfLong, defaultValue);
|
Assert.assertEquals(ipOfLong, defaultValue);
|
||||||
|
|
||||||
String strIP2 = "255.255.255.255";
|
final String strIP2 = "255.255.255.255";
|
||||||
long defaultValue2 = 0L;
|
final long defaultValue2 = 0L;
|
||||||
long ipOfLong2 = Ipv4Util.ipv4ToLong(strIP2, defaultValue2);
|
final long ipOfLong2 = Ipv4Util.ipv4ToLong(strIP2, defaultValue2);
|
||||||
Assert.assertEquals(ipOfLong2, 4294967295L);
|
Assert.assertEquals(ipOfLong2, 4294967295L);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user