Add shouldFilterAllDispatcherTypes to Kotlin DSL

Closes gh-11153
This commit is contained in:
Marcus Da Coregio
2022-04-25 14:20:56 -03:00
committed by Marcus Hert Da Coregio
parent 8e34cedcfe
commit e94adedb94
3 changed files with 174 additions and 0 deletions

View File

@@ -53,7 +53,9 @@ import org.springframework.web.bind.annotation.RestController
import org.springframework.web.servlet.config.annotation.EnableWebMvc
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer
import org.springframework.web.util.WebUtils
import java.util.function.Supplier
import javax.servlet.DispatcherType
/**
* Tests for [AuthorizeHttpRequestsDsl]
@@ -641,4 +643,155 @@ class AuthorizeHttpRequestsDslTests {
return http.build()
}
}
@Test
fun `request when shouldFilterAllDispatcherTypes and denyAll and ERROR then responds with forbidden`() {
this.spring.register(ShouldFilterAllDispatcherTypesTrueDenyAllConfig::class.java).autowire()
this.mockMvc.perform(get("/path")
.with { request ->
request.setAttribute(WebUtils.ERROR_REQUEST_URI_ATTRIBUTE, "/error")
request.apply {
dispatcherType = DispatcherType.ERROR
}
})
.andExpect(status().isForbidden)
}
@EnableWebSecurity
@EnableWebMvc
open class ShouldFilterAllDispatcherTypesTrueDenyAllConfig {
@Bean
open fun securityFilterChain(http: HttpSecurity): SecurityFilterChain {
http {
authorizeHttpRequests {
shouldFilterAllDispatcherTypes = true
authorize(anyRequest, denyAll)
}
}
return http.build()
}
@RestController
internal class PathController {
@RequestMapping("/path")
fun path() {
}
}
}
@Test
fun `request when shouldFilterAllDispatcherTypes and permitAll and ERROR then responds with ok`() {
this.spring.register(ShouldFilterAllDispatcherTypesTruePermitAllConfig::class.java).autowire()
this.mockMvc.perform(get("/path")
.with { request ->
request.setAttribute(WebUtils.ERROR_REQUEST_URI_ATTRIBUTE, "/error")
request.apply {
dispatcherType = DispatcherType.ERROR
}
})
.andExpect(status().isOk)
}
@EnableWebSecurity
@EnableWebMvc
open class ShouldFilterAllDispatcherTypesTruePermitAllConfig {
@Bean
open fun securityFilterChain(http: HttpSecurity): SecurityFilterChain {
http {
authorizeHttpRequests {
shouldFilterAllDispatcherTypes = true
authorize(anyRequest, permitAll)
}
}
return http.build()
}
@RestController
internal class PathController {
@RequestMapping("/path")
fun path() {
}
}
}
@Test
fun `request when shouldFilterAllDispatcherTypes false and ERROR dispatcher then responds with ok`() {
this.spring.register(ShouldFilterAllDispatcherTypesFalseAndDenyAllConfig::class.java).autowire()
this.mockMvc.perform(get("/path")
.with { request ->
request.setAttribute(WebUtils.ERROR_REQUEST_URI_ATTRIBUTE, "/error")
request.apply {
dispatcherType = DispatcherType.ERROR
}
})
.andExpect(status().isOk)
}
@EnableWebSecurity
@EnableWebMvc
open class ShouldFilterAllDispatcherTypesFalseAndDenyAllConfig {
@Bean
open fun securityFilterChain(http: HttpSecurity): SecurityFilterChain {
http {
authorizeHttpRequests {
shouldFilterAllDispatcherTypes = false
authorize(anyRequest, denyAll)
}
}
return http.build()
}
@RestController
internal class PathController {
@RequestMapping("/path")
fun path() {
}
}
}
@Test
fun `request when shouldFilterAllDispatcherTypes omitted and ERROR dispatcher then responds with ok`() {
this.spring.register(ShouldFilterAllDispatcherTypesOmittedAndDenyAllConfig::class.java).autowire()
this.mockMvc.perform(get("/path")
.with { request ->
request.setAttribute(WebUtils.ERROR_REQUEST_URI_ATTRIBUTE, "/error")
request.apply {
dispatcherType = DispatcherType.ERROR
}
})
.andExpect(status().isOk)
}
@EnableWebSecurity
@EnableWebMvc
open class ShouldFilterAllDispatcherTypesOmittedAndDenyAllConfig {
@Bean
open fun securityFilterChain(http: HttpSecurity): SecurityFilterChain {
http {
authorizeHttpRequests {
authorize(anyRequest, denyAll)
}
}
return http.build()
}
@RestController
internal class PathController {
@RequestMapping("/path")
fun path() {
}
}
}
}