Skip to content

Commit 33b492d

Browse files
author
Steve Riesenberg
committed
Default to DelegatingSecurityContextRepository
Closes gh-12023 Closes gh-12049
1 parent e238b72 commit 33b492d

File tree

5 files changed

+60
-30
lines changed

5 files changed

+60
-30
lines changed

config/src/main/java/org/springframework/security/config/annotation/web/configurers/SecurityContextConfigurer.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@
2121
import org.springframework.security.config.http.SessionCreationPolicy;
2222
import org.springframework.security.core.context.SecurityContext;
2323
import org.springframework.security.core.context.SecurityContextHolder;
24+
import org.springframework.security.web.context.DelegatingSecurityContextRepository;
2425
import org.springframework.security.web.context.HttpSessionSecurityContextRepository;
26+
import org.springframework.security.web.context.RequestAttributeSecurityContextRepository;
2527
import org.springframework.security.web.context.SecurityContextHolderFilter;
2628
import org.springframework.security.web.context.SecurityContextPersistenceFilter;
2729
import org.springframework.security.web.context.SecurityContextRepository;
@@ -96,7 +98,8 @@ SecurityContextRepository getSecurityContextRepository() {
9698
SecurityContextRepository securityContextRepository = getBuilder()
9799
.getSharedObject(SecurityContextRepository.class);
98100
if (securityContextRepository == null) {
99-
securityContextRepository = new HttpSessionSecurityContextRepository();
101+
securityContextRepository = new DelegatingSecurityContextRepository(
102+
new RequestAttributeSecurityContextRepository(), new HttpSessionSecurityContextRepository());
100103
}
101104
return securityContextRepository;
102105
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright 2002-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.security.config;
18+
19+
import org.springframework.security.core.context.DeferredSecurityContext;
20+
import org.springframework.security.core.context.SecurityContext;
21+
22+
/**
23+
* @author Steve Riesenberg
24+
*/
25+
public class TestDeferredSecurityContext implements DeferredSecurityContext {
26+
27+
private SecurityContext securityContext;
28+
29+
private boolean isGenerated;
30+
31+
public TestDeferredSecurityContext(SecurityContext securityContext, boolean isGenerated) {
32+
this.securityContext = securityContext;
33+
this.isGenerated = isGenerated;
34+
}
35+
36+
@Override
37+
public SecurityContext get() {
38+
return this.securityContext;
39+
}
40+
41+
@Override
42+
public boolean isGenerated() {
43+
return this.isGenerated;
44+
}
45+
46+
}

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

+4-3
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.springframework.beans.factory.annotation.Autowired;
2929
import org.springframework.context.annotation.Bean;
3030
import org.springframework.context.annotation.Configuration;
31+
import org.springframework.security.config.TestDeferredSecurityContext;
3132
import org.springframework.security.config.annotation.ObjectPostProcessor;
3233
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
3334
import org.springframework.security.config.annotation.web.builders.TestHttpSecurity;
@@ -83,10 +84,10 @@ public void configureWhenRegisteringObjectPostProcessorThenInvokedOnSecurityCont
8384
@Test
8485
public void securityContextWhenInvokedTwiceThenUsesOriginalSecurityContextRepository() throws Exception {
8586
this.spring.register(DuplicateDoesNotOverrideConfig.class).autowire();
86-
given(DuplicateDoesNotOverrideConfig.SCR.loadContext(any(HttpServletRequest.class)))
87-
.willReturn(() -> mock(SecurityContext.class));
87+
given(DuplicateDoesNotOverrideConfig.SCR.loadDeferredContext(any(HttpServletRequest.class)))
88+
.willReturn(new TestDeferredSecurityContext(mock(SecurityContext.class), false));
8889
this.mvc.perform(get("/"));
89-
verify(DuplicateDoesNotOverrideConfig.SCR).loadContext(any(HttpServletRequest.class));
90+
verify(DuplicateDoesNotOverrideConfig.SCR).loadDeferredContext(any(HttpServletRequest.class));
9091
}
9192

9293
// SEC-2932

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.springframework.context.annotation.Configuration;
2828
import org.springframework.mock.web.MockHttpSession;
2929
import org.springframework.security.authentication.AuthenticationTrustResolver;
30+
import org.springframework.security.config.TestDeferredSecurityContext;
3031
import org.springframework.security.config.annotation.ObjectPostProcessor;
3132
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
3233
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
@@ -103,7 +104,8 @@ public void sessionManagementWhenConfiguredThenDoesNotOverrideRequestCache() thr
103104
public void sessionManagementWhenConfiguredThenDoesNotOverrideSecurityContextRepository() throws Exception {
104105
SessionManagementSecurityContextRepositoryConfig.SECURITY_CONTEXT_REPO = mock(SecurityContextRepository.class);
105106
given(SessionManagementSecurityContextRepositoryConfig.SECURITY_CONTEXT_REPO
106-
.loadContext(any(HttpServletRequest.class))).willReturn(() -> mock(SecurityContext.class));
107+
.loadDeferredContext(any(HttpServletRequest.class)))
108+
.willReturn(new TestDeferredSecurityContext(mock(SecurityContext.class), false));
107109
this.spring.register(SessionManagementSecurityContextRepositoryConfig.class).autowire();
108110
this.mvc.perform(get("/"));
109111
}

