@@ -31,6 +31,7 @@ import org.springframework.beans.factory.annotation.Autowired
3131import org.springframework.context.annotation.Bean
3232import org.springframework.context.annotation.Configuration
3333import org.springframework.http.HttpHeaders
34+ import org.springframework.http.HttpMethod
3435import org.springframework.security.authentication.AuthenticationManager
3536import org.springframework.security.authentication.ProviderManager
3637import org.springframework.security.authentication.TestingAuthenticationProvider
@@ -57,6 +58,8 @@ import org.springframework.test.web.servlet.MockMvc
5758import org.springframework.test.web.servlet.get
5859import org.springframework.test.web.servlet.post
5960import org.springframework.test.web.servlet.request.MockMvcRequestBuilders
61+ import org.springframework.web.bind.annotation.PostMapping
62+ import org.springframework.web.bind.annotation.RestController
6063import org.springframework.web.servlet.config.annotation.EnableWebMvc
6164
6265/* *
@@ -659,4 +662,57 @@ class HttpSecurityDslTests {
659662 }
660663 }
661664
665+ @Test
666+ fun `HTTP security when disabled Csrf Bean` () {
667+ this .spring.register(DisabledCsrfBeanConfig ::class .java, BasicController ::class .java).autowire()
668+
669+ this .mockMvc.post(" /test1" )
670+ .andExpect {
671+ status { isOk() }
672+ }
673+
674+ this .mockMvc.post(" /" )
675+ .andExpect {
676+ status { isForbidden() }
677+ }
678+ }
679+
680+ @RestController
681+ internal class BasicController {
682+
683+ @PostMapping(" /" )
684+ fun post ():String {
685+ return " ok"
686+ }
687+
688+ @PostMapping(" /test1" )
689+ fun test1 ():String {
690+ return " ok"
691+ }
692+ }
693+
694+ @Configuration
695+ @EnableWebSecurity
696+ open class DisabledCsrfBeanConfig {
697+
698+ @Bean
699+ open fun securityFilterChain (http : HttpSecurity ): SecurityFilterChain {
700+ http {
701+ authorizeHttpRequests {
702+ authorize(HttpMethod .POST , " /test1" , permitAll)
703+ authorize(anyRequest, authenticated)
704+ }
705+ }
706+ return http.build()
707+ }
708+
709+ @Bean
710+ open fun headersDsl (): HttpSecurityDsl .() -> Unit {
711+ return {
712+ csrf {
713+ disable()
714+ }
715+ }
716+ }
717+ }
662718}
0 commit comments