Skip to content

Commit fb9d9fd

Browse files
Verify POST endpoints when CSRF is disabled
Signed-off-by: Tran Ngoc Nhan <ngocnhan.tran1996@gmail.com>
1 parent 6d6552a commit fb9d9fd

1 file changed

Lines changed: 56 additions & 0 deletions

File tree

config/src/test/kotlin/org/springframework/security/config/annotation/web/HttpSecurityDslTests.kt

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import org.springframework.beans.factory.annotation.Autowired
2929
import org.springframework.context.annotation.Bean
3030
import org.springframework.context.annotation.Configuration
3131
import org.springframework.http.HttpHeaders
32+
import org.springframework.http.HttpMethod
3233
import org.springframework.security.authentication.AuthenticationManager
3334
import org.springframework.security.authentication.ProviderManager
3435
import org.springframework.security.authentication.TestingAuthenticationProvider
@@ -55,6 +56,8 @@ import org.springframework.test.web.servlet.MockMvc
5556
import org.springframework.test.web.servlet.get
5657
import org.springframework.test.web.servlet.post
5758
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders
59+
import org.springframework.web.bind.annotation.PostMapping
60+
import org.springframework.web.bind.annotation.RestController
5861
import 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

Comments
 (0)