Skip to content

Commit 5d44c89

Browse files
Add getHostAddress() and getMaskBits() to IpAddressMatcher.java
Add meaningful toString() to IpAddressMatcher.java Closes gh-16693 Signed-off-by: Christopher Thumberger <[email protected]>
1 parent 4dd4813 commit 5d44c89

File tree

2 files changed

+50
-3
lines changed

2 files changed

+50
-3
lines changed

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,4 +130,25 @@ private InetAddress parseAddress(String address) {
130130
}
131131
}
132132

133+
/**
134+
* Returns the IP address this Matcher is matching for without mask bits.
135+
* @return a string representation of the IP address
136+
*/
137+
public String getHostAddress() {
138+
return this.requiredAddress.getHostAddress();
139+
}
140+
141+
/**
142+
* Returns the mask bits of the IP address this Matcher is matching for
143+
* @return a string representation of the mask bits
144+
*/
145+
public String getMaskBits() {
146+
return String.valueOf(this.nMaskBits);
147+
}
148+
149+
@Override
150+
public String toString() {
151+
return "IpAddressMatcher[" + getHostAddress() + "/" + getMaskBits() + "]";
152+
}
153+
133154
}

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

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@
2121

2222
import org.springframework.mock.web.MockHttpServletRequest;
2323

24-
import static org.assertj.core.api.Assertions.assertThat;
25-
import static org.assertj.core.api.Assertions.assertThatException;
26-
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
24+
import static org.assertj.core.api.Assertions.*;
2725

2826
/**
2927
* @author Luke Taylor
@@ -153,4 +151,32 @@ public void constructorWhenRequiredAddressIsEmptyThenThrowsIllegalArgumentExcept
153151
.withMessage("ipAddress cannot be empty");
154152
}
155153

154+
// gh-16693
155+
@Test
156+
public void getHostAddressReturnsCorrectIPv4String() {
157+
IpAddressMatcher matcher = new IpAddressMatcher("192.168.1.0/24");
158+
assertThat(matcher.getHostAddress()).isEqualTo("192.168.1.0");
159+
}
160+
161+
// gh-16693
162+
@Test
163+
public void getMaskBitsIp4ReturnsCorrectString() {
164+
IpAddressMatcher matcher = new IpAddressMatcher("192.168.1.0/24");
165+
assertThat(matcher.getMaskBits()).isEqualTo("24");
166+
}
167+
168+
// gh-16693
169+
@Test
170+
public void getHostAddressReturnsCorrectIPv6String() {
171+
IpAddressMatcher matcher = new IpAddressMatcher("fe80::21f:5bff:fe33:bd68");
172+
assertThat(matcher.getHostAddress()).isEqualTo("fe80:0:0:0:21f:5bff:fe33:bd68");
173+
}
174+
175+
// gh-16693
176+
@Test
177+
public void getMaskBitsIp6ReturnsCorrectString() {
178+
IpAddressMatcher matcher = new IpAddressMatcher("2001:DB8::/48");
179+
assertThat(matcher.getMaskBits()).isEqualTo("48");
180+
}
181+
156182
}

0 commit comments

Comments
 (0)