Skip to content

Commit

Permalink
Add setRedirectStrategy to OidcClientInitiatedServerLogoutSuccessHandler
Browse files Browse the repository at this point in the history
Closes gh-16556

Signed-off-by: Max Batischev <[email protected]>
  • Loading branch information
franticticktick committed Feb 8, 2025
1 parent 556a430 commit df80ff5
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2024 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.
Expand Down Expand Up @@ -51,7 +51,7 @@
*/
public class OidcClientInitiatedServerLogoutSuccessHandler implements ServerLogoutSuccessHandler {

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

private final RedirectServerLogoutSuccessHandler serverLogoutSuccessHandler = new RedirectServerLogoutSuccessHandler();

Expand Down Expand Up @@ -199,6 +199,17 @@ public void setRedirectUriResolver(Converter<RedirectUriParameters, Mono<String>
this.redirectUriResolver = redirectUriResolver;
}

/**
* Set the {@link ServerRedirectStrategy} to use, default
* {@link DefaultServerRedirectStrategy}
* @param redirectStrategy {@link ServerRedirectStrategy}
* @since 6.5
*/
public void setRedirectStrategy(ServerRedirectStrategy redirectStrategy) {
Assert.notNull(redirectStrategy, "redirectStrategy cannot be null");
this.redirectStrategy = redirectStrategy;
}

/**
* Parameters, required for redirect URI resolving.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2024 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.
Expand Down Expand Up @@ -37,14 +37,18 @@
import org.springframework.security.oauth2.client.registration.TestClientRegistrations;
import org.springframework.security.oauth2.core.oidc.user.TestOidcUsers;
import org.springframework.security.oauth2.core.user.TestOAuth2Users;
import org.springframework.security.web.server.ServerRedirectStrategy;
import org.springframework.security.web.server.WebFilterExchange;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.WebFilterChain;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

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

@Test
public void setRedirectStrategyWhenGivenNullThenThrowsException() {
assertThatIllegalArgumentException().isThrownBy(() -> this.handler.setRedirectStrategy(null));
}

@Test
public void logoutWhenCustomRedirectStrategySetThenCustomRedirectStrategyUse() {
ServerRedirectStrategy redirectStrategy = mock(ServerRedirectStrategy.class);
given(redirectStrategy.sendRedirect(any(), any())).willReturn(Mono.empty());
OAuth2AuthenticationToken token = new OAuth2AuthenticationToken(TestOidcUsers.create(),
AuthorityUtils.NO_AUTHORITIES, this.registration.getRegistrationId());
WebFilterExchange filterExchange = new WebFilterExchange(this.exchange, this.chain);
given(this.exchange.getRequest())
.willReturn(MockServerHttpRequest.get("/").queryParam("location", "https://test.com").build());
this.handler.setRedirectStrategy(redirectStrategy);

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

verify(redirectStrategy, times(1)).sendRedirect(any(), any());
}

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

0 comments on commit df80ff5

Please sign in to comment.