Skip to content

Commit 29db051

Browse files
committed
Cache SecurityContextRepository.loadContext(HttpServletRequest) Result
Closes gh-11390
1 parent f035c30 commit 29db051

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import javax.servlet.http.HttpServletResponse;
2323

2424
import org.springframework.security.core.context.SecurityContext;
25+
import org.springframework.util.function.SingletonSupplier;
2526

2627
/**
2728
* Strategy used for persisting a {@link SecurityContext} between requests.
@@ -76,7 +77,7 @@ public interface SecurityContextRepository {
7677
* @since 5.7
7778
*/
7879
default Supplier<SecurityContext> loadContext(HttpServletRequest request) {
79-
return () -> loadContext(new HttpRequestResponseHolder(request, null));
80+
return SingletonSupplier.of(() -> loadContext(new HttpRequestResponseHolder(request, null)));
8081
}
8182

8283
/**
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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.web.context;
18+
19+
import java.util.function.Supplier;
20+
21+
import javax.servlet.http.HttpServletRequest;
22+
23+
import org.junit.jupiter.api.Test;
24+
25+
import org.springframework.security.core.context.SecurityContext;
26+
import org.springframework.security.core.context.SecurityContextImpl;
27+
28+
import static org.mockito.ArgumentMatchers.any;
29+
import static org.mockito.BDDMockito.given;
30+
import static org.mockito.Mockito.mock;
31+
import static org.mockito.Mockito.spy;
32+
import static org.mockito.Mockito.verify;
33+
import static org.mockito.Mockito.verifyNoMoreInteractions;
34+
35+
/**
36+
* @author Rob Winch
37+
*/
38+
class SecurityContextRepositoryTests {
39+
40+
SecurityContextRepository repository = spy(SecurityContextRepository.class);
41+
42+
@Test
43+
void loadContextHttpRequestResponseHolderWhenInvokeSupplierTwiceThenOnlyInvokesLoadContextOnce() {
44+
given(this.repository.loadContext(any(HttpRequestResponseHolder.class))).willReturn(new SecurityContextImpl());
45+
Supplier<SecurityContext> deferredContext = this.repository.loadContext(mock(HttpServletRequest.class));
46+
verify(this.repository).loadContext(any(HttpServletRequest.class));
47+
deferredContext.get();
48+
verify(this.repository).loadContext(any(HttpRequestResponseHolder.class));
49+
deferredContext.get();
50+
verifyNoMoreInteractions(this.repository);
51+
}
52+
53+
}

0 commit comments

Comments
 (0)