Skip to content

Commit beda4db

Browse files
eleftheriaskostya05983
authored andcommitted
Allow configuration of request cache through nested builder
Issue: spring-projectsgh-5557
1 parent 4ba70e2 commit beda4db

File tree

2 files changed

+127
-1
lines changed

2 files changed

+127
-1
lines changed

config/src/main/java/org/springframework/security/config/annotation/web/builders/HttpSecurity.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1056,6 +1056,46 @@ public RequestCacheConfigurer<HttpSecurity> requestCache() throws Exception {
10561056
return getOrApply(new RequestCacheConfigurer<>());
10571057
}
10581058

1059+
/**
1060+
* Allows configuring the Request Cache. For example, a protected page (/protected)
1061+
* may be requested prior to authentication. The application will redirect the user to
1062+
* a login page. After authentication, Spring Security will redirect the user to the
1063+
* originally requested protected page (/protected). This is automatically applied
1064+
* when using {@link WebSecurityConfigurerAdapter}.
1065+
*
1066+
* <h2>Example Custom Configuration</h2>
1067+
*
1068+
* The following example demonstrates how to disable request caching.
1069+
*
1070+
* <pre>
1071+
* &#064;Configuration
1072+
* &#064;EnableWebSecurity
1073+
* public class RequestCacheDisabledSecurityConfig extends WebSecurityConfigurerAdapter {
1074+
*
1075+
* &#064;Override
1076+
* protected void configure(HttpSecurity http) throws Exception {
1077+
* http
1078+
* .authorizeRequests()
1079+
* .antMatchers(&quot;/**&quot;).hasRole(&quot;USER&quot;)
1080+
* .and()
1081+
* .requestCache(requestCache ->
1082+
* requestCache.disable()
1083+
* );
1084+
* }
1085+
* }
1086+
* </pre>
1087+
*
1088+
* @param requestCacheCustomizer the {@link Customizer} to provide more options for
1089+
* the {@link RequestCacheConfigurer}
1090+
* @return the {@link HttpSecurity} for further customizations
1091+
* @throws Exception
1092+
*/
1093+
public HttpSecurity requestCache(Customizer<RequestCacheConfigurer<HttpSecurity>> requestCacheCustomizer)
1094+
throws Exception {
1095+
requestCacheCustomizer.customize(getOrApply(new RequestCacheConfigurer<>()));
1096+
return HttpSecurity.this;
1097+
}
1098+
10591099
/**
10601100
* Allows configuring exception handling. This is automatically applied when using
10611101
* {@link WebSecurityConfigurerAdapter}.

config/src/test/java/org/springframework/security/config/annotation/web/configurers/RequestCacheConfigurerTests.java

Lines changed: 87 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -33,6 +33,7 @@
3333
import org.springframework.security.config.test.SpringTestRule;
3434
import org.springframework.security.core.userdetails.User;
3535
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
36+
import org.springframework.security.web.savedrequest.NullRequestCache;
3637
import org.springframework.security.web.savedrequest.RequestCache;
3738
import org.springframework.security.web.savedrequest.RequestCacheAwareFilter;
3839
import org.springframework.test.web.servlet.MockMvc;
@@ -42,6 +43,7 @@
4243
import static org.mockito.Mockito.mock;
4344
import static org.mockito.Mockito.spy;
4445
import static org.mockito.Mockito.verify;
46+
import static org.springframework.security.config.Customizer.withDefaults;
4547
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf;
4648
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
4749
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
@@ -271,6 +273,90 @@ protected void configure(HttpSecurity http) throws Exception {
271273
}
272274
}
273275

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+
274360
@EnableWebSecurity
275361
static class DefaultSecurityConfig {
276362

0 commit comments

Comments
 (0)