Skip to content

Commit 13e6511

Browse files
author
Steve Riesenberg
committed
Add DelegatingSecurityContextRepository
Issue spring-projectsgh-12023
1 parent 56b9bad commit 13e6511

File tree

2 files changed

+108
-0
lines changed

2 files changed

+108
-0
lines changed

core/src/main/java/org/springframework/security/core/context/SecurityContext.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,12 @@ public interface SecurityContext extends Serializable {
4747
*/
4848
void setAuthentication(Authentication authentication);
4949

50+
/**
51+
* TODO
52+
* @return
53+
*/
54+
default boolean isGenerated() {
55+
return false;
56+
}
57+
5058
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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.Arrays;
20+
import java.util.List;
21+
import java.util.function.Supplier;
22+
23+
import javax.servlet.http.HttpServletRequest;
24+
import javax.servlet.http.HttpServletResponse;
25+
26+
import org.springframework.security.core.context.SecurityContext;
27+
28+
/**
29+
* @author Steve Riesenberg
30+
* @since 5.8
31+
*/
32+
class DelegatingSecurityContextRepository implements SecurityContextRepository {
33+
34+
private final List<SecurityContextRepository> delegates;
35+
36+
public DelegatingSecurityContextRepository(SecurityContextRepository... delegates) {
37+
this(Arrays.asList(delegates));
38+
}
39+
40+
public DelegatingSecurityContextRepository(List<SecurityContextRepository> delegates) {
41+
this.delegates = delegates;
42+
}
43+
44+
@Override
45+
public SecurityContext loadContext(HttpRequestResponseHolder requestResponseHolder) {
46+
return loadContext(requestResponseHolder.getRequest()).get();
47+
}
48+
49+
@Override
50+
public Supplier<SecurityContext> loadContext(HttpServletRequest request) {
51+
Supplier<SecurityContext> supplier = null;
52+
for (SecurityContextRepository delegate : this.delegates) {
53+
if (supplier == null) {
54+
supplier = delegate.loadContext(request);
55+
}
56+
else {
57+
Supplier<SecurityContext> current = delegate.loadContext(request);
58+
supplier = new DelegatingSupplier(current, supplier);
59+
}
60+
}
61+
return supplier;
62+
}
63+
64+
@Override
65+
public void saveContext(SecurityContext context, HttpServletRequest request, HttpServletResponse response) {
66+
throw new UnsupportedOperationException("saveContext() should be called directly on the desired SecurityContextRepository.");
67+
}
68+
69+
@Override
70+
public boolean containsContext(HttpServletRequest request) {
71+
for (SecurityContextRepository delegate : this.delegates) {
72+
if (delegate.containsContext(request)) {
73+
return true;
74+
}
75+
}
76+
return false;
77+
}
78+
79+
static class DelegatingSupplier implements Supplier<SecurityContext> {
80+
81+
private final Supplier<SecurityContext> current;
82+
83+
private final Supplier<SecurityContext> previous;
84+
85+
DelegatingSupplier(Supplier<SecurityContext> current, Supplier<SecurityContext> previous) {
86+
this.current = current;
87+
this.previous = previous;
88+
}
89+
90+
@Override
91+
public SecurityContext get() {
92+
SecurityContext securityContext = this.previous.get();
93+
if (!securityContext.isGenerated()) {
94+
return securityContext;
95+
}
96+
return this.current.get();
97+
}
98+
}
99+
100+
}

0 commit comments

Comments
 (0)