Programming
[Spring Boot 2.x -> Spring Boot 3.x] 마이그레이션 Spring Security 설정 방식
ipxy
2025. 3. 22. 14:37
728x90
Spring Boot 2.x와 3.x의 가장 큰 차이 중 하나는 Spring Security 설정 방식의 변화입니다. 특히 WebSecurityConfigurerAdapter의 제거로 인해 기존 방식으로 Security를 설정하던 코드들이 3.x에서는 더 이상 동작하지 않게 되었습니다.
✅ Spring Boot 2.x (Spring Security 5.x 기준)
주요 특징
- WebSecurityConfigurerAdapter를 상속하여 보안 설정
- configure(HttpSecurity http)와 configure(AuthenticationManagerBuilder auth) 메서드를 오버라이드하여 설정
예시 코드
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/", "/public").permitAll()
.anyRequest().authenticated()
.and()
.formLogin();
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth
.inMemoryAuthentication()
.withUser("user").password("{noop}password").roles("USER");
}
}
✅ Spring Boot 3.x (Spring Security 6.x 기준)
주요 변경점
- WebSecurityConfigurerAdapter가 제거됨
- SecurityFilterChain을 @Bean으로 등록
- UserDetailsService, PasswordEncoder 등도 명시적으로 등록 필요
- 전체적으로 함수형 스타일로 변경
예시 코드
@Configuration
@EnableWebSecurity
public class SecurityConfig {
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http
.authorizeHttpRequests(authz -> authz
.requestMatchers("/", "/public").permitAll()
.anyRequest().authenticated()
)
.formLogin(Customizer.withDefaults());
return http.build();
}
@Bean
public UserDetailsService userDetailsService() {
UserDetails user = User.withUsername("user")
.password(passwordEncoder().encode("password"))
.roles("USER")
.build();
return new InMemoryUserDetailsManager(user);
}
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder(); // 반드시 명시해야 함
}
}
🔄 주요 마이그레이션 포인트 정리
항목 | Spring Boot 2.x | Spring Boot 3.x |
보안 설정 방식 | WebSecurityConfigurerAdapter 상속 | SecurityFilterChain Bean 등록 |
사용자 정의 인증 | configure(AuthenticationManagerBuilder) | UserDetailsService Bean 직접 정의 |
PasswordEncoder | 생략 가능 | 반드시 명시 필요 |
URL 설정 | antMatchers() | requestMatchers() (기능 동일하지만 메서드 이름 변경) |
CSRF 설정 등 | http.csrf().disable() 등 기존 동일 | 함수형 체인으로 구성 |
728x90