Skip to content

Commit 24bae67

Browse files
Max BatischevMax Batischev
authored andcommitted
Add support customizing the serverLogoutSuccessHandler for OidcClientInitiatedServerLogoutSuccessHandler
Closes gh-14778
1 parent e771267 commit 24bae67

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2024 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 @@ public class OidcClientInitiatedServerLogoutSuccessHandler implements ServerLogo
5151

5252
private final ServerRedirectStrategy redirectStrategy = new DefaultServerRedirectStrategy();
5353

54-
private final RedirectServerLogoutSuccessHandler serverLogoutSuccessHandler = new RedirectServerLogoutSuccessHandler();
54+
private RedirectServerLogoutSuccessHandler serverLogoutSuccessHandler = new RedirectServerLogoutSuccessHandler();
5555

5656
private final ReactiveClientRegistrationRepository clientRegistrationRepository;
5757

@@ -189,4 +189,14 @@ public void setLogoutSuccessUrl(URI logoutSuccessUrl) {
189189
this.serverLogoutSuccessHandler.setLogoutSuccessUrl(logoutSuccessUrl);
190190
}
191191

192+
/**
193+
* Set the serverLogoutSuccessHandler.
194+
* @param serverLogoutSuccessHandler {@link RedirectServerLogoutSuccessHandler}
195+
* @since 6.3
196+
*/
197+
public void setServerLogoutSuccessHandler(RedirectServerLogoutSuccessHandler serverLogoutSuccessHandler) {
198+
Assert.notNull(serverLogoutSuccessHandler, "serverLogoutSuccessHandler cannot be null");
199+
this.serverLogoutSuccessHandler = serverLogoutSuccessHandler;
200+
}
201+
192202
}

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2024 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,6 +37,7 @@
3737
import org.springframework.security.oauth2.core.oidc.user.TestOidcUsers;
3838
import org.springframework.security.oauth2.core.user.TestOAuth2Users;
3939
import org.springframework.security.web.server.WebFilterExchange;
40+
import org.springframework.security.web.server.authentication.logout.RedirectServerLogoutSuccessHandler;
4041
import org.springframework.web.server.ServerWebExchange;
4142
import org.springframework.web.server.WebFilterChain;
4243

@@ -199,8 +200,25 @@ public void setPostLogoutRedirectUriTemplateWhenGivenNullThenThrowsException() {
199200
assertThatIllegalArgumentException().isThrownBy(() -> this.handler.setPostLogoutRedirectUri((String) null));
200201
}
201202

203+
@Test
204+
public void logoutWhenCustomRedirectServerLogoutSuccessHandlerSetThenRedirects() {
205+
OAuth2AuthenticationToken token = new OAuth2AuthenticationToken(TestOidcUsers.create(),
206+
AuthorityUtils.NO_AUTHORITIES, this.registration.getRegistrationId());
207+
given(this.exchange.getPrincipal()).willReturn(Mono.just(token));
208+
WebFilterExchange filterExchange = new WebFilterExchange(this.exchange, this.chain);
209+
this.handler.setServerLogoutSuccessHandler(new TestRedirectServerLogoutSuccessHandler());
210+
211+
this.handler.onLogoutSuccess(filterExchange, token).block();
212+
213+
assertThat(redirectedUrl(this.exchange)).isEqualTo("https://endpoint?id_token_hint=id-token");
214+
}
215+
202216
private String redirectedUrl(ServerWebExchange exchange) {
203217
return exchange.getResponse().getHeaders().getFirst("Location");
204218
}
205219

220+
private static class TestRedirectServerLogoutSuccessHandler extends RedirectServerLogoutSuccessHandler {
221+
222+
}
223+
206224
}

0 commit comments

Comments
 (0)