From 6a2e3447697a4e9c7b668c0be71994094e9d9736 Mon Sep 17 00:00:00 2001 From: kimjongil Date: Thu, 10 Jul 2025 18:38:45 +0900 Subject: [PATCH] Add disable DSL for RequestCache --- .../config/annotation/web/RequestCacheDsl.kt | 12 +++++++++ .../annotation/web/RequestCacheDslTests.kt | 27 +++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/config/src/main/kotlin/org/springframework/security/config/annotation/web/RequestCacheDsl.kt b/config/src/main/kotlin/org/springframework/security/config/annotation/web/RequestCacheDsl.kt index 738e0a06132..db0d97b9b3d 100644 --- a/config/src/main/kotlin/org/springframework/security/config/annotation/web/RequestCacheDsl.kt +++ b/config/src/main/kotlin/org/springframework/security/config/annotation/web/RequestCacheDsl.kt @@ -32,11 +32,23 @@ import org.springframework.security.web.savedrequest.RequestCache class RequestCacheDsl { var requestCache: RequestCache? = null + private var disabled = false + + /** + * Disables the request cache. + */ + fun disable() { + disabled = true + } + internal fun get(): (RequestCacheConfigurer) -> Unit { return { requestCacheConfig -> requestCache?.also { requestCacheConfig.requestCache(requestCache) } + if (disabled) { + requestCacheConfig.disable() + } } } } diff --git a/config/src/test/kotlin/org/springframework/security/config/annotation/web/RequestCacheDslTests.kt b/config/src/test/kotlin/org/springframework/security/config/annotation/web/RequestCacheDslTests.kt index 0239b386fbe..e13cc0d68ff 100644 --- a/config/src/test/kotlin/org/springframework/security/config/annotation/web/RequestCacheDslTests.kt +++ b/config/src/test/kotlin/org/springframework/security/config/annotation/web/RequestCacheDslTests.kt @@ -70,6 +70,33 @@ class RequestCacheDslTests { } } + @Test + fun `GET when request cache disabled then redirected to index page`() { + this.spring.register(RequestCacheDisabledConfig::class.java).autowire() + + this.mockMvc.get("/test") + + this.mockMvc.perform(formLogin()) + .andExpect { + redirectedUrl("/") + } + } + + @Configuration + @EnableWebSecurity + open class RequestCacheDisabledConfig { + @Bean + open fun securityFilterChain(http: HttpSecurity): SecurityFilterChain { + http { + requestCache { + disable() + } + formLogin { } + } + return http.build() + } + } + @Test fun `GET when custom request cache then custom request cache used`() { this.spring.register(CustomRequestCacheConfig::class.java).autowire()