|
14 | 14 | * limitations under the License.
|
15 | 15 | */
|
16 | 16 |
|
17 |
| -package org.springframework.security.oauth2.server.resource.web.server; |
18 |
| - |
19 |
| -import java.util.Map; |
20 |
| -import java.util.function.Consumer; |
| 17 | +package org.springframework.security.oauth2.server.resource.web.reactive.function.client; |
21 | 18 |
|
22 | 19 | import reactor.core.publisher.Mono;
|
23 | 20 |
|
|
52 | 49 | * @author Josh Cummings
|
53 | 50 | * @since 5.2
|
54 | 51 | */
|
55 |
| -public class ServerBearerExchangeFilterFunction |
| 52 | +public final class ServerBearerExchangeFilterFunction |
56 | 53 | implements ExchangeFilterFunction {
|
57 | 54 |
|
58 |
| - private static final String AUTHENTICATION_ATTR_NAME = Authentication.class.getName(); |
59 |
| - |
60 | 55 | private static final AnonymousAuthenticationToken ANONYMOUS_USER_TOKEN = new AnonymousAuthenticationToken("anonymous", "anonymousUser",
|
61 | 56 | AuthorityUtils.createAuthorityList("ROLE_USER"));
|
62 | 57 |
|
63 |
| - /** |
64 |
| - * Modifies the {@link ClientRequest#attributes()} to include the {@link Authentication} to be used for |
65 |
| - * providing the Bearer Token. Example usage: |
66 |
| - * |
67 |
| - * <pre> |
68 |
| - * WebClient webClient = WebClient.builder() |
69 |
| - * .filter(new ServerBearerExchangeFilterFunction()) |
70 |
| - * .build(); |
71 |
| - * Mono<String> response = webClient |
72 |
| - * .get() |
73 |
| - * .uri(uri) |
74 |
| - * .attributes(authentication(authentication)) |
75 |
| - * // ... |
76 |
| - * .retrieve() |
77 |
| - * .bodyToMono(String.class); |
78 |
| - * </pre> |
79 |
| - * @param authentication the {@link Authentication} to use |
80 |
| - * @return the {@link Consumer} to populate the client request attributes |
81 |
| - */ |
82 |
| - public static Consumer<Map<String, Object>> authentication(Authentication authentication) { |
83 |
| - return attributes -> attributes.put(AUTHENTICATION_ATTR_NAME, authentication); |
84 |
| - } |
85 |
| - |
86 | 58 | /**
|
87 | 59 | * {@inheritDoc}
|
88 | 60 | */
|
89 | 61 | @Override
|
90 | 62 | public Mono<ClientResponse> filter(ClientRequest request, ExchangeFunction next) {
|
91 |
| - return oauth2Token(request.attributes()) |
92 |
| - .map(oauth2Token -> bearer(request, oauth2Token)) |
| 63 | + return oauth2Token() |
| 64 | + .map(token -> bearer(request, token)) |
93 | 65 | .defaultIfEmpty(request)
|
94 | 66 | .flatMap(next::exchange);
|
95 | 67 | }
|
96 | 68 |
|
97 |
| - private Mono<AbstractOAuth2Token> oauth2Token(Map<String, Object> attrs) { |
98 |
| - return Mono.justOrEmpty(attrs.get(AUTHENTICATION_ATTR_NAME)) |
99 |
| - .cast(Authentication.class) |
100 |
| - .switchIfEmpty(currentAuthentication()) |
| 69 | + private Mono<AbstractOAuth2Token> oauth2Token() { |
| 70 | + return currentAuthentication() |
101 | 71 | .filter(authentication -> authentication.getCredentials() instanceof AbstractOAuth2Token)
|
102 | 72 | .map(Authentication::getCredentials)
|
103 | 73 | .cast(AbstractOAuth2Token.class);
|
|
0 commit comments