≣ 목차
Lambda DSL 방식의 도입 - Spring Security 5.7.0 이상 버전
특히 주목해야 할 점은 Lambda DSL 방식이 도입되었다는 것입니다. 예를 들어, authorizeRequests() 메서드를 사용하여 특정 URL 패턴에 대한 접근 권한을 간단하게 정의할 수 있습니다.
Lambda DSL 도입 전
http.authorizeRequests()
.authorizeRequests()
.antMatchers("/blog/**").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login")
.permitAll()
.and()
Lambda DSL 도입 후
http.authorizeRequests(authorize -> authorize
.authorizeRequests(authorizeRequests -> authorizeRequests
.antMatchers("/blog/**").permitAll()
.anyRequest().authenticated()
)
.formLogin(formLogin -> formLogin
.loginPage("/login")
.permitAll()
)
이 기능이 도입된 주된 이유는 가독성을 높이기 위함이라는 자료가 많았습니다. 하지만 개인적으로는 그 차이가 크지 않다고 느꼈는데 IDE의 자동 완성 기능이 더 잘 동작한다는 장점이 있어서 좋은 업데이트를 한 것 같습니다.ㅎㅎㅎ!!
Spring Security 6.x 버전 달라진 사항
Spring Security 6.x로 업그레이드되면서 보안 설정 방식에 몇 가지 중요한 변화가 있었습니다. 그 중 가장 눈에 띄는 변화는 WebSecurityConfigurerAdapter가 더 이상 사용되지 않는다는 점입니다. 대신, SecurityFilterChain을 사용하여 보안 구성을 정의하는 방식으로 전환되었습니다.
기존 방식: WebSecurityConfigurerAdapter
이전 버전에서는 WebSecurityConfigurerAdapter 클래스를 상속받아 보안 설정을 커스터마이징했습니다. 이는 기본적인 설정이 포함된 추상 클래스를 상속하여 필요한 메서드를 오버라이드하는 방식이었기 때문에, 특정 요구사항에 맞춘 설정을 하기에는 조금 불편한 점이 많았습니다.
새로운 방식: SecurityFilterChain
Spring Security 6.x부터는 WebSecurityConfigurerAdapter가 사라지고, 대신 SecurityFilterChain 빈을 직접 주입하여 보안 규칙을 설정합니다.
간단한 예시
@Configuration
@EnableWebSecurity
public class SecurityConfig {
// SecurityFilterChain을 직접 주입
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http
.authorizeHttpRequests((authz) -> authz
.requestMatchers("/test/**").permitAll()
.anyRequest().authenticated()
)
.formLogin((form) -> form
.loginPage("/login")
.permitAll()
)
.logout((logout) -> logout.permitAll());
return http.build();
}
삭제된 메소드
- antMatchers()
- mvcMatchers()
- regexMatchers()
스프링 시큐리티에서 antMatchers(), mvcMatchers(), regexMatchers()는 URL 접근 권한을 설정하는 공통적인 역할을 하긴 했지만 각각의 목적이 약간씩 달랐습니다. 그래서 스프링 시큐리티 6.x 버전에서는 이들이 requestMatchers() 메소드로 통합되어 URL 접근 권한 설정이 더 간편해졌습니다.
개선 메소드
requestMatchers()
참고 자료
[1] https://yongoh1253.tistory.com/entry/Spring-Security-reference-1
[2] https://spring.io/blog/2022/02/21/spring-security-without-the-websecurityconfigureradapter
[3] https://tech-monster.tistory.com/216
'Spring' 카테고리의 다른 글
[Spring] 포인트컷, 어드바이스, 어드바이저 간단 정리 (1) | 2024.09.23 |
---|---|
[Spring] 스프링 프록시 팩토리 간단 정리 (0) | 2024.09.19 |
[Spring] 트랜잭션 전파 속성 정리 - propagation (0) | 2024.09.02 |
[Spring] @Transactional 정리 (1) | 2024.08.28 |
[Spring + DB] 스프링의 DB 예외 추상화 - ExceptionTranslator (0) | 2024.08.15 |