|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2018 the original author or authors. |
| 2 | + * Copyright 2002-2019 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.
|
|
33 | 33 | import org.springframework.security.config.test.SpringTestRule;
|
34 | 34 | import org.springframework.security.core.userdetails.User;
|
35 | 35 | import org.springframework.security.provisioning.InMemoryUserDetailsManager;
|
| 36 | +import org.springframework.security.web.savedrequest.NullRequestCache; |
36 | 37 | import org.springframework.security.web.savedrequest.RequestCache;
|
37 | 38 | import org.springframework.security.web.savedrequest.RequestCacheAwareFilter;
|
38 | 39 | import org.springframework.test.web.servlet.MockMvc;
|
|
42 | 43 | import static org.mockito.Mockito.mock;
|
43 | 44 | import static org.mockito.Mockito.spy;
|
44 | 45 | import static org.mockito.Mockito.verify;
|
| 46 | +import static org.springframework.security.config.Customizer.withDefaults; |
45 | 47 | import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf;
|
46 | 48 | import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
47 | 49 | import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
|
@@ -271,6 +273,90 @@ protected void configure(HttpSecurity http) throws Exception {
|
271 | 273 | }
|
272 | 274 | }
|
273 | 275 |
|
| 276 | + @Test |
| 277 | + public void getWhenRequestCacheIsDisabledInLambdaThenExceptionTranslationFilterDoesNotStoreRequest() throws Exception { |
| 278 | + this.spring.register(RequestCacheDisabledInLambdaConfig.class, DefaultSecurityConfig.class).autowire(); |
| 279 | + |
| 280 | + MockHttpSession session = (MockHttpSession) |
| 281 | + this.mvc.perform(get("/bob")) |
| 282 | + .andReturn().getRequest().getSession(); |
| 283 | + |
| 284 | + this.mvc.perform(formLogin(session)) |
| 285 | + .andExpect(redirectedUrl("/")); |
| 286 | + } |
| 287 | + |
| 288 | + @EnableWebSecurity |
| 289 | + static class RequestCacheDisabledInLambdaConfig extends WebSecurityConfigurerAdapter { |
| 290 | + @Override |
| 291 | + protected void configure(HttpSecurity http) throws Exception { |
| 292 | + // @formatter:off |
| 293 | + http |
| 294 | + .authorizeRequests() |
| 295 | + .anyRequest().authenticated() |
| 296 | + .and() |
| 297 | + .formLogin(withDefaults()) |
| 298 | + .requestCache(RequestCacheConfigurer::disable); |
| 299 | + // @formatter:on |
| 300 | + } |
| 301 | + } |
| 302 | + |
| 303 | + @Test |
| 304 | + public void getWhenRequestCacheInLambdaThenRedirectedToCachedPage() throws Exception { |
| 305 | + this.spring.register(RequestCacheInLambdaConfig.class, DefaultSecurityConfig.class).autowire(); |
| 306 | + |
| 307 | + MockHttpSession session = (MockHttpSession) |
| 308 | + this.mvc.perform(get("/bob")) |
| 309 | + .andReturn().getRequest().getSession(); |
| 310 | + |
| 311 | + this.mvc.perform(formLogin(session)) |
| 312 | + .andExpect(redirectedUrl("http://localhost/bob")); |
| 313 | + } |
| 314 | + |
| 315 | + @EnableWebSecurity |
| 316 | + static class RequestCacheInLambdaConfig extends WebSecurityConfigurerAdapter { |
| 317 | + @Override |
| 318 | + protected void configure(HttpSecurity http) throws Exception { |
| 319 | + // @formatter:off |
| 320 | + http |
| 321 | + .authorizeRequests() |
| 322 | + .anyRequest().authenticated() |
| 323 | + .and() |
| 324 | + .formLogin(withDefaults()) |
| 325 | + .requestCache(withDefaults()); |
| 326 | + // @formatter:on |
| 327 | + } |
| 328 | + } |
| 329 | + |
| 330 | + @Test |
| 331 | + public void getWhenCustomRequestCacheInLambdaThenCustomRequestCacheUsed() throws Exception { |
| 332 | + this.spring.register(CustomRequestCacheInLambdaConfig.class, DefaultSecurityConfig.class).autowire(); |
| 333 | + |
| 334 | + MockHttpSession session = (MockHttpSession) |
| 335 | + this.mvc.perform(get("/bob")) |
| 336 | + .andReturn().getRequest().getSession(); |
| 337 | + |
| 338 | + this.mvc.perform(formLogin(session)) |
| 339 | + .andExpect(redirectedUrl("/")); |
| 340 | + } |
| 341 | + |
| 342 | + @EnableWebSecurity |
| 343 | + static class CustomRequestCacheInLambdaConfig extends WebSecurityConfigurerAdapter { |
| 344 | + @Override |
| 345 | + protected void configure(HttpSecurity http) throws Exception { |
| 346 | + // @formatter:off |
| 347 | + http |
| 348 | + .authorizeRequests() |
| 349 | + .anyRequest().authenticated() |
| 350 | + .and() |
| 351 | + .formLogin(withDefaults()) |
| 352 | + .requestCache(requestCache -> |
| 353 | + requestCache |
| 354 | + .requestCache(new NullRequestCache()) |
| 355 | + ); |
| 356 | + // @formatter:on |
| 357 | + } |
| 358 | + } |
| 359 | + |
274 | 360 | @EnableWebSecurity
|
275 | 361 | static class DefaultSecurityConfig {
|
276 | 362 |
|
|
0 commit comments