Skip to content

Commit 1a3be83

Browse files
author
Steve Riesenberg
committed
Merge branch '5.8.x'
Closes gh-12185
2 parents d72e2ab + 9071f10 commit 1a3be83

File tree

2 files changed

+68
-1
lines changed

2 files changed

+68
-1
lines changed

docs/modules/ROOT/pages/servlet/authentication/persistence.adoc

+66
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,72 @@ public SecurityFilterChain filterChain(HttpSecurity http) {
114114
----
115115
====
116116

117+
[[delegatingsecuritycontextrepository]]
118+
=== DelegatingSecurityContextRepository
119+
120+
The {security-api-url}org/springframework/security/web/context/DelegatingSecurityContextRepository.html[`DelegatingSecurityContextRepository`] saves the `SecurityContext` to multiple `SecurityContextRepository` delegates and allows retrieval from any of the delegates in a specified order.
121+
122+
The most useful arrangement for this is configured with the following example, which allows the use of both xref:requestattributesecuritycontextrepository[`RequestAttributeSecurityContextRepository`] and xref:httpsecuritycontextrepository[`HttpSessionSecurityContextRepository`] simultaneously.
123+
124+
.Configure DelegatingSecurityContextRepository
125+
====
126+
.Java
127+
[source,java,role="primary"]
128+
----
129+
@Bean
130+
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
131+
http
132+
// ...
133+
.securityContext((securityContext) -> securityContext
134+
.securityContextRepository(new DelegatingSecurityContextRepository(
135+
new RequestAttributeSecurityContextRepository(),
136+
new HttpSessionSecurityContextRepository()
137+
))
138+
);
139+
return http.build();
140+
}
141+
----
142+
143+
.Kotlin
144+
[source,kotlin,role="secondary"]
145+
----
146+
@Bean
147+
fun securityFilterChain(http: HttpSecurity): SecurityFilterChain {
148+
http {
149+
// ...
150+
securityContext {
151+
securityContextRepository = DelegatingSecurityContextRepository(
152+
RequestAttributeSecurityContextRepository(),
153+
HttpSessionSecurityContextRepository()
154+
)
155+
}
156+
}
157+
return http.build()
158+
}
159+
----
160+
161+
.XML
162+
[source,xml,role="secondary"]
163+
----
164+
<http security-context-repository-ref="contextRepository">
165+
<!-- ... -->
166+
</http>
167+
<bean name="contextRepository"
168+
class="org.springframework.security.web.context.DelegatingSecurityContextRepository">
169+
<constructor-arg>
170+
<bean class="org.springframework.security.web.context.RequestAttributeSecurityContextRepository" />
171+
</constructor-arg>
172+
<constructor-arg>
173+
<bean class="org.springframework.security.web.context.HttpSessionSecurityContextRepository" />
174+
</constructor-arg>
175+
</bean>
176+
----
177+
====
178+
179+
[NOTE]
180+
====
181+
In Spring Security 6, the example shown above is the default configuration.
182+
====
117183

118184
[[securitycontextpersistencefilter]]
119185
== SecurityContextPersistenceFilter

web/src/main/java/org/springframework/security/web/csrf/CookieCsrfTokenRepository.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2016 the original author or authors.
2+
* Copyright 2012-2022 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
* AngularJS. When using with AngularJS be sure to use {@link #withHttpOnlyFalse()}.
3434
*
3535
* @author Rob Winch
36+
* @author Steve Riesenberg
3637
* @since 4.1
3738
*/
3839
public final class CookieCsrfTokenRepository implements CsrfTokenRepository {

0 commit comments

Comments
 (0)