ReactiveAuthorizationManager replace deprecated #check calls with #authorize
Closes gh-16936 Signed-off-by: Evgeniy Cheban <mister.cheban@gmail.com>
This commit is contained in:
committed by
Josh Cummings
parent
b0cecb37d2
commit
092bbfc8e7
+17
-4
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2020 the original author or authors.
|
||||
* Copyright 2002-2025 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -26,6 +26,7 @@ import reactor.core.publisher.Mono;
|
||||
|
||||
import org.springframework.core.log.LogMessage;
|
||||
import org.springframework.security.authorization.AuthorizationDecision;
|
||||
import org.springframework.security.authorization.AuthorizationResult;
|
||||
import org.springframework.security.authorization.ReactiveAuthorizationManager;
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.web.server.util.matcher.ServerWebExchangeMatcher.MatchResult;
|
||||
@@ -35,6 +36,7 @@ import org.springframework.web.server.ServerWebExchange;
|
||||
/**
|
||||
* @author Rob Winch
|
||||
* @author Mathieu Ouellet
|
||||
* @author Evgeniy Cheban
|
||||
* @since 5.0
|
||||
*/
|
||||
public final class DelegatingReactiveAuthorizationManager implements ReactiveAuthorizationManager<ServerWebExchange> {
|
||||
@@ -49,11 +51,22 @@ public final class DelegatingReactiveAuthorizationManager implements ReactiveAut
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated please use {@link #authorize(Mono, Object)} instead
|
||||
* @deprecated please use {@link #authorize(Mono, ServerWebExchange)} instead
|
||||
*/
|
||||
@Deprecated
|
||||
@Override
|
||||
public Mono<AuthorizationDecision> check(Mono<Authentication> authentication, ServerWebExchange exchange) {
|
||||
return authorize(authentication, exchange).flatMap((result) -> {
|
||||
if (result instanceof AuthorizationDecision decision) {
|
||||
return Mono.just(decision);
|
||||
}
|
||||
return Mono.error(new IllegalArgumentException(
|
||||
"Please call #authorize or ensure that the returned result is of type Mono<AuthorizationDecision>"));
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<AuthorizationResult> authorize(Mono<Authentication> authentication, ServerWebExchange exchange) {
|
||||
return Flux.fromIterable(this.mappings)
|
||||
.concatMap((mapping) -> mapping.getMatcher()
|
||||
.matches(exchange)
|
||||
@@ -63,10 +76,10 @@ public final class DelegatingReactiveAuthorizationManager implements ReactiveAut
|
||||
logger.debug(LogMessage.of(() -> "Checking authorization on '"
|
||||
+ exchange.getRequest().getPath().pathWithinApplication() + "' using "
|
||||
+ mapping.getEntry()));
|
||||
return mapping.getEntry().check(authentication, new AuthorizationContext(exchange, variables));
|
||||
return mapping.getEntry().authorize(authentication, new AuthorizationContext(exchange, variables));
|
||||
}))
|
||||
.next()
|
||||
.defaultIfEmpty(new AuthorizationDecision(false));
|
||||
.switchIfEmpty(Mono.fromCallable(() -> new AuthorizationDecision(false)));
|
||||
}
|
||||
|
||||
public static DelegatingReactiveAuthorizationManager.Builder builder() {
|
||||
|
||||
+3
-1
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2017 the original author or authors.
|
||||
* Copyright 2002-2025 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -81,6 +81,7 @@ public class DelegatingReactiveAuthorizationManagerTests {
|
||||
given(this.match1.matches(any())).willReturn(ServerWebExchangeMatcher.MatchResult.match());
|
||||
given(this.delegate1.check(eq(this.authentication), any(AuthorizationContext.class)))
|
||||
.willReturn(Mono.just(this.decision));
|
||||
given(this.delegate1.authorize(eq(this.authentication), any(AuthorizationContext.class))).willCallRealMethod();
|
||||
assertThat(this.manager.check(this.authentication, this.exchange).block()).isEqualTo(this.decision);
|
||||
verifyNoMoreInteractions(this.match2, this.delegate2);
|
||||
}
|
||||
@@ -91,6 +92,7 @@ public class DelegatingReactiveAuthorizationManagerTests {
|
||||
given(this.match2.matches(any())).willReturn(ServerWebExchangeMatcher.MatchResult.match());
|
||||
given(this.delegate2.check(eq(this.authentication), any(AuthorizationContext.class)))
|
||||
.willReturn(Mono.just(this.decision));
|
||||
given(this.delegate2.authorize(eq(this.authentication), any(AuthorizationContext.class))).willCallRealMethod();
|
||||
assertThat(this.manager.check(this.authentication, this.exchange).block()).isEqualTo(this.decision);
|
||||
verifyNoMoreInteractions(this.delegate1);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user