Skip to content

Add disable DSL for RequestCache #17506

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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<HttpSecurity>) -> Unit {
return { requestCacheConfig ->
requestCache?.also {
requestCacheConfig.requestCache(requestCache)
}
if (disabled) {
requestCacheConfig.disable()
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down