diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/NetUtils.java b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/NetUtils.java index c9ddff2a561..29095e88482 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/NetUtils.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/NetUtils.java @@ -738,7 +738,7 @@ public static boolean matchIpExpression(String pattern, String address) throws U String host = address; int port = 0; // only works for ipv4 address with 'ip:port' format - if (address.endsWith(":")) { + if (address.contains(":")) { String[] hostPort = address.split(":"); host = hostPort[0]; port = StringUtils.parseInteger(hostPort[1]); diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/utils/NetUtilsTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/utils/NetUtilsTest.java index 9f093229ab0..49a2305f7f6 100644 --- a/dubbo-common/src/test/java/org/apache/dubbo/common/utils/NetUtilsTest.java +++ b/dubbo-common/src/test/java/org/apache/dubbo/common/utils/NetUtilsTest.java @@ -346,6 +346,22 @@ void testMatchIpMatch() throws UnknownHostException { assertTrue(NetUtils.matchIpExpression("192.168.1.192/26", "192.168.1.199", 90)); } + @Test + void testMatchIpExpressionDeprecatedTwoArgsWithIpPort() throws UnknownHostException { + // The deprecated 2-arg matchIpExpression should correctly parse host and port from + // an "ip:port" address string. Normal addresses like "192.168.1.63:90" should + // split into host="192.168.1.63" and port=90. + // With contains(":") this works; with endsWith(":") it does not. + + // Pattern with port: host must match AND port must match + assertTrue(NetUtils.matchIpExpression("192.168.1.63:90", "192.168.1.63:90")); + assertFalse(NetUtils.matchIpExpression("192.168.1.63:90", "192.168.1.63:80")); + + // Pattern without port: only host must match + assertTrue(NetUtils.matchIpExpression("192.168.1.*", "192.168.1.63:90")); + assertTrue(NetUtils.matchIpExpression("192.168.1.192/26", "192.168.1.199:8080")); + } + @Test void testMatchIpv6WithIpPort() throws UnknownHostException { assertTrue(NetUtils.matchIpRange("[234e:0:4567::3d:ee]", "234e:0:4567::3d:ee", 8090));