2022-07-31 @이영훈
Spring Security 5.7.0-M2 버전부터
WebSecurityConfigurerAdapter가 Deprecated 되었습니다. (해당 내용)In Spring Security 5.7.0-M2 we deprecated theWebSecurityConfigurerAdapter, as we encourage users to move towards a component-based security configuration.
WebSecurityConfigurerAdapter 가 deprecated 되었고, component 기반의 설정으로 변경할 것을 권장한다고 되어있습니다.스프링부트 2.7 버전에서 Spring Security 5.7으로 upgrade 하면서 해당 내용이 포함되었습니다.
SecurityFilterChain으로 migration guide 공식 문서
WebSecurityConfigurerAdapter 에서 컴포넌트 방법으로 변경 후 테스트 코드를 @WebMvcTest 에서 실패가 뜹니다.스프링부트 깃헙 위키에 migration guide가 소개되어 있습니다.
Migrating From WebSecurityConfigurerAdapter to SecurityFilterChain 내용을 보면
When configuring Spring Security withoutWebSecurityConfigurerAdapterand using Spring Boot’s sliced tests such as@WebMvcTest, you may need to make some changes to your application to make yourSecurityFilterChainbeans available to your tests by@Importing your security configuration class. See the reference documentation for further details.
WebMvcTest 테스트 오류 해결
다음과 같은 SecurityFilterChain 컴포넌트로 만든 Security 설정이 있습니다.
Kotlin
@Configuration
class WebSecurityConfig {
@Bean
fun filterChain(http: HttpSecurity): SecurityFilterChain {
http.cors()
.and()
.csrf().disable()
.sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.exceptionHandling()
return http.build()
}
}WebMvcTest에서 위에서 만든 Spring Security 설정을 import 합니다.
Kotlin
@WebMvcTest(HealthController::class)
@Import(WebSecurityConfig::class) // ⭐️ WebSecurityConfig를 import 했습니다.
internal class HealthControllerTest {
@Autowired
private lateinit var mockMvc: MockMvc
@Test
fun `test ping`() {
mockMvc.perform(
get("/").contentType(MediaType.APPLICATION_JSON)
)
.andExpect(status().isOk)
.andExpect(jsonPath("$.message").value("Hello World"))
}
}
