|
35 | 35 | import org.springframework.mock.web.MockHttpSession;
|
36 | 36 | import org.springframework.security.access.AccessDeniedException;
|
37 | 37 | import org.springframework.security.authentication.TestingAuthenticationToken;
|
| 38 | +import org.springframework.security.config.annotation.SecurityContextChangedListenerConfig; |
38 | 39 | import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
39 | 40 | import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
|
40 | 41 | import org.springframework.security.config.test.SpringTestContext;
|
41 | 42 | import org.springframework.security.config.test.SpringTestContextExtension;
|
42 |
| -import org.springframework.security.core.context.SecurityContextHolder; |
| 43 | +import org.springframework.security.core.Authentication; |
| 44 | +import org.springframework.security.core.context.SecurityContextHolderStrategy; |
43 | 45 | import org.springframework.security.core.userdetails.User;
|
44 | 46 | import org.springframework.security.core.userdetails.UserDetails;
|
45 | 47 | import org.springframework.security.core.userdetails.UserDetailsService;
|
|
54 | 56 |
|
55 | 57 | import static org.assertj.core.api.Assertions.assertThat;
|
56 | 58 | import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
| 59 | +import static org.mockito.Mockito.atLeastOnce; |
| 60 | +import static org.mockito.Mockito.verify; |
57 | 61 | import static org.springframework.security.config.Customizer.withDefaults;
|
58 | 62 | import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.authentication;
|
59 | 63 | import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf;
|
@@ -134,6 +138,22 @@ public void loadConfigWhenDefaultConfigThenWebAsyncManagerIntegrationFilterAdded
|
134 | 138 | // @formatter:on
|
135 | 139 | }
|
136 | 140 |
|
| 141 | + @Test |
| 142 | + public void asyncDispatchWhenCustomSecurityContextHolderStrategyThenUses() throws Exception { |
| 143 | + this.spring.register(DefaultWithFilterChainConfig.class, SecurityContextChangedListenerConfig.class, |
| 144 | + NameController.class).autowire(); |
| 145 | + // @formatter:off |
| 146 | + MockHttpServletRequestBuilder requestWithBob = get("/name").with(user("Bob")); |
| 147 | + MvcResult mvcResult = this.mockMvc.perform(requestWithBob) |
| 148 | + .andExpect(request().asyncStarted()) |
| 149 | + .andReturn(); |
| 150 | + this.mockMvc.perform(asyncDispatch(mvcResult)) |
| 151 | + .andExpect(status().isOk()) |
| 152 | + .andExpect(content().string("Bob")); |
| 153 | + // @formatter:on |
| 154 | + verify(this.spring.getContext().getBean(SecurityContextHolderStrategy.class), atLeastOnce()).getContext(); |
| 155 | + } |
| 156 | + |
137 | 157 | @Test
|
138 | 158 | public void getWhenDefaultFilterChainBeanThenAnonymousPermitted() throws Exception {
|
139 | 159 | this.spring.register(AuthorizeRequestsConfig.class, UserDetailsConfig.class, BaseController.class).autowire();
|
@@ -243,8 +263,8 @@ public void configureWhenDefaultConfigurerAsSpringFactoryThenDefaultConfigurerAp
|
243 | 263 | static class NameController {
|
244 | 264 |
|
245 | 265 | @GetMapping("/name")
|
246 |
| - Callable<String> name() { |
247 |
| - return () -> SecurityContextHolder.getContext().getAuthentication().getName(); |
| 266 | + Callable<String> name(Authentication authentication) { |
| 267 | + return () -> authentication.getName(); |
248 | 268 | }
|
249 | 269 |
|
250 | 270 | }
|
|
0 commit comments