[Spring Security] 6.1 이후의 SecurityFilterChain 설정

mrcocoball·2024년 4월 14일

Spring Framework

목록 보기
10/20

이슈

회사에서 사용하는 Spring Boot의 버전이 3.0.9였는데 사이드 프로젝트에서는 그 이후의 버전인 3.1.10을 사용하게 되었다. 그 이후 Spring Security 설정 클래스에서 SecurityFilterChain을 지정하는 코드를 작성하던 도중 컴파일 에러가 발생하게 되었다.

확인해보니 Spring Boot 3.1.10 버전의 Spring Security의 버전이 6.1.8이고 기존에 사용하던 Spring Security의 버전은 CVE 문제로 6.0.7로 사용하고 있었는데 두 버전 간의 SecurityFilterChain 작성 방식이 변경되어 생긴 내용이었다.

변경된 내용 및 예제

https://docs.spring.io/spring-security/reference/servlet/configuration/java.html#jc-custom-dsls

최신 버전의 공식 문서 상에서 이전 버전과의 변경점에 대해 소개하는 내용은 안 보이긴 하지만 (못 찾은 것일 수도 있다) 각종 설정을 비활성화하는 .disable(), 관련 메서드를 이어주던 .and()가 사라졌고 각종 설정에 대한 체인이 .and()로 엮이지 않고 블록 내부로 옮겨진 것으로 보인다.

6.1 이전 설정

    @Bean
    @Profile("local")
    public SecurityFilterChain securityFilterChainLocal(HttpSecurity http) throws Exception {

        return http
                .httpBasic().disable()
                .csrf().disable()
                .userDetailsService(customUserDetailsService)
                .authorizeHttpRequests(auth -> auth
                        .requestMatchers(PathRequest.toStaticResources().atCommonLocations()).permitAll()
                        .requestMatchers(new AntPathRequestMatcher("/user/login")).permitAll()
                        .requestMatchers(new AntPathRequestMatcher ("/user/join")).permitAll()
                        .requestMatchers(new AntPathRequestMatcher ("/api/v1/internal/probe/**")).permitAll()
                        .anyRequest().authenticated()
                        .and())
                .formLogin().loginPage("/user/login")
                .successHandler(authenticationSuccessHandler())
                .failureUrl("/user/login?error=true")
                .failureHandler(authenticationFailureHandler())
                .and()
                .logout()
                .logoutSuccessHandler(logoutSuccessHandler())
                .and()
                .build();
    }

6.1 이후 설정

    @Bean
    @Profile("local")
    public SecurityFilterChain securityFilterChainLocal(HttpSecurity http) throws Exception {

        return http
                .httpBasic(AbstractHttpConfigurer::disable)
                .csrf(AbstractHttpConfigurer::disable)
                .userDetailsService(userDetailsService)
                .authorizeHttpRequests(auth -> auth
                        .requestMatchers(PathRequest.toStaticResources().atCommonLocations()).permitAll()
                        .requestMatchers(new AntPathRequestMatcher("/user/login")).permitAll()
                        .requestMatchers(new AntPathRequestMatcher ("/user/join")).permitAll()
                        .requestMatchers(new AntPathRequestMatcher ("/api/v1/internal/probe/**")).permitAll()
                        .anyRequest().authenticated())
                .formLogin(form -> form.loginPage("/user/login")
                        .successHandler(authenticationSuccessHandler())
                        .failureUrl("/user/login?error=true")
                        .failureHandler(authenticationFailureHandler()))
                .logout(logout -> logout.logoutSuccessHandler(logoutSuccessHandler()))
                .build();
    }

두 코드를 비교해보면 현재의 필터 체인이 훨씬 더 간결하고 어떤 설정에 대한 부가 설정을 다루고 있는지 명확하게 보인다.

profile
Backend Developer

0개의 댓글