config/src/test/java/org/springframework/security/config/http/MiscHttpConfigTests.java

+3-25
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,14 @@
6868
import org.springframework.security.authentication.InsufficientAuthenticationException;
6969
import org.springframework.security.authentication.TestingAuthenticationToken;
7070
import org.springframework.security.authentication.jaas.AuthorityGranter;
71+
import org.springframework.security.config.TestDeferredSecurityContext;
7172
import org.springframework.security.config.test.SpringTestContext;
7273
import org.springframework.security.config.test.SpringTestContextExtension;
7374
import org.springframework.security.core.Authentication;
7475
import org.springframework.security.core.AuthenticationException;
7576
import org.springframework.security.core.GrantedAuthority;
7677
import org.springframework.security.core.annotation.AuthenticationPrincipal;
7778
import org.springframework.security.core.authority.AuthorityUtils;
78-
import org.springframework.security.core.context.DeferredSecurityContext;
7979
import org.springframework.security.core.context.SecurityContext;
8080
import org.springframework.security.core.context.SecurityContextHolder;
8181
import org.springframework.security.core.context.SecurityContextHolderStrategy;
@@ -473,7 +473,8 @@ public void getWhenAuthenticatingThenConsultsCustomSecurityContextRepository() t
473473
this.spring.configLocations(xml("SecurityContextRepository")).autowire();
474474
SecurityContextRepository repository = this.spring.getContext().getBean(SecurityContextRepository.class);
475475
SecurityContext context = new SecurityContextImpl(new TestingAuthenticationToken("user", "password"));
476-
given(repository.loadContext(any(HttpServletRequest.class))).willReturn(() -> context);
476+
given(repository.loadDeferredContext(any(HttpServletRequest.class)))
477+
.willReturn(new TestDeferredSecurityContext(context, false));
477478
// @formatter:off
478479
MvcResult result = this.mvc.perform(get("/protected").with(userCredentials()))
479480
.andExpect(status().isOk())
@@ -1039,27 +1040,4 @@ public String encodeRedirectUrl(String url) {
10391040

10401041
}
10411042

1042-
static class TestDeferredSecurityContext implements DeferredSecurityContext {
1043-
1044-
private SecurityContext securityContext;
1045-
1046-
private boolean isGenerated;
1047-
1048-
TestDeferredSecurityContext(SecurityContext securityContext, boolean isGenerated) {
1049-
this.securityContext = securityContext;
1050-
this.isGenerated = isGenerated;
1051-
}
1052-
1053-
@Override
1054-
public SecurityContext get() {
1055-
return this.securityContext;
1056-
}
1057-
1058-
@Override
1059-
public boolean isGenerated() {
1060-
return this.isGenerated;
1061-
}
1062-
1063-
}
1064-
10651043
}

0 commit comments

Comments
 (0)