Skip to content

Commit e94aded

Browse files
Add shouldFilterAllDispatcherTypes to Kotlin DSL
Closes gh-11153
1 parent 8e34ced commit e94aded

File tree

3 files changed

+174
-0
lines changed

3 files changed

+174
-0
lines changed

config/src/main/kotlin/org/springframework/security/config/web/servlet/AuthorizeHttpRequestsDsl.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import org.springframework.security.authorization.AuthorizationManager
2424
import org.springframework.security.config.annotation.web.builders.HttpSecurity
2525
import org.springframework.security.config.annotation.web.configurers.AuthorizeHttpRequestsConfigurer
2626
import org.springframework.security.core.Authentication
27+
import org.springframework.security.web.access.intercept.AuthorizationFilter
2728
import org.springframework.security.web.access.intercept.RequestAuthorizationContext
2829
import org.springframework.security.web.util.matcher.AnyRequestMatcher
2930
import org.springframework.security.web.util.matcher.RequestMatcher
@@ -35,8 +36,11 @@ import java.util.function.Supplier
3536
*
3637
* @author Yuriy Savchenko
3738
* @since 5.7
39+
* @property shouldFilterAllDispatcherTypes whether the [AuthorizationFilter] should filter all dispatcher types
3840
*/
3941
class AuthorizeHttpRequestsDsl : AbstractRequestMatcherDsl() {
42+
var shouldFilterAllDispatcherTypes: Boolean? = null
43+
4044
private val authorizationRules = mutableListOf<AuthorizationManagerRule>()
4145

4246
private val HANDLER_MAPPING_INTROSPECTOR = "org.springframework.web.servlet.handler.HandlerMappingIntrospector"
@@ -248,6 +252,9 @@ class AuthorizeHttpRequestsDsl : AbstractRequestMatcherDsl() {
248252
}
249253
}
250254
}
255+
shouldFilterAllDispatcherTypes?.also { shouldFilter ->
256+
requests.shouldFilterAllDispatcherTypes(shouldFilter)
257+
}
251258
}
252259
}
253260
}

config/src/test/kotlin/org/springframework/security/config/web/servlet/AuthorizeHttpRequestsDslTests.kt

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@ import org.springframework.web.bind.annotation.RestController
5353
import org.springframework.web.servlet.config.annotation.EnableWebMvc
5454
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer
5555
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer
56+
import org.springframework.web.util.WebUtils
5657
import java.util.function.Supplier
58+
import javax.servlet.DispatcherType
5759

