Skip to content

Commit d693ad9

Browse files
rwinchvasporigKJ202rstoyanchev
committed
Add InetAddressMatcher
Co-authored-by: Gábor Vaspöri <gabor.vaspori@gmail.com> Co-authored-by: Kian Jamali <kianjamali123@gmail.com> Co-authored-by: Rossen Stoyanchev <rstoyanchev@users.noreply.github.com>
1 parent d4589c0 commit d693ad9

11 files changed

Lines changed: 1352 additions & 90 deletions

File tree

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
[[new]]
22
= What's New in Spring Security 7.1
33

4-
This is a placeholder for updates to Spring Security 7.1
4+
* https://github.com/spring-projects/spring-security/pull/18634[gh-18634] - Added javadoc:org.springframework.security.web.util.matcher.InetAddressMatcher[]

web/src/main/java/org/springframework/security/web/server/util/matcher/IpAddressServerWebExchangeMatcher.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,12 @@
1616

1717
package org.springframework.security.web.server.util.matcher;
1818

19+
import java.util.List;
20+
1921
import reactor.core.publisher.Mono;
2022

21-
import org.springframework.security.web.util.matcher.IpAddressMatcher;
23+
import org.springframework.security.web.util.matcher.InetAddressMatcher;
24+
import org.springframework.security.web.util.matcher.InetAddressMatchers;
2225
import org.springframework.util.Assert;
2326
import org.springframework.web.server.ServerWebExchange;
2427

@@ -31,7 +34,7 @@
3134
*/
3235
public final class IpAddressServerWebExchangeMatcher implements ServerWebExchangeMatcher {
3336

34-
private final IpAddressMatcher ipAddressMatcher;
37+
private final InetAddressMatcher ipAddressMatcher;
3538

3639
/**
3740
* Takes a specific IP address or a range specified using the IP/Netmask (e.g.
@@ -41,7 +44,7 @@ public final class IpAddressServerWebExchangeMatcher implements ServerWebExchang
4144
*/
4245
public IpAddressServerWebExchangeMatcher(String ipAddress) {
4346
Assert.hasText(ipAddress, "IP address cannot be empty");
44-
this.ipAddressMatcher = new IpAddressMatcher(ipAddress);
47+
this.ipAddressMatcher = InetAddressMatchers.builder().includeAddresses(List.of(ipAddress)).build();
4548
}
4649

4750
@Override
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright 2004-present the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.security.web.util.matcher;
18+
19+
import java.net.InetAddress;
20+
21+
import org.jspecify.annotations.Nullable;
22+
23+
/**
24+
* Matches an {@link InetAddress}.
25+
*
26+
* @author Rossen Stoyanchev
27+
* @author Rob Winch
28+
* @since 7.1
29+
*/
30+
@FunctionalInterface
31+
public interface InetAddressMatcher {
32+
33+
/**
34+
* Whether the given address matches.
35+
* @param address the {@link InetAddress} to check (may be {@code null})
36+
* @return {@code true} if the address matches, {@code false} otherwise
37+
*/
38+
boolean matches(@Nullable InetAddress address);
39+
40+
/**
41+
* Whether the given address string matches.
42+
* @param address the IP address string to check (may be {@code null})
43+
* @return {@code true} if the address matches, {@code false} otherwise
44+
*/
45+
default boolean matches(@Nullable String address) {
46+
return (address != null) ? matches(InetAddressParser.parseAddress(address)) : false;
47+
}
48+
49+
}

0 commit comments

Comments
 (0)