개요
- Spring Security는 Spring 기반 애플리케이션의 인증(Authentication), 인가(Authorization) 및 일반적인 보안 보호 기능을 제공하는 프레임워크임
- Servlet Filter 기반으로 동작하며, 클라이언트의 요청이
DispatcherServlet에 도달하기 전에 보안 처리를 선제적으로 수행함
Spring Security 아키텍처

서블릿 필터와 DelegatingFilterProxy
- 서블릿 컨테이너(Tomcat 등)는 자체적인 필터(Filter) 생명주기를 가짐
- 스프링 객체(Bean)를 서블릿 필터로 사용하기 위해
DelegatingFilterProxy라는 특수한 필터가 브릿지 역할을 수행함 - 서블릿 컨테이너의 요청을 가로채어 스프링의 ApplicationContext에 등록된 특정 Bean(Spring Security의 필터)에게 처리를 위임함
FilterChainProxy
DelegatingFilterProxy가 요청을 위임하는 실제 스프링 Bean이 바로FilterChainProxy임- Spring Security의 진입점 역할을 하며, 모든 보안 관련 요청은 이 클래스를 거치게 됨
- 요청 URL에 따라 적합한
SecurityFilterChain을 선택하여 실행함
SecurityFilterChain
- 실제 보안 로직이 순서대로 구현된 여러 Spring Security Filter들의 묶음임
- 인증 및 권한 확인, CSRF 방어, CORS 설정 등의 작업이 각각의 특화된 필터에서 연쇄적으로 처리됨
- 프로젝트 요구사항에 따라 하나의 애플리케이션 내에 여러 개의
SecurityFilterChain을 구성할 수도 있음
인증(Authentication) 흐름
컴포넌트
- Authentication
- 인증 주체(Principal), 자격 증명(Credentials), 그리고 부여된 권한 목록(Authorities)을 담고 있는 객체임
- SecurityContext
- 현재 실행 중인 스레드의 보안 컨텍스트로,
Authentication객체를 보관함 - 일반적으로
ThreadLocal을 사용하는SecurityContextHolder를 통해 전역적으로 접근 가능함
- 현재 실행 중인 스레드의 보안 컨텍스트로,
- AuthenticationManager
- 인증 처리의 전반적인 관리를 담당하는 인터페이스임
- 주로
ProviderManager구현체를 사용하며, 내부적으로 여러 등록된AuthenticationProvider를 순회하며 인증을 시도함
- AuthenticationProvider
- 실제 인증 로직(DB 검증 등)을 수행함
- 폼 로그인 방식의 경우
UserDetailsService를 호출하여 사용자 정보를 데이터베이스에서 조회하고,PasswordEncoder로 비밀번호 일치 여부를 검증함
동작 순서

- 클라이언트가 자격 증명(예: 아이디/비밀번호)을 포함하여 요청을 보냄
UsernamePasswordAuthenticationFilter등의 인증 필터가 요청을 가로채어 미인증 상태의Authentication객체 생성- 생성된 객체를
AuthenticationManager에 전달하여 인증을 위임함 AuthenticationManager는 적절한AuthenticationProvider를 찾아 처리함- 내부적으로
UserDetailsService를 통해 데이터베이스 값과 대조 및 검증 수행 - 인증에 성공하면 권한(Authorities) 정보가 포함된 안전한(fully authenticated)
Authentication객체를 반환함 - 반환된 객체를
SecurityContextHolder에 저장하여 이후 요청부터는 별도 인증 없이 사용자를 식별함
인가(Authorization) 흐름
동작 방식
- 성공적으로 인증(Authentication)이 완료된 후, 클라이언트가 요청한 자원(URL, 메서드 등)에 접근할 ‘권한’이 있는지 검사하는 과정임
- Spring Security Filter Chain의 마지막 부근에 위치한
AuthorizationFilter가 이 역할을 담당함
권한 결정 기구
- 요청 객체(Request)와 현재 사용자의 권한 정보(
Authentication)를 바탕으로 접근을 허용할지 평가함 - 여러 방식의 설정(엔드포인트별 Role 기반 매칭, 표현식 등)을 바탕으로 보안 관리자가 정의한 정책과 일치하는지 대조함
- 접근이 거부될 경우
AccessDeniedException을 발생시켜 요청을 차단함
간단한 설정 예제 (Spring Security 6.x)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
@Configuration
@EnableWebSecurity
public class SecurityConfig {
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http
// CSRF 보호 비활성화 (API 서버의 경우)
.csrf(csrf -> csrf.disable())
// 세션 관리: STATELESS 설정 (JWT 토큰 기반 일 경우)
.sessionManagement(session -> session
.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
// HTTP 요청 인가 규칙 설정
.authorizeHttpRequests(auth -> auth
.requestMatchers("/api/public/**", "/login").permitAll()
.requestMatchers("/api/admin/**").hasRole("ADMIN")
.anyRequest().authenticated()
);
return http.build();
}
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
}