ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [Spring Boot] Spring Security 6에서 permitAll 경로 설정하기
    Spring Boot/기타 2025. 6. 5. 22:17
    반응형

     

    목차

       

      Spring Boot 3.x 이상을 사용하면, Spring Security 역시 자동으로 6.x 버전을 적용하게 됩니다.
      하지만 Spring Security 6부터는 구조적으로 많은 변화가 있었고, 그중에서도 특히 permitAll() 경로 설정 방식이 가장 혼란스럽게 느껴질 수 있습니다.
      특히 아래 항목들이 deprecated되거나 제거되면서, 기존 방식과의 호환성이 완전히 무너졌습니다.

       

      주요 변화 요약

      • WebSecurityConfigurerAdapter 완전 제거
      • antMatchers(...) deprecated
      • authorizeRequests(...)가 authorizeHttpRequests(...)로 변경
      • AntPathRequestMatcher 직접 생성 방식도 deprecated 
      • 람다 기반 DSL 스타일 코드 권장
      • permitAll() 설정은 String[] 기반으로 선언 권장

       

      1. Spring Security 5.x 방식

      Spring Security 5.x까지는 WebSecurityConfigurerAdapter를 상속받고 antMatchers()를 통해 경로별 접근 권한을 설정하는 방식입니다.

      @Configuration
      @EnableWebSecurity
      public class SecurityConfig extends WebSecurityConfigurerAdapter {
      
          @Override
          protected void configure(HttpSecurity http) throws Exception {
              http
                  .httpBasic().disable()
                  .csrf().disable()
                  .authorizeRequests()
                      .antMatchers("/", "/api-docs/**", "/swagger-ui/**", "/swagger-ui.html", "/token/**").permitAll()
                      .anyRequest().authenticated();
          }
      }

      Spring Security 5.7부터 WebSecurityConfigurerAdapter는 deprecated 되었고, 6.x에서는 완전히 제거되었습니다.

       

      2. Spring Security 5.8 ~ 6.x 혼합 방식

      SecurityFilterChain을 사용한 방식으로,  AntPathRequestMatcher 등을 사용하는 경우입니다.

      private RequestMatcher[] permitAllPattern() {
          return new RequestMatcher[] {
              new AntPathRequestMatcher("/"),
              new AntPathRequestMatcher("/api-docs/**"),
              new AntPathRequestMatcher("/swagger-ui/**"),
              new AntPathRequestMatcher("/swagger-ui.html"),
              new AntPathRequestMatcher("/token/**")
          };
      }
      
      @Bean
      public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
          http
              .httpBasic(HttpBasicConfigurer::disable)
              .csrf(CsrfConfigurer::disable)
              .authorizeHttpRequests(authz -> authz
                  .requestMatchers(permitAllPattern()).permitAll()
                  .anyRequest().authenticated()
              );
          return http.build();
      }

      AntPathRequestMatcher를 직접 생성하는 방식은 Spring Security 6에서 deprecated 되었으며, 향후 버전에서 완전히 제거될 예정입니다.

       

      3. Spring Security 6.x 권장 방식 (문자열 기반)

      가장 권장되는 방식은 requestMatchers(String...)를 이용해 permitAll() 경로를 문자열 배열로 선언하는 것입니다.

      private static final String[] PERMIT_ALL_PATTERNS = {
          "/",
          "/api-docs/**",
          "/swagger-ui/**",
          "/swagger-ui.html",
          "/token/**"
      };
      
      @Bean
      public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
          http
              .httpBasic(HttpBasicConfigurer::disable)
              .csrf(CsrfConfigurer::disable)
              .authorizeHttpRequests(authz -> authz
                  .requestMatchers(PERMIT_ALL_PATTERNS).permitAll()
                  .anyRequest().authenticated()
              );
          return http.build();
      }

      requestMatchers(...)는 String[]뿐 아니라 RequestMatcher도 받을 수 있지만, 대부분의 API, 정적 리소스는 문자열 경로 기반이 더 간결하고 유지보수가 용이합니다.

       

      정리

      Spring Security 6에서는 다음과 같은 방식으로 설정하는 것이 권장되며, 유지보수에도 적합합니다.

      • SecurityFilterChain을 Bean으로 등록
      • authorizeHttpRequests() DSL 방식 사용
      • requestMatchers(String...)으로 permitAll 경로 설정
      반응형

      댓글

    Designed by Tistory.