@@ -53,7 +53,9 @@ import org.springframework.web.bind.annotation.RestController
53
53
import org.springframework.web.servlet.config.annotation.EnableWebMvc
54
54
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer
55
55
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer
56
+ import org.springframework.web.util.WebUtils
56
57
import java.util.function.Supplier
58
+ import javax.servlet.DispatcherType
57
59
58
60
/* *
59
61
* Tests for [AuthorizeHttpRequestsDsl]
@@ -641,4 +643,155 @@ class AuthorizeHttpRequestsDslTests {
641
643
return http.build()
642
644
}
643
645
}
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
+ }
644
797
}
0 commit comments