5860
/**
5961
* Tests for [AuthorizeHttpRequestsDsl]
@@ -641,4 +643,155 @@ class AuthorizeHttpRequestsDslTests {
641643
return http.build()
642644
}
643645
}
646+
647+
@Test
648+
fun `request when shouldFilterAllDispatcherTypes and denyAll and ERROR then responds with forbidden`() {
649+
this.spring.register(ShouldFilterAllDispatcherTypesTrueDenyAllConfig::class.java).autowire()
650+
651+
this.mockMvc.perform(get("/path")
652+
.with { request ->
653+
request.setAttribute(WebUtils.ERROR_REQUEST_URI_ATTRIBUTE, "/error")
654+
request.apply {
655+
dispatcherType = DispatcherType.ERROR
656+
}
657+
})
658+
.andExpect(status().isForbidden)
659+
}
660+
661+
@EnableWebSecurity
662+
@EnableWebMvc
663+
open class ShouldFilterAllDispatcherTypesTrueDenyAllConfig {
664+
665+
@Bean
666+
open fun securityFilterChain(http: HttpSecurity): SecurityFilterChain {
667+
http {
668+
authorizeHttpRequests {
669+
shouldFilterAllDispatcherTypes = true
670+
authorize(anyRequest, denyAll)
671+
}
672+
}
673+
return http.build()
674+
}
675+
676+
@RestController
677+
internal class PathController {
678+
@RequestMapping("/path")
679+
fun path() {
680+
}
681+
}
682+
683+
}
684+
685+
@Test
686+
fun `request when shouldFilterAllDispatcherTypes and permitAll and ERROR then responds with ok`() {
687+
this.spring.register(ShouldFilterAllDispatcherTypesTruePermitAllConfig::class.java).autowire()
688+
689+
this.mockMvc.perform(get("/path")
690+
.with { request ->
691+
request.setAttribute(WebUtils.ERROR_REQUEST_URI_ATTRIBUTE, "/error")
692+
request.apply {
693+
dispatcherType = DispatcherType.ERROR
694+
}
695+
})
696+
.andExpect(status().isOk)
697+
}
698+
699+
@EnableWebSecurity
700+
@EnableWebMvc
701+
open class ShouldFilterAllDispatcherTypesTruePermitAllConfig {
702+
703+
@Bean
704+
open fun securityFilterChain(http: HttpSecurity): SecurityFilterChain {
705+
http {
706+
authorizeHttpRequests {
707+
shouldFilterAllDispatcherTypes = true
708+
authorize(anyRequest, permitAll)
709+
}
710+
}
711+
return http.build()
712+
}
713+
714+
@RestController
715+
internal class PathController {
716+
@RequestMapping("/path")
717+
fun path() {
718+
}
719+
}
720+
721+
}
722+
723+
@Test
724+
fun `request when shouldFilterAllDispatcherTypes false and ERROR dispatcher then responds with ok`() {
725+
this.spring.register(ShouldFilterAllDispatcherTypesFalseAndDenyAllConfig::class.java).autowire()
726+
727+
this.mockMvc.perform(get("/path")
728+
.with { request ->
729+
request.setAttribute(WebUtils.ERROR_REQUEST_URI_ATTRIBUTE, "/error")
730+
request.apply {
731+
dispatcherType = DispatcherType.ERROR
732+
}
733+
})
734+
.andExpect(status().isOk)
735+
}
736+
737+
@EnableWebSecurity
738+
@EnableWebMvc
739+
open class ShouldFilterAllDispatcherTypesFalseAndDenyAllConfig {
740+
741+
@Bean
742+
open fun securityFilterChain(http: HttpSecurity): SecurityFilterChain {
743+
http {
744+
authorizeHttpRequests {
745+
shouldFilterAllDispatcherTypes = false
746+
authorize(anyRequest, denyAll)
747+
}
748+
}
749+
return http.build()
750+
}
751+
752+
@RestController
753+
internal class PathController {
754+
@RequestMapping("/path")
755+
fun path() {
756+
}
757+
}
758+
759+
}
760+
761+
@Test
762+
fun `request when shouldFilterAllDispatcherTypes omitted and ERROR dispatcher then responds with ok`() {
763+
this.spring.register(ShouldFilterAllDispatcherTypesOmittedAndDenyAllConfig::class.java).autowire()
764+
765+
this.mockMvc.perform(get("/path")
766+
.with { request ->
767+
request.setAttribute(WebUtils.ERROR_REQUEST_URI_ATTRIBUTE, "/error")
768+
request.apply {
769+
dispatcherType = DispatcherType.ERROR
770+
}
771+
})
772+
.andExpect(status().isOk)
773+
}
774+
775+
@EnableWebSecurity
776+
@EnableWebMvc
777+
open class ShouldFilterAllDispatcherTypesOmittedAndDenyAllConfig {
778+
779+
@Bean
780+
open fun securityFilterChain(http: HttpSecurity): SecurityFilterChain {
781+
http {
782+
authorizeHttpRequests {
783+
authorize(anyRequest, denyAll)
784+
}
785+
}
786+
return http.build()
787+
}
788+
789+
@RestController
790+
internal class PathController {
791+
@RequestMapping("/path")
792+
fun path() {
793+
}
794+
}
795+
796+
}
644797
}

docs/modules/ROOT/pages/servlet/authorization/authorize-http-requests.adoc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,4 +190,18 @@ SecurityFilterChain web(HttpSecurity http) throws Exception {
190190
return http.build();
191191
}
192192
----
193+
.Kotlin
194+
[source,kotlin,role="secondary"]
195+
----
196+
@Bean
197+
open fun web(http: HttpSecurity): SecurityFilterChain {
198+
http {
199+
authorizeHttpRequests {
200+
shouldFilterAllDispatcherTypes = true
201+
authorize(anyRequest, authenticated)
202+
}
203+
}
204+
return http.build()
205+
}
206+
----
193207
====

0 commit comments

Comments
 (0)