Skip to content

Commit 177514b

Browse files
Merge branch '5.8.x' into 6.0.x
Closes gh-12919
2 parents cbb4e40 + 8d664bc commit 177514b

File tree

2 files changed

+70
-3
lines changed

2 files changed

+70
-3
lines changed

web/src/main/java/org/springframework/security/web/context/DelegatingSecurityContextRepository.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 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.
@@ -46,7 +46,14 @@ public DelegatingSecurityContextRepository(List<SecurityContextRepository> deleg
4646

4747
@Override
4848
public SecurityContext loadContext(HttpRequestResponseHolder requestResponseHolder) {
49-
return loadDeferredContext(requestResponseHolder.getRequest()).get();
49+
SecurityContext result = null;
50+
for (SecurityContextRepository delegate : this.delegates) {
51+
SecurityContext delegateResult = delegate.loadContext(requestResponseHolder);
52+
if (result == null || delegate.containsContext(requestResponseHolder.getRequest())) {
53+
result = delegateResult;
54+
}
55+
}
56+
return result;
5057
}
5158

5259
@Override

web/src/test/java/org/springframework/security/web/context/DelegatingSecurityContextRepositoryTests.java

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 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.
@@ -141,4 +141,64 @@ public void containsContextWhenFirstDelegatesReturnTrueThenReturnsTrue() {
141141
verifyNoInteractions(delegates.get(2));
142142
}
143143

144+
// gh-12314
145+
@Test
146+
public void loadContextWhenSecondDelegateReturnsThenContextFromSecondDelegate() {
147+
SecurityContextRepository delegate1 = mock(SecurityContextRepository.class);
148+
SecurityContextRepository delegate2 = mock(SecurityContextRepository.class);
149+
HttpRequestResponseHolder holder = new HttpRequestResponseHolder(this.request, this.response);
150+
SecurityContext securityContext1 = mock(SecurityContext.class);
151+
SecurityContext securityContext2 = mock(SecurityContext.class);
152+
153+
given(delegate1.loadContext(holder)).willReturn(securityContext1);
154+
given(delegate1.containsContext(holder.getRequest())).willReturn(false);
155+
given(delegate2.loadContext(holder)).willReturn(securityContext2);
156+
given(delegate2.containsContext(holder.getRequest())).willReturn(true);
157+
158+
DelegatingSecurityContextRepository repository = new DelegatingSecurityContextRepository(delegate1, delegate2);
159+
SecurityContext returnedSecurityContext = repository.loadContext(holder);
160+
161+
assertThat(returnedSecurityContext).isSameAs(securityContext2);
162+
}
163+
164+
// gh-12314
165+
@Test
166+
public void loadContextWhenBothDelegateReturnsThenContextFromSecondDelegate() {
167+
SecurityContextRepository delegate1 = mock(SecurityContextRepository.class);
168+
SecurityContextRepository delegate2 = mock(SecurityContextRepository.class);
169+
HttpRequestResponseHolder holder = new HttpRequestResponseHolder(this.request, this.response);
170+
SecurityContext securityContext1 = mock(SecurityContext.class);
171+
SecurityContext securityContext2 = mock(SecurityContext.class);
172+
173+
given(delegate1.loadContext(holder)).willReturn(securityContext1);
174+
given(delegate1.containsContext(holder.getRequest())).willReturn(true);
175+
given(delegate2.loadContext(holder)).willReturn(securityContext2);
176+
given(delegate2.containsContext(holder.getRequest())).willReturn(true);
177+
178+
DelegatingSecurityContextRepository repository = new DelegatingSecurityContextRepository(delegate1, delegate2);
179+
SecurityContext returnedSecurityContext = repository.loadContext(holder);
180+
181+
assertThat(returnedSecurityContext).isSameAs(securityContext2);
182+
}
183+
184+
// gh-12314
185+
@Test
186+
public void loadContextWhenFirstDelegateReturnsThenContextFromFirstDelegate() {
187+
SecurityContextRepository delegate1 = mock(SecurityContextRepository.class);
188+
SecurityContextRepository delegate2 = mock(SecurityContextRepository.class);
189+
HttpRequestResponseHolder holder = new HttpRequestResponseHolder(this.request, this.response);
190+
SecurityContext securityContext1 = mock(SecurityContext.class);
191+
SecurityContext securityContext2 = mock(SecurityContext.class);
192+
193+
given(delegate1.loadContext(holder)).willReturn(securityContext1);
194+
given(delegate1.containsContext(holder.getRequest())).willReturn(true);
195+
given(delegate2.loadContext(holder)).willReturn(securityContext2);
196+
given(delegate2.containsContext(holder.getRequest())).willReturn(false);
197+
198+
DelegatingSecurityContextRepository repository = new DelegatingSecurityContextRepository(delegate1, delegate2);
199+
SecurityContext returnedSecurityContext = repository.loadContext(holder);
200+
201+
assertThat(returnedSecurityContext).isSameAs(securityContext1);
202+
}
203+
144204
}

0 commit comments

Comments
 (0)