Spring Security의 최신 버전에서는 보안 설정을 Customizer를 사용하여 구성하는 방식을 권장하고 있습니다. 이 방식은 Spring Boot 2.7 및 Spring Security 5.7부터 도입되었습니다. 아래는 Customizer를 사용한 보안 설정 예시입니다.
SecurityConfig 클래스에서 보안 설정을 정의하고 필요한 Customizer를 주입합니다.
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.web.server.ServerHttpSecurity;
import org.springframework.security.web.server.SecurityWebFilterChain;
@Configuration
@EnableWebSecurity
public class SecurityConfig {
private final CorsCustomizer corsCustomizer;
private final AuthorizeExchangeCustomizer authorizeExchangeCustomizer;
private final OAuth2ResourceServerCustomizer oauth2ResourceServerCustomizer;
@Autowired
public SecurityConfig(
@Qualifier("corsCustomizer") CorsCustomizer corsCustomizer,
@Qualifier("authorizeExchangeCustomizer") AuthorizeExchangeCustomizer authorizeExchangeCustomizer,
@Qualifier("oAuth2ResourceServerCustomizer") OAuth2ResourceServerCustomizer oauth2ResourceServerCustomizer
) {
this.corsCustomizer = corsCustomizer;
this.authorizeExchangeCustomizer = authorizeExchangeCustomizer;
this.oauth2ResourceServerCustomizer = oauth2ResourceServerCustomizer;
}
@Bean
public SecurityWebFilterChain securityWebFilterChain(ServerHttpSecurity http) {
http
// CSRF 설정 (이와 같이, 간단한 설정은 람다로 가능.)
.csrf(ServerHttpSecurity.CsrfSpec::disable)
// CORS 설정
.cors(corsCustomizer)
// HTTP 보안 설정
.authorizeExchange(authorizeExchangeCustomizer)
// Oauth2 & JWT 인증 필터 설정
.oauth2ResourceServer(oauth2ResourceServerCustomizer);
// 아래 설정들도 필요한 경우, customizer 를 생성.
// .formLogin()
// .httpBasic()
// .anonymous()
// .authenticationManager()
// .exceptionHandling()
// .headers()
// .logout()
// .passwordManagement()
return http.build();
}
}
이 클래스는 ServerHttpSecurity.AuthorizeExchangeSpec을 커스터마이징합니다.
여기서 특정 URL 패턴에 대한 접근 권한을 설정할 수 있습니다. 예시로, 하나만 작성 하겠습니다.
유사한 방식으로, corsCustomizer, oauth2ResourceServerCustomizer 등을 작성 할 수 있습니다.
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.web.server.ServerHttpSecurity;
import org.springframework.security.config.web.server.ServerHttpSecurity.AuthorizeExchangeSpec;
import org.springframework.security.web.server.util.matcher.ServerWebExchangeMatchers;
import org.springframework.web.server.handler.FilteringWebHandler;
@Configuration("authorizeExchangeCustomizer")
public class AuthorizeExchangeCustomizer implements Customizer<ServerHttpSecurity.AuthorizeExchangeSpec> {
@Override
public void customize(ServerHttpSecurity.AuthorizeExchangeSpec authorizeExchangeSpec) {
authorizeExchangeSpec
.pathMatchers(HttpMethod.GET, "/health").permitAll()
.pathMatchers(HttpMethod.POST, "/auth/login").permitAll()
.anyExchange().authenticated();
}
}