|
| 1 | +/* |
| 2 | + * Copyright 2002-2022 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.server.authentication; |
| 18 | + |
| 19 | +import java.util.ArrayList; |
| 20 | +import java.util.Arrays; |
| 21 | +import java.util.List; |
| 22 | + |
| 23 | +import reactor.core.publisher.Flux; |
| 24 | +import reactor.core.publisher.Mono; |
| 25 | + |
| 26 | +import org.springframework.security.authentication.AuthenticationServiceException; |
| 27 | +import org.springframework.security.authentication.ReactiveAuthenticationManager; |
| 28 | +import org.springframework.security.authentication.ReactiveAuthenticationManagerResolver; |
| 29 | +import org.springframework.security.web.access.intercept.RequestMatcherDelegatingAuthorizationManager; |
| 30 | +import org.springframework.security.web.authentication.RequestMatcherDelegatingAuthenticationManagerResolver; |
| 31 | +import org.springframework.security.web.server.util.matcher.ServerWebExchangeMatcher; |
| 32 | +import org.springframework.security.web.server.util.matcher.ServerWebExchangeMatcherEntry; |
| 33 | +import org.springframework.util.Assert; |
| 34 | +import org.springframework.web.server.ServerWebExchange; |
| 35 | + |
| 36 | +/** |
| 37 | + * A {@link ReactiveAuthenticationManagerResolver} that returns a |
| 38 | + * {@link ReactiveAuthenticationManager} instances based upon the type of |
| 39 | + * {@link ServerWebExchange} passed into {@link #resolve(ServerWebExchange)}. |
| 40 | + * |
| 41 | + * @author Josh Cummings |
| 42 | + * @since 5.7 |
| 43 | + * |
| 44 | + */ |
| 45 | +public final class ServerWebExchangeDelegatingReactiveAuthenticationManagerResolver |
| 46 | + implements ReactiveAuthenticationManagerResolver<ServerWebExchange> { |
| 47 | + |
| 48 | + private final List<ServerWebExchangeMatcherEntry<ReactiveAuthenticationManager>> authenticationManagers; |
| 49 | + |
| 50 | + private ReactiveAuthenticationManager defaultAuthenticationManager = (authentication) -> Mono |
| 51 | + .error(new AuthenticationServiceException("Cannot authenticate " + authentication)); |
| 52 | + |
| 53 | + /** |
| 54 | + * Construct an |
| 55 | + * {@link ServerWebExchangeDelegatingReactiveAuthenticationManagerResolver} based on |
| 56 | + * the provided parameters |
| 57 | + * @param managers a set of {@link ServerWebExchangeMatcherEntry}s |
| 58 | + */ |
| 59 | + ServerWebExchangeDelegatingReactiveAuthenticationManagerResolver( |
| 60 | + ServerWebExchangeMatcherEntry<ReactiveAuthenticationManager>... managers) { |
| 61 | + this(Arrays.asList(managers)); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Construct an |
| 66 | + * {@link ServerWebExchangeDelegatingReactiveAuthenticationManagerResolver} based on |
| 67 | + * the provided parameters |
| 68 | + * @param managers a {@link List} of {@link ServerWebExchangeMatcherEntry}s |
| 69 | + */ |
| 70 | + ServerWebExchangeDelegatingReactiveAuthenticationManagerResolver( |
| 71 | + List<ServerWebExchangeMatcherEntry<ReactiveAuthenticationManager>> managers) { |
| 72 | + Assert.notNull(managers, "entries cannot be null"); |
| 73 | + this.authenticationManagers = managers; |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * {@inheritDoc} |
| 78 | + */ |
| 79 | + @Override |
| 80 | + public Mono<ReactiveAuthenticationManager> resolve(ServerWebExchange exchange) { |
| 81 | + return Flux.fromIterable(this.authenticationManagers).filterWhen((entry) -> isMatch(exchange, entry)).next() |
| 82 | + .map(ServerWebExchangeMatcherEntry::getEntry).defaultIfEmpty(this.defaultAuthenticationManager); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Set the default {@link ReactiveAuthenticationManager} to use when a request does |
| 87 | + * not match |
| 88 | + * @param defaultAuthenticationManager the default |
| 89 | + * {@link ReactiveAuthenticationManager} to use |
| 90 | + */ |
| 91 | + public void setDefaultAuthenticationManager(ReactiveAuthenticationManager defaultAuthenticationManager) { |
| 92 | + Assert.notNull(defaultAuthenticationManager, "defaultAuthenticationManager cannot be null"); |
| 93 | + this.defaultAuthenticationManager = defaultAuthenticationManager; |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Creates a builder for {@link RequestMatcherDelegatingAuthorizationManager}. |
| 98 | + * @return the new {@link RequestMatcherDelegatingAuthorizationManager.Builder} |
| 99 | + * instance |
| 100 | + */ |
| 101 | + public static Builder builder() { |
| 102 | + return new Builder(); |
| 103 | + } |
| 104 | + |
| 105 | + private Mono<Boolean> isMatch(ServerWebExchange exchange, |
| 106 | + ServerWebExchangeMatcherEntry<ReactiveAuthenticationManager> entry) { |
| 107 | + ServerWebExchangeMatcher matcher = entry.getMatcher(); |
| 108 | + return matcher.matches(exchange).map(ServerWebExchangeMatcher.MatchResult::isMatch); |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * A builder for {@link RequestMatcherDelegatingAuthenticationManagerResolver}. |
| 113 | + */ |
| 114 | + public static final class Builder { |
| 115 | + |
| 116 | + private final List<ServerWebExchangeMatcherEntry<ReactiveAuthenticationManager>> entries = new ArrayList<>(); |
| 117 | + |
| 118 | + private Builder() { |
| 119 | + |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * Maps a {@link ServerWebExchangeMatcher} to an |
| 124 | + * {@link ReactiveAuthenticationManager}. |
| 125 | + * @param matcher the {@link ServerWebExchangeMatcher} to use |
| 126 | + * @param manager the {@link ReactiveAuthenticationManager} to use |
| 127 | + * @return the |
| 128 | + * {@link RequestMatcherDelegatingAuthenticationManagerResolver.Builder} for |
| 129 | + * further customizations |
| 130 | + */ |
| 131 | + public ServerWebExchangeDelegatingReactiveAuthenticationManagerResolver.Builder add( |
| 132 | + ServerWebExchangeMatcher matcher, ReactiveAuthenticationManager manager) { |
| 133 | + Assert.notNull(matcher, "matcher cannot be null"); |
| 134 | + Assert.notNull(manager, "manager cannot be null"); |
| 135 | + this.entries.add(new ServerWebExchangeMatcherEntry<>(matcher, manager)); |
| 136 | + return this; |
| 137 | + } |
| 138 | + |
| 139 | + /** |
| 140 | + * Creates a {@link RequestMatcherDelegatingAuthenticationManagerResolver} |
| 141 | + * instance. |
| 142 | + * @return the {@link RequestMatcherDelegatingAuthenticationManagerResolver} |
| 143 | + * instance |
| 144 | + */ |
| 145 | + public ServerWebExchangeDelegatingReactiveAuthenticationManagerResolver build() { |
| 146 | + return new ServerWebExchangeDelegatingReactiveAuthenticationManagerResolver(this.entries); |
| 147 | + } |
| 148 | + |
| 149 | + } |
| 150 | + |
| 151 | +} |
0 commit comments