From 57b163bb78ec0aea4116f0b22702417684263c47 Mon Sep 17 00:00:00 2001 From: Steve Riesenberg Date: Wed, 9 Nov 2022 12:19:12 -0600 Subject: [PATCH 1/2] Polish gh-12141 --- .../security/web/csrf/CookieCsrfTokenRepository.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/web/src/main/java/org/springframework/security/web/csrf/CookieCsrfTokenRepository.java b/web/src/main/java/org/springframework/security/web/csrf/CookieCsrfTokenRepository.java index 40bccc5ca39..1cb38a6a93b 100644 --- a/web/src/main/java/org/springframework/security/web/csrf/CookieCsrfTokenRepository.java +++ b/web/src/main/java/org/springframework/security/web/csrf/CookieCsrfTokenRepository.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2016 the original author or authors. + * Copyright 2012-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -33,6 +33,7 @@ * AngularJS. When using with AngularJS be sure to use {@link #withHttpOnlyFalse()}. * * @author Rob Winch + * @author Steve Riesenberg * @since 4.1 */ public final class CookieCsrfTokenRepository implements CsrfTokenRepository { From 9071f1075981a24ffd3da255f8ad93505996263d Mon Sep 17 00:00:00 2001 From: Steve Riesenberg Date: Wed, 9 Nov 2022 11:46:51 -0600 Subject: [PATCH 2/2] Document DelegatingSecurityContextRepository Closes gh-12069 --- .../servlet/authentication/persistence.adoc | 66 +++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/docs/modules/ROOT/pages/servlet/authentication/persistence.adoc b/docs/modules/ROOT/pages/servlet/authentication/persistence.adoc index 3f17e9b39a1..441f6a528f8 100644 --- a/docs/modules/ROOT/pages/servlet/authentication/persistence.adoc +++ b/docs/modules/ROOT/pages/servlet/authentication/persistence.adoc @@ -114,6 +114,72 @@ public SecurityFilterChain filterChain(HttpSecurity http) { ---- ==== +[[delegatingsecuritycontextrepository]] +=== DelegatingSecurityContextRepository + +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. + +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. + +.Configure DelegatingSecurityContextRepository +==== +.Java +[source,java,role="primary"] +---- +@Bean +public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { + http + // ... + .securityContext((securityContext) -> securityContext + .securityContextRepository(new DelegatingSecurityContextRepository( + new RequestAttributeSecurityContextRepository(), + new HttpSessionSecurityContextRepository() + )) + ); + return http.build(); +} +---- + +.Kotlin +[source,kotlin,role="secondary"] +---- +@Bean +fun securityFilterChain(http: HttpSecurity): SecurityFilterChain { + http { + // ... + securityContext { + securityContextRepository = DelegatingSecurityContextRepository( + RequestAttributeSecurityContextRepository(), + HttpSessionSecurityContextRepository() + ) + } + } + return http.build() +} +---- + +.XML +[source,xml,role="secondary"] +---- + + + + + + + + + + + +---- +==== + +[NOTE] +==== +In Spring Security 6, the example shown above is the default configuration. +==== [[securitycontextpersistencefilter]] == SecurityContextPersistenceFilter