Skip to content

Commit f35d956

Browse files
frantictickticksjohnr
authored andcommitted
Add setRedirectStrategy to OidcClientInitiatedServerLogoutSuccessHandler
Closes gh-16556 Signed-off-by: Max Batischev <mblancer@mail.ru>
1 parent ede22de commit f35d956

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

oauth2/oauth2-client/src/main/java/org/springframework/security/oauth2/client/oidc/web/server/logout/OidcClientInitiatedServerLogoutSuccessHandler.java

+13-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2024 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -51,7 +51,7 @@
5151
*/
5252
public class OidcClientInitiatedServerLogoutSuccessHandler implements ServerLogoutSuccessHandler {
5353

54-
private final ServerRedirectStrategy redirectStrategy = new DefaultServerRedirectStrategy();
54+
private ServerRedirectStrategy redirectStrategy = new DefaultServerRedirectStrategy();
5555

5656
private final RedirectServerLogoutSuccessHandler serverLogoutSuccessHandler = new RedirectServerLogoutSuccessHandler();
5757

@@ -199,6 +199,17 @@ public void setRedirectUriResolver(Converter<RedirectUriParameters, Mono<String>
199199
this.redirectUriResolver = redirectUriResolver;
200200
}
201201

202+
/**
203+
* Set the {@link ServerRedirectStrategy} to use, default
204+
* {@link DefaultServerRedirectStrategy}
205+
* @param redirectStrategy {@link ServerRedirectStrategy}
206+
* @since 6.5
207+
*/
208+
public void setRedirectStrategy(ServerRedirectStrategy redirectStrategy) {
209+
Assert.notNull(redirectStrategy, "redirectStrategy cannot be null");
210+
this.redirectStrategy = redirectStrategy;
211+
}
212+
202213
/**
203214
* Parameters, required for redirect URI resolving.
204215
*

oauth2/oauth2-client/src/test/java/org/springframework/security/oauth2/client/oidc/web/server/logout/OidcClientInitiatedServerLogoutSuccessHandlerTests.java

+26-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2024 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -37,14 +37,18 @@
3737
import org.springframework.security.oauth2.client.registration.TestClientRegistrations;
3838
import org.springframework.security.oauth2.core.oidc.user.TestOidcUsers;
3939
import org.springframework.security.oauth2.core.user.TestOAuth2Users;
40+
import org.springframework.security.web.server.ServerRedirectStrategy;
4041
import org.springframework.security.web.server.WebFilterExchange;
4142
import org.springframework.web.server.ServerWebExchange;
4243
import org.springframework.web.server.WebFilterChain;
4344

4445
import static org.assertj.core.api.Assertions.assertThat;
4546
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
47+
import static org.mockito.ArgumentMatchers.any;
4648
import static org.mockito.BDDMockito.given;
4749
import static org.mockito.Mockito.mock;
50+
import static org.mockito.Mockito.times;
51+
import static org.mockito.Mockito.verify;
4852

4953
/**
5054
* Tests for {@link OidcClientInitiatedServerLogoutSuccessHandler}
@@ -219,6 +223,27 @@ public void logoutWhenCustomRedirectUriResolverSetThenRedirects() {
219223
assertThat(redirectedUrl(this.exchange)).isEqualTo("https://test.com");
220224
}
221225

226+
@Test
227+
public void setRedirectStrategyWhenGivenNullThenThrowsException() {
228+
assertThatIllegalArgumentException().isThrownBy(() -> this.handler.setRedirectStrategy(null));
229+
}
230+
231+
@Test
232+
public void logoutWhenCustomRedirectStrategySetThenCustomRedirectStrategyUse() {
233+
ServerRedirectStrategy redirectStrategy = mock(ServerRedirectStrategy.class);
234+
given(redirectStrategy.sendRedirect(any(), any())).willReturn(Mono.empty());
235+
OAuth2AuthenticationToken token = new OAuth2AuthenticationToken(TestOidcUsers.create(),
236+
AuthorityUtils.NO_AUTHORITIES, this.registration.getRegistrationId());
237+
WebFilterExchange filterExchange = new WebFilterExchange(this.exchange, this.chain);
238+
given(this.exchange.getRequest())
239+
.willReturn(MockServerHttpRequest.get("/").queryParam("location", "https://test.com").build());
240+
this.handler.setRedirectStrategy(redirectStrategy);
241+
242+
this.handler.onLogoutSuccess(filterExchange, token).block();
243+
244+
verify(redirectStrategy, times(1)).sendRedirect(any(), any());
245+
}
246+
222247
private String redirectedUrl(ServerWebExchange exchange) {
223248
return exchange.getResponse().getHeaders().getFirst("Location");
224249
}

0 commit comments

Comments
 (0)