개요
Spring Boot 가 3.x Version 으로 들어서면서 Minor Version 이 바뀌었다보니
확실히 생각보다 변화된 부분이 많습니다.
Spring Boot 3.x.x 이전 설정 코드
기존 코드는 아래와 같이 Builder 형식으로 추가적인 설정을 해줬어야 했습니다.
.disable() 이 대표적인 예시라고 보면 됩니다.
Security 에서는 5.2.x 버전부터 Lambda DSL 방식으로 설정할 수 있도록 변경이 이루어진 적이 있습니다. 따라서, 예로 authorizeRequests() 에서 람다를 사용해서 표현할 수 있도록 되었습니다.
@Configuration
@RequiredArgsConstructor
@EnableWebSecurity
public class SecurityConfig {
private final CustomUserDetailsService userDetailsService;
private final DataSource dataSource;
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http
.csrf().disable()
.formLogin().disable()
.and().authorizeRequests()
.antMatchers("/auth/**").authenticated()
.and()
.headers()
.frameOptions()
.sameOrigin()
return http.build();
}
@Bean
public WebSecurityCustomizer webSecurityCustomizer() {
// 정적 리소스 spring security 대상에서 제외
return (web) -> {
web
.ignoring()
.requestMatchers(
PathRequest.toStaticResources().atCommonLocations()
);
};
}
}
Spring boot 3.x 이후 설정 코드
@Configuration
public class SecurityConfig {
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http
.csrf(AbstractHttpConfigurer::disable)
.formLogin(Customizer.withDefaults())
.authorizeHttpRequests(authorizeRequest ->
authorizeRequest
.requestMatchers(
AntPathRequestMatcher.antMatcher("/auth/**")
).authenticated()
.requestMatchers(
AntPathRequestMatcher.antMatcher("/h2-console/**")
).permitAll()
)
.headers(
headersConfigurer ->
headersConfigurer
.frameOptions(
HeadersConfigurer.FrameOptionsConfig::sameOrigin
)
);
return http.build();
}
@Bean
public WebSecurityCustomizer webSecurityCustomizer() {
// 정적 리소스 spring security 대상에서 제외
return (web) ->
web
.ignoring()
.requestMatchers(
PathRequest.toStaticResources().atCommonLocations()
);
}
}
위와 같이Spring Boot 3.x.x 버전 즉, Security 6.1.x 버전 부터는 기존에 썼던 csrf() , formLogin() , headers() 와 같이 인수를 갖지 않는 메서드들은 전부 Deprecated 되었습니다.
권한 설정 코드
Boot 3.x 이전에는 antMatchers(...), mvcMatchers(...) 만 선언해서 특정 리소스에 대한 접근권한 설정을 줄 수 있었습니다.
http
.csrf().disable()
.formLogin().disable()
.and()
.authorizeRequests()
.antMatchers("/auth/**").authenticated()
만약 위 구조를 Lambda DSL 로 변경하면 다음처럼 바뀌죠
.authorizeHttpRequests(authorizeRequest ->
authorizeRequest
.antMatcher("/auth/**").authenticated()
)
Boot 3.x 이후 부터는 antMatchers(), mvcMatchers() 가 없어지고 requestMatchers() 를 사용하도록 바뀌었습니다.
.authorizeHttpRequests(authorizeRequest ->
authorizeRequest
.requestMatchers("/auth/**").authenticated()
)
문제는 위와 같이 설정을 할 경우 예외가 발생합니다
Factory method 'filterChain' threw exception with message: This method cannot decide whether these patterns are Spring MVC patterns or not. If this endpoint is a Spring MVC endpoint, please use requestMatchers(MvcRequestMatcher); otherwise, please use requestMatchers(AntPathRequestMatcher).
해당 문제를 해결하기 위해서 AntPathRequestMatcher 클래스의 속성을 집어넣었습니다.
.authorizeHttpRequests(authorizeRequest ->
authorizeRequest
.requestMatchers(
AntPathRequestMatcher.antMatcher("/auth/**")
).authenticated()
)
참고 링크 : https://velog.io/@kide77/Spring-Boot-3.x-Security-기본-설정-및-변화
'Spring boot > Spring Security' 카테고리의 다른 글
Authorize HttpServletRequests (1) | 2024.07.17 |
---|---|
Password Storage (0) | 2024.07.17 |
BCryptPasswordEncoder란? (0) | 2024.07.15 |
Spring Security - JPA 연동하기 (0) | 2024.07.15 |