Skip to content

Commit 740d2d4

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

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
@@ -31,6 +31,7 @@ import org.springframework.beans.factory.annotation.Autowired
3131
import org.springframework.context.annotation.Bean
3232
import org.springframework.context.annotation.Configuration
3333
import org.springframework.http.HttpHeaders
34+
import org.springframework.http.HttpMethod
3435
import org.springframework.security.authentication.AuthenticationManager
3536
import org.springframework.security.authentication.ProviderManager
3637
import org.springframework.security.authentication.TestingAuthenticationProvider
@@ -57,6 +58,8 @@ import org.springframework.test.web.servlet.MockMvc
5758
import org.springframework.test.web.servlet.get
5859
import org.springframework.test.web.servlet.post
5960
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders
61+
import org.springframework.web.bind.annotation.PostMapping
62+
import org.springframework.web.bind.annotation.RestController
6063
import 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

Comments
 (0)