|
22 | 22 | import org.junit.Test;
|
23 | 23 | import org.springframework.beans.factory.annotation.Autowired;
|
24 | 24 | import org.springframework.context.annotation.Bean;
|
| 25 | +import org.springframework.context.annotation.Configuration; |
25 | 26 | import org.springframework.security.access.AccessDeniedException;
|
26 | 27 | import org.springframework.security.authentication.AuthenticationManager;
|
27 | 28 | import org.springframework.security.authentication.AuthenticationTrustResolver;
|
|
44 | 45 | import org.springframework.security.web.authentication.logout.LogoutSuccessEventPublishingLogoutHandler;
|
45 | 46 | import org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter;
|
46 | 47 | import org.springframework.test.web.servlet.MockMvc;
|
| 48 | +import org.springframework.test.web.servlet.MvcResult; |
| 49 | +import org.springframework.test.web.servlet.setup.MockMvcBuilders; |
47 | 50 | import org.springframework.web.bind.annotation.GetMapping;
|
48 | 51 | import org.springframework.web.bind.annotation.RestController;
|
| 52 | +import org.springframework.web.context.ConfigurableWebApplicationContext; |
49 | 53 |
|
50 | 54 | import javax.servlet.Filter;
|
| 55 | +import javax.servlet.ServletException; |
51 | 56 | import javax.servlet.http.HttpServletRequest;
|
52 | 57 | import javax.servlet.http.HttpServletResponse;
|
53 | 58 |
|
|
60 | 65 | import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestBuilders.formLogin;
|
61 | 66 | import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.authentication;
|
62 | 67 | import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.user;
|
| 68 | +import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.springSecurity; |
63 | 69 | import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
64 | 70 | import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
65 | 71 |
|
@@ -329,6 +335,39 @@ protected void configure(HttpSecurity http) throws Exception {
|
329 | 335 | }
|
330 | 336 | }
|
331 | 337 |
|
| 338 | + @Test |
| 339 | + public void logoutServletApiWhenCsrfDisabled() throws Exception { |
| 340 | + ConfigurableWebApplicationContext context = this.spring.register(CsrfDisabledConfig.class).getContext(); |
| 341 | + MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(context) |
| 342 | + .apply(springSecurity()) |
| 343 | + .build(); |
| 344 | + MvcResult mvcResult = mockMvc.perform(get("/")) |
| 345 | + .andReturn(); |
| 346 | + assertThat(mvcResult.getRequest().getSession(false)).isNull(); |
| 347 | + } |
| 348 | + |
| 349 | + @Configuration |
| 350 | + @EnableWebSecurity |
| 351 | + static class CsrfDisabledConfig extends WebSecurityConfigurerAdapter { |
| 352 | + @Override |
| 353 | + protected void configure(HttpSecurity http) throws Exception { |
| 354 | + // @formatter:off |
| 355 | + http |
| 356 | + .csrf().disable(); |
| 357 | + // @formatter:on |
| 358 | + } |
| 359 | + |
| 360 | + @RestController |
| 361 | + static class LogoutController { |
| 362 | + @GetMapping("/") |
| 363 | + String logout(HttpServletRequest request) throws ServletException { |
| 364 | + request.getSession().setAttribute("foo", "bar"); |
| 365 | + request.logout(); |
| 366 | + return "logout"; |
| 367 | + } |
| 368 | + } |
| 369 | + } |
| 370 | + |
332 | 371 | private <T extends Filter> T getFilter(Class<T> filterClass) {
|
333 | 372 | return (T) getFilters().stream()
|
334 | 373 | .filter(filterClass::isInstance)
|
|
0 commit comments