GenericFilterBean 과 OncePerRequestFilterGenericFilterBean vs OncePerRequestFilterSecurityFilterChain 에 등록된 필터는 GenericFilterBean 기반과 OncePerRequestFilter 기반으로 나뉜다.두 방식의 차이는 "한 번의 클라이언트 요청" 기준에서 동작 방식이 다르다는 점이다.필터 종류실행 방식GenericFilterBean같은 요청에서 여러 번 실행될 수 있음OncePerRequestFilter같은 요청에서 한 번만 실행됨GenericFilterBean 의 동작 방식요청이 같은 필터를 여러 번 통과할 경우, 통과한 횟수만큼 실행된다.즉, 클라이언트의 한 번 요청에 대해 여러 번 필터가 실행될 수 있다.Cu..
필터 상속과 요청 전파SecurityFilterChain 의 필터SecurityFilterChain 에 속한 각각의 필터를 이해하기 전에, 필터의 구조를 먼저 살펴보자.모든 필터는 공통된 부모 필터 클래스를 상속받아 구현되며, 이를 통해 중복 코드 방지 및 역할 분리가 가능하다.필터의 상속Spring Security 의 필터들은 계층적으로 구성되어 있으며, 공통적인 기능을 가진 부모 필터를 상속하여 구현된다.필터는 최상위 Filter 인터페이스를 기반으로 하며, 이를 확장하여 여러 필터가 존재한다.SecurityFilterChain 에서의 필터 계층 구조SecurityFilterChain 에서 필터들은 체인 형태로 연결되며, 각 필터가 특정 보안 로직을 수행한 후 다음 필터로 요청을 전달한다.상속의 이점..
SecurityContextHolderSecurityFilterChain 에서 작업 상태 저장 필요SecurityFilterChain 내부의 각 필터는 시큐리티 관련 작업을 수행한다.모든 필터는 각기 다른 기능을 수행하며, 앞단에서 수행된 작업의 결과를 뒷단 필터에서 확인할 수 있어야 한다.예를 들어, 인가(Authorization) 필터는 요청을 허용할지 결정하기 위해 인증(Authentication) 필터가 설정한 사용자 권한(ROLE) 정보를 확인해야 한다.SecurityContext 와 Authentication 객체SecurityContextHolder 는 현재 요청의 보안 컨텍스트(SecurityContext)를 관리하는 클래스이다.SecurityContext 내부에는 인증(Authentica..
SecurityFilterChain 구조@EnableWebSecurity 에 debug 모드를 true 로 설정하면, 요청이 통과하는 SecurityFilterChain 의 filter 목록이 출력 된다.Spring Security 디버깅을 활성화하는 옵션으로, 개발 환경에서만 사용해야 한다.debug 모드의 기본값은 false 이다. import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org...
SecurityFilterChain 등록Spring Boot 에서 spring-boot-starter-security 의존성을 추가하면 기본적으로 DefaultSecurityFilterChain 이 자동으로 생성된다.기본 설정은 모든 요청을 인증해야 하며, form-based 로그인 페이지를 제공하는 방식 이다.개발자가 직접 SecurityFilterChain 을 등록하지 않으면 Spring Boot 의 SecurityAutoConfiguration 이 자동으로 적용 된다.SecurityFilterChain 을 등록하기 위해서는 SecurityFilterChain 을 리턴하는 @Bean 메소드를 등록 한다.import org.springframework.context.annotation.Bean;impo..
Spring Security 동작 원리 개요클라이언트의 요청은 WAS(Web Application Server / ex> tomcat) 의 filter 를 통과한 다음 Spring Container의 Controller에 도달한다.WAS 의 filter 에 새로운 filter 를 추가하여 요청을 가로챈다.가로챈 요청은 Spring Container 내부에 정의한 로직을 통과 한다.Spring Security 의 로직을 처리한 다음 다시 WAS 의 다음 filter 로 복귀하여 요청을 처리 한다.DelegatingFilterProxySpring Security 의 필터 체인을 WAS 의 Servlet Filter 로 등록하기 위한 Spring 제공 클래스 이다.WAS(Tomcat 등)에서 Delegatin..