Spring
Spring security Config 설정 | 생각없이 따라하다가 deprecated 놓치잖아
힐안
2025. 1. 8. 16:20
Springboot 3.xx 버전 업그레이드 이후
spring security의 config 설정 부분의 상당 부분이 변경되었다.
package com.example.testsecurity.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.web.SecurityFilterChain;
@Configuration
@EnableWebSecurity
public class SecurityConfig {
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception{
http
.authorizeHttpRequests((auth) -> auth
.requestMatchers("/", "/auth/login", "/auth/loginProc").permitAll()
.requestMatchers("/admin").hasRole("ADMIN")
.requestMatchers("/my/**").hasAnyRole("ADMIN", "USER")
.anyRequest().authenticated()
);
http
.formLogin((auth) -> auth.loginPage("/login")
.loginProcessingUrl("/loginProc")
.permitAll()
);
http
.csrf((auth) -> auth.disable());
return http.build();
}
}
우선, config 클래스를 만들 때 기존에는 WebSecurityConfigurerAdapter를 extends 해서 오버라이드 했는데 SecurityFilterChain을 @Bean 등록해서 사용하는 방식으로 변경되었다.
위 예시와 같이 "람다식"으로 표현하는 것이 필수적이다.