|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2019 the original author or authors. |
| 2 | + * Copyright 2002-2022 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
25 | 25 | import org.springframework.context.annotation.Bean;
|
26 | 26 | import org.springframework.http.MediaType;
|
27 | 27 | import org.springframework.security.config.annotation.ObjectPostProcessor;
|
| 28 | +import org.springframework.security.config.annotation.SecurityContextChangedListenerConfig; |
28 | 29 | import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
|
29 | 30 | import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
30 | 31 | import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
31 | 32 | import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
|
32 | 33 | import org.springframework.security.config.test.SpringTestContext;
|
33 | 34 | import org.springframework.security.config.test.SpringTestContextExtension;
|
| 35 | +import org.springframework.security.core.context.SecurityContextHolderStrategy; |
34 | 36 | import org.springframework.security.web.authentication.RememberMeServices;
|
35 | 37 | import org.springframework.security.web.authentication.logout.LogoutFilter;
|
36 | 38 | import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
|
|
40 | 42 |
|
41 | 43 | import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
42 | 44 | import static org.mockito.ArgumentMatchers.any;
|
| 45 | +import static org.mockito.Mockito.atLeastOnce; |
43 | 46 | import static org.mockito.Mockito.mock;
|
44 | 47 | import static org.mockito.Mockito.spy;
|
45 | 48 | import static org.mockito.Mockito.verify;
|
@@ -241,6 +244,22 @@ public void logoutWhenAcceptTextHtmlThenRedirectsToLogin() throws Exception {
|
241 | 244 | // @formatter:on
|
242 | 245 | }
|
243 | 246 |
|
| 247 | + @Test |
| 248 | + public void logoutWhenCustomSecurityContextHolderStrategyThenUses() throws Exception { |
| 249 | + this.spring.register(BasicSecurityConfig.class, SecurityContextChangedListenerConfig.class).autowire(); |
| 250 | + // @formatter:off |
| 251 | + MockHttpServletRequestBuilder logoutRequest = post("/logout") |
| 252 | + .with(csrf()) |
| 253 | + .with(user("user")) |
| 254 | + .header(HttpHeaders.ACCEPT, MediaType.TEXT_HTML_VALUE); |
| 255 | + this.mvc.perform(logoutRequest) |
| 256 | + .andExpect(status().isFound()) |
| 257 | + .andExpect(redirectedUrl("/login?logout")); |
| 258 | + // @formatter:on |
| 259 | + SecurityContextHolderStrategy strategy = this.spring.getContext().getBean(SecurityContextHolderStrategy.class); |
| 260 | + verify(strategy, atLeastOnce()).getContext(); |
| 261 | + } |
| 262 | + |
244 | 263 | // gh-3282
|
245 | 264 | @Test
|
246 | 265 | public void logoutWhenAcceptApplicationJsonThenReturnsStatusNoContent() throws Exception {
|
|
0 commit comments