본문 바로가기
Backend/Spring

[Spring] Spring Security 로그인 구조 이해

by 이혤 2026. 3. 25.

Spring Security를 처음 접하면
“로그인은 되는데… 도대체 내부에서 뭐가 어떻게 돌아가는 거지?”
라는 생각이 한 번쯤 들게 됩니다.

특히 Controller에서 로그인 처리하는 것도 아닌데
어느 순간 인증이 되고, 세션까지 생기기 때문에
흐름이 잘 보이지 않는 경우가 많습니다.

이번 글에서는
Spring Security의 로그인 구조를
개념 → 구조 → 요청 흐름 → 실무 기준으로 정리해보겠습니다.


📌 목차

  1. Spring Security 로그인 개념
  2. 전체 구조 (Filter 기반 구조)
  3. 로그인 요청 처리 흐름
  4. 실무에서 사용하는 구조
  5. 정리

1. Spring Security 로그인 개념

Spring Security의 로그인은
Controller에서 처리하는 구조가 아닙니다.

👉 핵심은 이것입니다

  • 로그인 요청 → Filter에서 처리
  • 인증 성공 → SecurityContext에 저장
  • 이후 요청 → 세션 기반 인증 유지

즉,

👉 “로그인 = Filter 기반 인증 처리”


2. 전체 구조 (Filter 기반 구조)

그림1 spring security filter chain architecture diagram

 

Spring Security는
Filter Chain 구조로 동작합니다.

👉 주요 구성 요소

  • UsernamePasswordAuthenticationFilter
  • AuthenticationManager
  • UserDetailsService
  • SecurityContextHolder

👉 역할 정리

구성 요소역할
Filter 로그인 요청 가로채기
AuthenticationManager 인증 처리
UserDetailsService 사용자 정보 조회
SecurityContextHolder 인증 정보 저장

3. 로그인 요청 처리 흐름

그림2  spring security login authentication flow diagram

 

로그인 요청이 들어오면 아래 순서로 처리됩니다.


1️⃣ 로그인 요청

  • 사용자가 /login 요청 전송
  • id / password 포함

2️⃣ Filter에서 요청 가로채기

  • UsernamePasswordAuthenticationFilter 실행
  • 요청 파라미터 추출

3️⃣ Authentication 객체 생성

  • id / password 기반으로 생성
  • 아직 인증되지 않은 상태

4️⃣ AuthenticationManager로 전달

  • 실제 인증 처리 시작

5️⃣ UserDetailsService 호출

  • DB에서 사용자 조회
  • 비밀번호 비교

6️⃣ 인증 성공

  • Authentication 객체 생성 (인증 완료 상태)
  • 권한 정보 포함

7️⃣ SecurityContext에 저장

  • SecurityContextHolder에 저장
  • 이후 요청에서 인증 유지

8️⃣ 세션에 저장

  • Session 기반으로 로그인 유지

👉 핵심 요약

로그인 처리는 Controller가 아니라
Spring Security Filter 내부에서 자동 처리된다


4. 실무에서 사용하는 구조

실무에서는 보통 아래 방식으로 사용합니다.


✔ 로그인 URL 설정

http
    .formLogin()
    .loginPage("/login")
    .loginProcessingUrl("/login")
    .defaultSuccessUrl("/main")
 

 

👉 의미

  • /login → 로그인 페이지
  • /login POST → 필터가 자동 처리

✔ UserDetailsService 구현

@Override
public UserDetails loadUserByUsername(String username) {
    // DB 조회
    return new User(username, password, authorities);
}

 

👉 실무 포인트

  • 여기서 DB 조회
  • 권한(Role) 설정

✔ 인증 성공 이후

  • SecurityContextHolder에 저장
  • Session 유지
  • 이후 요청 자동 인증

👉 실무 핵심

  • Controller에서 로그인 처리 ❌
  • Filter + Service 기반 구조 ✔

5. 정리

👉 Spring Security 로그인 구조 핵심

  • 로그인은 Filter에서 처리된다
  • AuthenticationManager가 인증 수행
  • UserDetailsService가 사용자 조회
  • 인증 정보는 SecurityContext에 저장
  • Session 기반으로 로그인 유지

👉 한 줄 정리

Spring Security 로그인은
“Filter → 인증 → SecurityContext 저장 → Session 유지” 구조입니다.

 

 

 

 

참고자료

그림1 spring security filter chain architecture diagram

https://docs.spring.io/spring-security/reference/servlet/architecture.html

 

Architecture :: Spring Security

The Security Filters are inserted into the FilterChainProxy with the SecurityFilterChain API. Those filters can be used for a number of different purposes, like exploit protection, authentication, authorization, and more. The filters are executed in a spec

docs.spring.io

 

그림2 spring security login authentication flow diagram

https://medium.com/%40greekykhs/springsecurity-part-3-spring-security-flow-7da9cc3624ab

 

#SpringSecurity Part 3 : Spring Security Flow

In the Part 1 of #SpringSecurity tutorial, we learned the difference between Authentication and Authorization. In Part 2, we created a…

medium.com