
안녕하세요. 오늘은
스프링 시큐리티를 다루는데 가장 기본이자 중요한 시작점인
Filter, Provider, Config에 알아볼게요.
1. Filter (필터)
필터의 역할은 HTTP 요청을 가로채서 인증/인가 처리 흐름을 시작하는 친구예요.
스프링 시큐리티는 서블링 필터 체인을 기반으로 작동해요.
이 필터 체인 중 일부가 스프링 시큐리티의 필터들이고, 요청이 들어오면
가장 먼저 여기를 통과하게 돼요.
필터의 작동 순서는
HTTP 요청 → 여러 시큐리티 필터 → 인증 여부 판단
일제 인증 로직은 필터가 AuthenticationManager에게 위임을 하게 돼요.
// 예시 필터
UsernamePasswordAuthenticationFilter: 로그인 요청에서 사용자명/비밀번호를 처리
BasicAuthenticationFilter: HTTP Basic 인증 처리
SecurityContextPersistenceFilter: SecurityContext를 관리
2. Provider (AuthenticationProvider)
프로바이더의 역할은 실제 인증을 수행하는 담당으로
필터가 로그인 정보를 담은 Authentication 객체를 생성하고,
AuthenticationManager가 이를 적절한 AuthenticationProvider에 전달을 해서
실제 DB를 통해 인증을 수행해 줘요.
예시 Provider:
DaoAuthenticationProvider: 사용자 정보를 DB에서 조회 (UserDetailsService 사용)
커스텀 Provider도 구현 가능
3. Config (설정)
컨픽은 시큐리티 전반의 설정을 담당하는 구성 클래스로
스프링 시큐리티에서는 Java Config 방식으로 보안 설정을 구성하는 것이 일반적이에요.
SecurityFilterChain 빈을 등록하거나
WebSecurityConfigurerAdapter (이전 방식) 또는 SecurityConfigurer를 이용해요.
주요 설정 내용:
어떤 URL은 인증 없이 접근 가능
어떤 필터를 어디에 끼울지
로그인/로그아웃 설정
커스텀 필터 또는 Provider 등록
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http
.authorizeHttpRequests(auth -> auth
.antMatchers("/login", "/public/**").permitAll()
.anyRequest().authenticated()
)
.formLogin(withDefaults())
.logout(withDefaults());
return http.build();
}
작동 순서 흐름도 (전체 인증 과정)
[클라이언트 요청]
↓
[FilterChain (여러 필터)]
↓
[특정 필터 선택, 예: UsernamePasswordAuthenticationFilter]
↓
[Authentication 객체 생성 (ex: UsernamePasswordAuthenticationToken)]
↓
[AuthenticationManager 호출]
↓
[AuthenticationProvider가 인증 시도]
↓
[인증 성공 시 Authentication 반환 (인증 정보 포함)]
↓
[SecurityContextHolder에 인증 정보 저장]
↓
[요청 처리 계속 진행 (Controller 진입 가능)]

여기까지 스프링 시큐리티를 다루는데 가장 기본이자 중요한 시작점인
Filter, Provider, Config에 알아봤는데요.
스피링 시큐리티는 배울 게 많아서 하나하나 천천히 복습해야 할 것 같아요.
'Backend > Java' 카테고리의 다른 글
| RESTful API (0) | 2025.09.07 |
|---|---|
| @Transactional (0) | 2025.09.04 |
| Spring Boot - JWT (0) | 2025.08.31 |
| Spring Security (2) | 2025.08.29 |
| Java - List vs Set (0) | 2025.08.25 |