Skip to content

Commit

Permalink
Add support customizing the serverLogoutSuccessHandler for OidcClient…
Browse files Browse the repository at this point in the history
…InitiatedServerLogoutSuccessHandler

Closes gh-14778
  • Loading branch information
Max Batischev authored and Max Batischev committed Mar 27, 2024
1 parent e771267 commit 24bae67
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2024 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.
Expand Down Expand Up @@ -51,7 +51,7 @@ public class OidcClientInitiatedServerLogoutSuccessHandler implements ServerLogo

private final ServerRedirectStrategy redirectStrategy = new DefaultServerRedirectStrategy();

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

private final ReactiveClientRegistrationRepository clientRegistrationRepository;

Expand Down Expand Up @@ -189,4 +189,14 @@ public void setLogoutSuccessUrl(URI logoutSuccessUrl) {
this.serverLogoutSuccessHandler.setLogoutSuccessUrl(logoutSuccessUrl);
}

/**
* Set the serverLogoutSuccessHandler.
* @param serverLogoutSuccessHandler {@link RedirectServerLogoutSuccessHandler}
* @since 6.3
*/
public void setServerLogoutSuccessHandler(RedirectServerLogoutSuccessHandler serverLogoutSuccessHandler) {
Assert.notNull(serverLogoutSuccessHandler, "serverLogoutSuccessHandler cannot be null");
this.serverLogoutSuccessHandler = serverLogoutSuccessHandler;
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2024 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.
Expand Down Expand Up @@ -37,6 +37,7 @@
import org.springframework.security.oauth2.core.oidc.user.TestOidcUsers;
import org.springframework.security.oauth2.core.user.TestOAuth2Users;
import org.springframework.security.web.server.WebFilterExchange;
import org.springframework.security.web.server.authentication.logout.RedirectServerLogoutSuccessHandler;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.WebFilterChain;

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

@Test
public void logoutWhenCustomRedirectServerLogoutSuccessHandlerSetThenRedirects() {
OAuth2AuthenticationToken token = new OAuth2AuthenticationToken(TestOidcUsers.create(),
AuthorityUtils.NO_AUTHORITIES, this.registration.getRegistrationId());
given(this.exchange.getPrincipal()).willReturn(Mono.just(token));
WebFilterExchange filterExchange = new WebFilterExchange(this.exchange, this.chain);
this.handler.setServerLogoutSuccessHandler(new TestRedirectServerLogoutSuccessHandler());

this.handler.onLogoutSuccess(filterExchange, token).block();

assertThat(redirectedUrl(this.exchange)).isEqualTo("https://endpoint?id_token_hint=id-token");
}

private String redirectedUrl(ServerWebExchange exchange) {
return exchange.getResponse().getHeaders().getFirst("Location");
}

private static class TestRedirectServerLogoutSuccessHandler extends RedirectServerLogoutSuccessHandler {

}

}

0 comments on commit 24bae67

Please sign in to comment.