@@ -29,6 +29,7 @@ import org.springframework.beans.factory.annotation.Autowired
2929import org.springframework.context.annotation.Bean
3030import org.springframework.context.annotation.Configuration
3131import org.springframework.http.HttpHeaders
32+ import org.springframework.http.HttpMethod
3233import org.springframework.security.authentication.AuthenticationManager
3334import org.springframework.security.authentication.ProviderManager
3435import org.springframework.security.authentication.TestingAuthenticationProvider
@@ -55,6 +56,8 @@ import org.springframework.test.web.servlet.MockMvc
5556import org.springframework.test.web.servlet.get
5657import org.springframework.test.web.servlet.post
5758import org.springframework.test.web.servlet.request.MockMvcRequestBuilders
59+ import org.springframework.web.bind.annotation.PostMapping
60+ import org.springframework.web.bind.annotation.RestController
5861import org.springframework.web.servlet.config.annotation.EnableWebMvc
5962
6063/* *
@@ -657,4 +660,57 @@ class HttpSecurityDslTests {
657660 }
658661 }
659662
663+ @Test
664+ fun `HTTP security when disabled Csrf Bean` () {
665+ this .spring.register(DisabledCsrfBeanConfig ::class .java, BasicController ::class .java).autowire()
666+
667+ this .mockMvc.post(" /test1" )
668+ .andExpect {
669+ status { isOk() }
670+ }
671+
672+ this .mockMvc.post(" /" )
673+ .andExpect {
674+ status { isForbidden() }
675+ }
676+ }
677+
678+ @RestController
679+ internal class BasicController {
680+
681+ @PostMapping(" /" )
682+ fun post ():String {
683+ return " ok"
684+ }
685+
686+ @PostMapping(" /test1" )
687+ fun test1 ():String {
688+ return " ok"
689+ }
690+ }
691+
692+ @Configuration
693+ @EnableWebSecurity
694+ open class DisabledCsrfBeanConfig {
695+
696+ @Bean
697+ open fun securityFilterChain (http : HttpSecurity ): SecurityFilterChain {
698+ http {
699+ authorizeHttpRequests {
700+ authorize(HttpMethod .POST , " /test1" , permitAll)
701+ authorize(anyRequest, authenticated)
702+ }
703+ }
704+ return http.build()
705+ }
706+
707+ @Bean
708+ open fun headersDsl (): HttpSecurityDsl .() -> Unit {
709+ return {
710+ csrf {
711+ disable()
712+ }
713+ }
714+ }
715+ }
660716}
0 commit comments