- Add POST /login assertion
- Rearrange test and config class

Issue gh-12552
This commit is contained in:
Josh Cummings 2023-02-15 17:44:49 -07:00
parent e2332d9620
commit a4bc0a6f3c
1 changed files with 16 additions and 8 deletions

View File

@ -32,6 +32,7 @@ import org.springframework.security.config.test.SpringTestContext
import org.springframework.security.config.test.SpringTestContextExtension import org.springframework.security.config.test.SpringTestContextExtension
import org.springframework.security.core.userdetails.User import org.springframework.security.core.userdetails.User
import org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestBuilders.formLogin import org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestBuilders.formLogin
import org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf
import org.springframework.security.web.SecurityFilterChain import org.springframework.security.web.SecurityFilterChain
import org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler import org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler
import org.springframework.security.web.authentication.SimpleUrlAuthenticationSuccessHandler import org.springframework.security.web.authentication.SimpleUrlAuthenticationSuccessHandler
@ -39,6 +40,7 @@ import org.springframework.security.web.authentication.WebAuthenticationDetailsS
import org.springframework.stereotype.Controller import org.springframework.stereotype.Controller
import org.springframework.test.web.servlet.MockMvc import org.springframework.test.web.servlet.MockMvc
import org.springframework.test.web.servlet.get import org.springframework.test.web.servlet.get
import org.springframework.test.web.servlet.post
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.redirectedUrl import org.springframework.test.web.servlet.result.MockMvcResultMatchers.redirectedUrl
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.status import org.springframework.test.web.servlet.result.MockMvcResultMatchers.status
import org.springframework.web.bind.annotation.GetMapping import org.springframework.web.bind.annotation.GetMapping
@ -90,16 +92,12 @@ class FormLoginDslTests {
} }
@Configuration @Configuration
@EnableWebMvc
@EnableWebSecurity @EnableWebSecurity
open class DisabledConfig { open class FormLoginConfig {
@Bean @Bean
open fun securityFilterChain(http: HttpSecurity): SecurityFilterChain { open fun securityFilterChain(http: HttpSecurity): SecurityFilterChain {
http.formLogin()
http { http {
formLogin { formLogin {}
disable()
}
} }
return http.build() return http.build()
} }
@ -113,15 +111,25 @@ class FormLoginDslTests {
.andExpect { .andExpect {
status { isNotFound() } status { isNotFound() }
} }
this.mockMvc.post("/login") {
with(csrf())
}.andExpect {
status { isNotFound() }
}
} }
@Configuration @Configuration
@EnableWebMvc
@EnableWebSecurity @EnableWebSecurity
open class FormLoginConfig { open class DisabledConfig {
@Bean @Bean
open fun securityFilterChain(http: HttpSecurity): SecurityFilterChain { open fun securityFilterChain(http: HttpSecurity): SecurityFilterChain {
http.formLogin()
http { http {
formLogin {} formLogin {
disable()
}
} }
return http.build() return http.build()
} }