Skip to content

Commit 40ff837

Browse files
committed
Polish Server|ServletBearerExchangeFilterFunction
Fixes gh-7353
1 parent e6618d4 commit 40ff837

File tree

5 files changed

+120
-303
lines changed

5 files changed

+120
-303
lines changed

oauth2/oauth2-resource-server/src/main/java/org/springframework/security/oauth2/server/resource/web/ServletBearerExchangeFilterFunction.java

-248
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

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;
2118

2219
import reactor.core.publisher.Mono;
2320

@@ -52,52 +49,25 @@
5249
* @author Josh Cummings
5350
* @since 5.2
5451
*/
55-
public class ServerBearerExchangeFilterFunction
52+
public final class ServerBearerExchangeFilterFunction
5653
implements ExchangeFilterFunction {
5754

58-
private static final String AUTHENTICATION_ATTR_NAME = Authentication.class.getName();
59-
6055
private static final AnonymousAuthenticationToken ANONYMOUS_USER_TOKEN = new AnonymousAuthenticationToken("anonymous", "anonymousUser",
6156
AuthorityUtils.createAuthorityList("ROLE_USER"));
6257

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-
8658
/**
8759
* {@inheritDoc}
8860
*/
8961
@Override
9062
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))
9365
.defaultIfEmpty(request)
9466
.flatMap(next::exchange);
9567
}
9668

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()
10171
.filter(authentication -> authentication.getCredentials() instanceof AbstractOAuth2Token)
10272
.map(Authentication::getCredentials)
10373
.cast(AbstractOAuth2Token.class);

0 commit comments

Comments
 (0)