Skip to content

Commit 9593f9c

Browse files
philsttrjgrandja
authored andcommitted
Defer downstream filter execution if no OAuth2AuthorizedClient is found
Prior to this change, ServerOAuth2AuthorizedClientExchangeFilterFunction would invoke next.exchange: - first at assembly time inside the .switchIfEmpty call. - second at execution time inside .flatMap when a OAuth2AuthorizedClient is found. While this double-call should not technically cause any functional problems, since the Mono returned by the first call will not be subscribed if a OAuth2AuthorizedClient is found, it does result in a lot of unnecessary execution and object creation. There is no technical need to invoke the downstream filters twice. This change defers the call inside .switchIfEmpty, so that it will only execute at execution time if an OAuth2AuthorizedClient is not found. After this change, ServerOAuth2AuthorizedClientExchangeFilterFunction will not invoke next.exchange at assembly time, and will only execute next.exchange once per subscription at execution time. Fixes gh-6719
1 parent bf88e87 commit 9593f9c

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

oauth2/oauth2-client/src/main/java/org/springframework/security/oauth2/client/web/reactive/function/client/ServerOAuth2AuthorizedClientExchangeFilterFunction.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ public Mono<ClientResponse> filter(ClientRequest request, ExchangeFunction next)
227227
return authorizedClient(request, next)
228228
.map(authorizedClient -> bearer(request, authorizedClient))
229229
.flatMap(next::exchange)
230-
.switchIfEmpty(next.exchange(request));
230+
.switchIfEmpty(Mono.defer(() -> next.exchange(request)));
231231
}
232232

233233
private Mono<OAuth2AuthorizedClient> authorizedClient(ClientRequest request, ExchangeFunction next) {

0 commit comments

Comments
 (0)