|
16 | 16 |
|
17 | 17 | package org.springframework.security.config.web.server;
|
18 | 18 |
|
| 19 | +import org.jspecify.annotations.Nullable; |
19 | 20 | import org.junit.jupiter.api.Test;
|
20 | 21 | import org.openqa.selenium.WebDriver;
|
| 22 | +import reactor.core.publisher.Mono; |
21 | 23 |
|
| 24 | +import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; |
22 | 25 | import org.springframework.security.config.annotation.web.reactive.ServerHttpSecurityConfigurationBuilder;
|
| 26 | +import org.springframework.security.core.context.SecurityContext; |
23 | 27 | import org.springframework.security.htmlunit.server.WebTestClientHtmlUnitDriverBuilder;
|
24 | 28 | import org.springframework.security.test.web.reactive.server.WebTestClientBuilder;
|
25 | 29 | import org.springframework.security.web.server.SecurityWebFilterChain;
|
| 30 | +import org.springframework.security.web.server.authentication.logout.ServerLogoutHandler; |
| 31 | +import org.springframework.security.web.server.context.ServerSecurityContextRepository; |
26 | 32 | import org.springframework.security.web.server.context.WebSessionServerSecurityContextRepository;
|
27 | 33 | import org.springframework.security.web.server.util.matcher.ServerWebExchangeMatchers;
|
28 | 34 | import org.springframework.test.web.reactive.server.WebTestClient;
|
| 35 | +import org.springframework.util.LinkedMultiValueMap; |
| 36 | +import org.springframework.util.MultiValueMap; |
29 | 37 | import org.springframework.web.bind.annotation.GetMapping;
|
30 | 38 | import org.springframework.web.bind.annotation.RestController;
|
| 39 | +import org.springframework.web.server.ServerWebExchange; |
31 | 40 |
|
32 | 41 | import static org.assertj.core.api.Assertions.assertThat;
|
33 | 42 | import static org.springframework.security.config.Customizer.withDefaults;
|
@@ -210,6 +219,84 @@ public void logoutWhenCustomSecurityContextRepositoryThenLogsOut() {
|
210 | 219 | FormLoginTests.HomePage.to(driver, FormLoginTests.DefaultLoginPage.class).assertAt();
|
211 | 220 | }
|
212 | 221 |
|
| 222 | + @Test |
| 223 | + public void multipleLogoutHandlers() { |
| 224 | + InMemorySecurityContextRepository repository = new InMemorySecurityContextRepository(); |
| 225 | + MultiValueMap<String, String> logoutData = new LinkedMultiValueMap<>(); |
| 226 | + ServerLogoutHandler handler1 = (exchange, authentication) -> { |
| 227 | + logoutData.add("handler-header", "value1"); |
| 228 | + return Mono.empty(); |
| 229 | + }; |
| 230 | + ServerLogoutHandler handler2 = (exchange, authentication) -> { |
| 231 | + logoutData.add("handler-header", "value2"); |
| 232 | + return Mono.empty(); |
| 233 | + }; |
| 234 | + // @formatter:off |
| 235 | + SecurityWebFilterChain securityWebFilter = this.http |
| 236 | + .securityContextRepository(repository) |
| 237 | + .authorizeExchange((authorize) -> authorize |
| 238 | + .anyExchange().authenticated()) |
| 239 | + .formLogin(withDefaults()) |
| 240 | + .logout((logoutSpec) -> logoutSpec.logoutHandler((handlers) -> { |
| 241 | + handlers.add(handler1); |
| 242 | + handlers.add(0, handler2); |
| 243 | + })) |
| 244 | + .build(); |
| 245 | + WebTestClient webTestClient = WebTestClientBuilder |
| 246 | + .bindToWebFilters(securityWebFilter) |
| 247 | + .build(); |
| 248 | + WebDriver driver = WebTestClientHtmlUnitDriverBuilder |
| 249 | + .webTestClientSetup(webTestClient) |
| 250 | + .build(); |
| 251 | + // @formatter:on |
| 252 | + FormLoginTests.DefaultLoginPage loginPage = FormLoginTests.HomePage |
| 253 | + .to(driver, FormLoginTests.DefaultLoginPage.class) |
| 254 | + .assertAt(); |
| 255 | + // @formatter:off |
| 256 | + loginPage = loginPage.loginForm() |
| 257 | + .username("user") |
| 258 | + .password("invalid") |
| 259 | + .submit(FormLoginTests.DefaultLoginPage.class) |
| 260 | + .assertError(); |
| 261 | + FormLoginTests.HomePage homePage = loginPage.loginForm() |
| 262 | + .username("user") |
| 263 | + .password("password") |
| 264 | + .submit(FormLoginTests.HomePage.class); |
| 265 | + // @formatter:on |
| 266 | + homePage.assertAt(); |
| 267 | + SecurityContext savedContext = repository.getSavedContext(); |
| 268 | + assertThat(savedContext).isNotNull(); |
| 269 | + assertThat(savedContext.getAuthentication()).isInstanceOf(UsernamePasswordAuthenticationToken.class); |
| 270 | + |
| 271 | + loginPage = FormLoginTests.DefaultLogoutPage.to(driver).assertAt().logout(); |
| 272 | + loginPage.assertAt().assertLogout(); |
| 273 | + assertThat(logoutData).hasSize(1); |
| 274 | + assertThat(logoutData.get("handler-header")).containsExactly("value2", "value1"); |
| 275 | + savedContext = repository.getSavedContext(); |
| 276 | + assertThat(savedContext).isNull(); |
| 277 | + } |
| 278 | + |
| 279 | + private static class InMemorySecurityContextRepository implements ServerSecurityContextRepository { |
| 280 | + |
| 281 | + @Nullable private SecurityContext savedContext; |
| 282 | + |
| 283 | + @Override |
| 284 | + public Mono<Void> save(ServerWebExchange exchange, SecurityContext context) { |
| 285 | + this.savedContext = context; |
| 286 | + return Mono.empty(); |
| 287 | + } |
| 288 | + |
| 289 | + @Override |
| 290 | + public Mono<SecurityContext> load(ServerWebExchange exchange) { |
| 291 | + return Mono.justOrEmpty(this.savedContext); |
| 292 | + } |
| 293 | + |
| 294 | + @Nullable private SecurityContext getSavedContext() { |
| 295 | + return this.savedContext; |
| 296 | + } |
| 297 | + |
| 298 | + } |
| 299 | + |
213 | 300 | @RestController
|
214 | 301 | public static class HomeController {
|
215 | 302 |
|
|
0 commit comments