1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.bremersee.security;
18
19 import java.net.InetAddress;
20 import java.net.InetSocketAddress;
21 import java.util.Optional;
22 import org.springframework.http.server.reactive.ServerHttpRequest;
23 import org.springframework.security.web.server.util.matcher.ServerWebExchangeMatcher;
24 import org.springframework.web.server.ServerWebExchange;
25 import reactor.core.publisher.Mono;
26
27
28
29
30
31
32 public class ReactiveIpAddressMatcher extends IpAddressMatcher implements ServerWebExchangeMatcher {
33
34
35
36
37
38
39 public ReactiveIpAddressMatcher(String ipAddress) {
40 super(ipAddress);
41 }
42
43
44
45
46
47
48
49 public boolean matchesRemoteAddress(ServerWebExchange exchange) {
50 return Optional.ofNullable(exchange)
51 .map(ServerWebExchange::getRequest)
52 .map(ServerHttpRequest::getRemoteAddress)
53 .map(InetSocketAddress::getAddress)
54 .map(InetAddress::getHostAddress)
55 .map(this::matches)
56 .orElse(false);
57 }
58
59 @Override
60 public Mono<MatchResult> matches(ServerWebExchange exchange) {
61 return matchesRemoteAddress(exchange) ? MatchResult.match() : MatchResult.notMatch();
62 }
63
64 }