Skip to content

Commit bdbf6a2

Browse files
wtigerhyunsujzheaux
authored andcommitted
Add toString() to IpAddressMatcher.java
Closes gh-16795 Signed-off-by: wtigerhyunsu <[email protected]>
1 parent 40b84d3 commit bdbf6a2

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

web/src/main/java/org/springframework/security/web/util/matcher/IpAddressMatcher.java

+7
Original file line numberDiff line numberDiff line change
@@ -130,4 +130,11 @@ private InetAddress parseAddress(String address) {
130130
}
131131
}
132132

133+
@Override
134+
public String toString() {
135+
String hostAddress = this.requiredAddress.getHostAddress();
136+
return (this.nMaskBits < 0) ? "IpAddress [" + hostAddress + "]"
137+
: "IpAddress [" + hostAddress + "/" + this.nMaskBits + "]";
138+
}
139+
133140
}

web/src/test/java/org/springframework/security/web/util/matcher/IpAddressMatcherTests.java

+14
Original file line numberDiff line numberDiff line change
@@ -153,4 +153,18 @@ public void constructorWhenRequiredAddressIsEmptyThenThrowsIllegalArgumentExcept
153153
.withMessage("ipAddress cannot be empty");
154154
}
155155

156+
// gh-16795
157+
@Test
158+
public void toStringWhenCidrIsProvidedThenReturnsIpAddressWithCidr() {
159+
IpAddressMatcher matcher = new IpAddressMatcher("192.168.1.0/24");
160+
assertThat(matcher.toString()).hasToString("IpAddress [192.168.1.0/24]");
161+
}
162+
163+
// gh-16795
164+
@Test
165+
public void toStringWhenOnlyIpIsProvidedThenReturnsIpAddressOnly() {
166+
IpAddressMatcher matcher = new IpAddressMatcher("127.0.0.1");
167+
assertThat(matcher.toString()).hasToString("IpAddress [127.0.0.1]");
168+
}
169+
156170
}

0 commit comments

Comments
 (0)