๋ฏธ๋ฃจ๊ณ ๋ฏธ๋ฃจ๋ Spring Security๋ฅผ ์ฌ์ฌ ๊ณต๋ถํ ๋๊ฐ ๋ ๋ฏ ํ๋ค.... ๊ทธ๋ ์ด ์๋๋ค


1. Spring Security๋
Spring Security๋ Spring ๊ธฐ๋ฐ์ ์ ํ๋ฆฌ์ผ์ด์ ์ ๋ณด์์ ๋ด๋นํ๋ ์คํ๋ง ํ์ ํ๋ ์์ํฌ์ด๋ค.
๋ก๊ทธ์ธ, ๋ณด์์ ์ํ ์ธ์ฆ • ์ธ๊ฐ๋ค์ ๊ฐ๋ฐํ๊ธฐ ์ํด์ Spring Security๋ฅผ ๋ง์ด ์ฌ์ฉํ๋ค.
2. Spring Security ์๋ฆฌ
Spring Security์ ํ๋ฆ์ ๋ค์๊ณผ ๊ฐ๋ค.
์์ฒญ(Request) → ํํฐ(Filter) → ์ธ์ฆ(Authentication) → ์ธ๊ฐ(Authorization)
(1) ์์ฒญ(Request)
์ฌ๊ธฐ์ ์์ฒญ(Request)๋ ํด๋ผ์ด์ธํธ๊ฐ ์๋ฒ๋ก ๋ณด๋ด๋ ๋ชจ๋ HTTP ์์ฒญ์ ์๋ฏธํ๋ค.
์๋ฒ๋ ํ ๋ฒ ์ธ์ฆํ๋ค๊ณ ํด์ ๊ณ์ ๋ก๊ทธ์ธ ์ํ์์ ๊ธฐ์ตํ์ง ์๊ณ , ๊ธฐ์ต๋ ฅ์ด ์๋ ์กด์ฌ(Stateless)๋ก ์ค๊ณ๋๋ค.
๋ฐ๋ผ์ ์์ฒญ์ด ๋ค์ด์ฌ ๋ ๋ง๋ค "์ด ์์ฒญ์ ๋ณด๋ธ ์ฌ๋์ด ์๊น ๊ทธ ์ฌ์ฉ์๋ผ๋ ์ฆ๊ฑฐ๊ฐ ์ด๋จ์ง" ๋ผ๋ ์๊ฐ์ ํ๋๋ฐ, ๊ทธ ์ฆ๊ฑฐ๊ฐ ๋ฐ๋ก JWT ํ ํฐ์ด๊ณ , ๊ทธ๊ฑธ ๊ฒ์ฌํ๋ ๊ฒ์ด JWTFilter์ด๋ค.
CRUD์ ๊ฐ์ ์ฌ๋ฌ RestfulAPI ์์ฒญ์ด ๋ค์ด์ฌ ๋ ๋ง๋ค, Authorization ํค๋์ ์๋ JWT๋ฅผ JWTFilter๊ฐ ๋งค๋ฒ ํ์ธํ๋ค.
(2) ํํฐ(Filter)

์์ฒญ์ด ๋ค์ด์ค๋ฉด ํํฐ ์ฒด์ธ(Filter Chain)์ด ์ฌ๋ฌ ํํฐ๋ค์ ๋์ดํด์, ์์ฒญ์ด ๋ค์ด์ฌ ๋ ๋ง๋ค ์ด ํํฐ๋ค์ ์ค์ค์ด ๊ฒ์ฌํ๊ฒ ๋๋ค.
๋ํ์ ์ธ ํํฐ๋ ์๋์ ๊ฐ๋ค.
- UsernamePasswordAuthenticationFilter(์ผ๋ฐ ๋ก๊ทธ์ธ) : ํผ ๊ธฐ๋ฐ ์ธ์ฆ ์ฒ๋ฆฌ - LoginRequest์ ๊ฐ์ ID/PW ์์ฒญ์ ์ฒ๋ฆฌํ๊ณ Authentication ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ค.
- JWTAuthenticationFilter / JWTAuthorizationFilter(JWT ๋ก๊ทธ์ธ) : JWT ๊ธฐ๋ฐ ์ธ์ฆ ์ฒ๋ฆฌ - ์์ฒญ ํค๋์ JWT ํ ํฐ์ ์ถ์ถํ์ฌ ์ฌ์ฉ์๋ฅผ ์ธ์ฆํ๋ค(์ฌ์ฉ์๊ฐ ๊ตฌํํ๋ ํํฐ).
- SecurityContextPersistenceFilter : ์ธ์ฆ ์ ๋ณด ์ ์ง - ์ธ์ฆ ์ฑ๊ณต ํ SecurityContext์ Authentication ๊ฐ์ฒด๋ฅผ ์ ์ฅํ๊ฑฐ๋ ์กฐํํ๋ค. ์์ฒญ์ด ์์๋ ๋ ์ด์ ์ ์ ์ฅ๋ SecurityContext๊ฐ ์๋ค๋ฉด ์ฝ์ด์ ๋ถ๋ฌ์ค๊ณ (JWT์์ ๋ณดํต ๋นํ์ฑํ๋จ) ์์ฒญ์ด ๋๋๋ฉด SecurityContext๋ฅผ clear(์ญ์ )ํ๋ค.
- ExceptionTranslationFilter : ์์ธ ์ฒ๋ฆฌ - ์ธ์ฆ ๋ฐ ์ธ๊ฐ ์์ธ๋ฅผ ์ฒ๋ฆฌํ๊ณ ์ ์ ํ HTTP ์๋ต ์ฝ๋๋ฅผ ๋ฐํํ๋ค.
- FilterSecurityInterceptor(๋ง์ง๋ง ๊ถํ ์ฒดํฌ) : ์ต์ข ์ธ๊ฐ ์ฒ๋ฆฌ - ๋ฆฌ์์ค ์ ๊ทผ ์ ๋ง์ง๋ง์ผ๋ก ๊ถํ(Authority)์ ํ์ธํ์ฌ ์ธ๊ฐ(Authorization)๋ฅผ ๊ฒฐ์ ํ๋ค. - ์ผ๋ฐ Interceptor๊ณผ ํท๊ฐ๋ฆฌ์ง ๋ง๊ฒ.
์ฐธ๊ณ
- Persistence : ์ง์์ฑ, ๋๊ธฐ
- SecurityContext : ์คํ๋ง ์ํ๋ฆฌํฐ์์ "์ง๊ธ ์ด ์์ฒญ์ ๋ณด๋ธ ์ฌ๋์ ๋ํ ์ธ์ฆ(๋ก๊ทธ์ธ)์ ๋ณด๊ฐ ๋ค์ด ์๋ ์์ ๊ฐ๋ฐฉ" ๊ฐ์ ๋๋. ์คํ๋ง์์ JWT๊ฐ์ ์ธ์ฆ ๊ณผ์ ์ ๊ฑธ์ณ์ "์ธ์ฆ๋ ์ฌ์ฉ์"์์ด ํ์ธ์ด ๋๋ฉด, ๊ทธ ์ธ์ฆ ๊ฒฐ๊ณผ๋ฅผ SecurityContext์ ๋ณด๊ดํ๋ค. ์ด SecurityContext ์์ ๋ค์ด ์๋ ์ ๋ณด๋ Authentication ๊ฐ์ฒด์ด๋ค.
- Authentication ๊ฐ์ฒด : ์ธ์ฆ์ ๊ฒฐ๊ณผ๋ฅผ ๋ด๊ณ ์๋ ํต์ฌ ๊ฐ์ฒด. ๋๊ฐ ์์ฒญํ๋์ง(username), ๊ทธ ์ฌ๋์ ๊ถํ(ROLE_USER, ROLE_ADMIN), ์ธ์ฆ์ด ๋ฌ๋์ง(Boolean) ์ด ๋ค์ด์๋ค.
- Authentication ๊ฐ์ฒด์ ์ญํ - GPTํผ์ ๐ฝ
1. ์ฌ์ฉ์ ์ ๋ณด (Principal & Credentials)
Principal (์ฃผ์ฒด): ํ์ฌ ์์คํ ์ ์ ๊ทผํ๋ ค๋ ์ฌ์ฉ์๋ฅผ ๋ํ๋ ๋๋ค. ๋ณดํต ์ฌ์ฉ์ ์ด๋ฆ(username), ์ฌ์ฉ์ ID, ๋๋ ์ฌ์ฉ์ ์์ธ ์ ๋ณด(UserDetails) ๊ฐ์ฒด ์์ฒด๊ฐ ๋ ์ ์์ต๋๋ค.
Credentials (์๊ฒฉ ์ฆ๋ช ): ์ฌ์ฉ์์ ๋น๋ฐ๋ฒํธ๋ ํ ํฐ๊ณผ ๊ฐ์ด ์ฌ์ฉ์๊ฐ ์์ ์ด ์ฃผ์ฅํ๋ ์ฌ๋์์ ์ฆ๋ช ํ๋ ๋ฐ ์ฌ์ฉ๋๋ ์ ๋ณด์ ๋๋ค. ์ธ์ฆ์ด ์๋ฃ๋ ํ์๋ ๋ณด์์ ์ํด ์ด ์ ๋ณด๋ ๋ณดํต ์ง์์ง๋๋ค.
2. ์ธ์ฆ ์ํ (Authenticated)
isAuthenticated() ๋ฉ์๋๋ฅผ ํตํด ์ด ๊ฐ์ฒด๊ฐ ๋ํ๋ด๋ ์ฌ์ฉ์๊ฐ ์ธ์ฆ๋์๋์ง(true) ๋๋ ์ธ์ฆ ์ ์ธ์ง(false) ์ํ๋ฅผ ๋ํ๋ ๋๋ค.
3. ๊ถํ ์ ๋ณด (Authorities)
์ธ์ฆ๋ ์ฌ์ฉ์๊ฐ ๊ฐ์ง ๊ถํ ๋ชฉ๋ก(GrantedAuthority)์ ๋ด๊ณ ์์ต๋๋ค. ์๋ฅผ ๋ค์ด, ROLE_ADMIN, ROLE_USER ๋ฑ์ ์ ๋ณด๋ฅผ ๋ด๊ณ ์์ด, ์ดํ ์ธ๊ฐ(Authorization) ๋จ๊ณ์์ ์ฌ์ฉ์๊ฐ ํน์ ๋ฆฌ์์ค์ ์ ๊ทผํ ์ ์๋์ง ํ๋จํ๋ ๊ทผ๊ฑฐ๊ฐ ๋ฉ๋๋ค.
ํํฐ๊ฐ ์ฐจ๋ก ์ฐจ๋ก ์์ฒญ์ ๋๊ธฐ๋๋ฐ, ํํฐ ์ค๊ฐ์ ์ธ์ฆ์ด ์๋ฃ๋๋ค๋ฉด ๋ค์ ํํฐ๋ค์ ์ธ์ฆ์ด ์๋ฃ๋ ์ ์ ์ ๋ณด(SecurityContext)๋ฅผ ๊ทธ๋๋ก ์ฌ์ฉํ๋ค.
๋ฐ๋ผ์ ์ธ์ฆ์ด ์๋ฃ๋ฌ๋ค๋ฉด SecurityContext ์์ฑ & Authentication ๊ฐ์ฒด๊ฐ ์ ์ฅ๋๋ค.
(3) ์ธ์ฆ(Authentication)
1) AuthenticationManager๊ฐ ์ง์ง๋ก ๋ก๊ทธ์ธ์ ๊ฒ์ฆ
๋ก๊ทธ์ธ ์๋(์์ด๋/๋น๋ฐ๋ฒํธ ์ธ์ฆ)๊ฐ ๋ค์ด์ค๋ฉด AuthenticationManager๊ฐ ๋ฑ์ฅ์์ ๋ค์์ ์ํํ๋ค.
- ์ ์ ์ ๋ณด(loadUserByUsername) ๊ฐ์ ธ์ค๊ธฐ
- ๋น๋ฐ๋ฒํธ ํ์ธ
- ์ฑ๊ณตํ๋ฉด Authentication ๊ฐ์ฒด ์์ฑ
- SecurityContext์ ์ ์ฅ
์ด๊ฑธ๋ก "๋ก๊ทธ์ธ ๋จ" ์ํ๊ฐ ๋ง๋ค์ด์ง๋ค.
2) SecurityContext์ ์ฌ์ฉ์ ์ ๋ณด ์ ์ฅ
SecurityContext๋ "์ด๋ฒ ์์ฒญ์์ ์ธ์ฆ๋ ์ ์ ์ ๋ณด ์ ์ฅ์"์ด๋ค.
- ์ธ์ฆ์ ์ฑ๊ณตํ๋ฉด Authentication ๊ฐ์ฒด๋ฅผ ์ ์ฅ
- ์ปจํธ๋กค๋ฌ์์ @AuthenticationPrincipal ๊ฐ์ ๊ฒ์ ์ฐ๋ฉด ๊ทธ ์ ๋ณด๋ฅผ ์ฝ์ ์ ์๋ค.
- ์์ฒญ์ด ๋๋๋ฉด ์ฌ๋ผ์ง๋ค.
์ฆ, ํ ๋ฒ์ ์์ฒญ ๋์๋ง ๋ก๊ทธ์ธ ์ํ๋ฅผ ์ ์งํ๋ ์์ ๋ฉ๋ชจ์ฅ๊ณผ ๊ฐ์ ๊ฒ์ด๋ค.
+ ๋ด ์ฝ๋๋ฅผ ๋ฐํ์ผ๋ก ํ Spring Security์ด์ ํ๋ฆ
| ๋จ๊ณ | ์ฃผ์ฒด | ๋์ | ๊ฒฐ๊ณผ |
| 1. ์์ฒญ ์์ฑ | Client | ์ฌ์ฉ์ ์ด๋ฆ๊ณผ ๋น๋ฐ๋ฒํธ๋ฅผ ๋ด์ ๋ก๊ทธ์ธ ์์ฒญ(/login) | LoginRequestDTO |
| 2. ์ธ์ฆ ์์ฒญ | AuthService | LoginRequest๋ฅผ ๊ธฐ๋ฐ์ ์ธ์ฆ ์ ํ ํฐ(UsernamePasswordAuthenticatoinToken) ์์ฑ | Authentication ๊ฐ์ฒด(Unauthenticated) |
| 3. ์ธ์ฆ ์์ | LoginHandler | ํ ํฐ์ AuthenticationManager์๊ฒ ์ ๋ฌ | |
| 4. ์ธ์ฆ ์ํ | AuthenticationManager | ์์ฒญ์ ์ฒ๋ฆฌํ ์ ์๋ AuthenticationProvider์๊ฒ ์์ | |
| 5. ์ฌ์ฉ์ ๋ก๋ | AuthenticationProvider | UserDetailService๋ฅผ ํตํด DB์์ ์ฌ์ฉ์ ์ ๋ณด(UserDetails) ๋ก๋ | |
| 6. ๊ฒ์ฆ | AuthenticationProvider | PasswordEncoder๋ก ๋น๋ฐ๋ฒํธ ๋น๊ต | |
| 7. ์ฑ๊ณต, ์คํจ | AuthenticationProvider | ์ฑ๊ณต ์ ๊ถํ์ด ๋ด๊ธด ์ธ์ฆ ์๋ฃ ํ ํฐ ๋ฐํ | Authentication ๊ฐ์ฒด(Authenticated) |
| 7. ์ปจํ ์คํธ ์ ์ฅ | AuthService | ์ธ์ฆ ์๋ฃ๋ Authentication ๊ฐ์ฒด๋ฅผ SecurityContextHandler์ ์ ์ฅ | ์ธ์ฆ ์ ๋ณด ์ ์ง |
(4) ์ธ๊ฐ(Authorizatoin) - ๊ถํ ์ฒดํฌ
ํํฐ ์ฒด์ธ์ ๋ง์ง๋ง ์ฏค์์ FilterSecurityInterceptor๊ฐ "์ด ์ ์ ๊ฐ ์ด URL์ ์ ๊ทผํ ๊ถํ์ด ์๋๊ฐ?"๋ฅผ ์ฒดํฌํ๋ค.
- ROLE_USER๋ /mypage ๊ฐ๋ฅ
- ROLE_ADMIN์ /admin ๊ฐ๋ฅ
๊ถํ์ด ์์ผ๋ฉด 403 Forbidden.
์ฃผ์!
FilterSecurityInterceptor๊ณผ ์ผ๋ฐ HandlerInterceptor๋ฅผ ํท๊ฐ๋ฆฌ์ง ๋ง๊ฒ.
• FilterSecurityInterceptor : ์ธ์ฆ ๋ฐ ์ธ๊ฐ ๋จ๊ณ์์ ์คํ ๋ฐ ์๋ฃ
• ์ผ๋ฐ HandlerInterceptor : DispatcherServlet์ด ํธ๋ค๋ฌ๋ฅผ ์ฐพ์ ์งํ, ์ปจํธ๋กค๋ฌ ์คํ '์ง์ '์ ๋์ํจ

3. Spring MVC ์ ์ฒด ์์ฒญ ํ๋ฆ
Client → Filter → DispatcherServlet → Interceptor → Controller
๋ง๋ค. ์ด๊ฒ ๋ด๊ฐ ์๋ ๊ณต๋ถํ๋ Spring MVC ํจํด์ด๋ค. ์ฌ๊ธฐ์ ๋์ ํด์ ์ค๋ช ํ ๊ฑฐ๋ค.
- ํด๋ผ์ด์ธํธ๊ฐ ์์ฒญ์ ๋ณด๋
- "์๋ธ๋ฆฟ ํํฐ"๊ฐ ๋จผ์ ์คํ๋จ ← ์ฌ๊ธฐ์ ์์์ ์ค๋ช ํ Spring Security Filter chain๋ค ์์ด์๐
- DispatcherServlet์ด ์์ฒญ์ ์ก์
- Interceptor๊ฐ ๋์
- Controller ์คํ
- ์๋ต ๋ฐํ...
์ฆ ์์ ๋งํ Spring Security์ ํ๋ฆ์ "Filer Chain"์ ๋ด๋ถ ๋ชจ์ต์ ํ๋ํด์ ๋ณธ ๊ฒ์ด๋ผ ๋ด๋ ๋ฌด๋ฐฉํ๋ค.
DispatcherServlet์ด๋? (๋ณต์ต ๊ฒธ)

์น ์์ฒญ์ด ๋ค์ด์ค๋ฉด Spring MVC๋ ๊ทธ๊ฑธ ์ด๋ ์ปจํธ๋กค๋ฌ์์ ์ฒ๋ฆฌํด์ผ ํ๋์ง ์ฐ๊ฒฐํด์ผ ํ๋๋ฐ, ๊ทธ ํ๋ฆ์ ์กฐ์ข ํ๋ ์น๊ตฌ๊ฐ DispatcherServlet์ด๋ค.
- Filter → ํ๊ต ์ ๋ฌธ ๊ฒฝ๋น ์์ ์จ
- DispatcherServlet → ๊ต๋ฌด์ค์์ “์ด ํ์ ์ด๋ ๋ฐ์ผ๋ก ๋ณด๋ด์ผ ํ์ง?” ์ ๋ฆฌํ๋ ๊ต์ฌ
- Controller → ์ค์ ์์ (๋น์ฆ๋์ค ๋ก์ง)์ ๋ด๋นํ๋ ๋ฐ
MVC ์์ฒญ ํ๋ฆ์์ DispatcherServlet์ ์ค์ฌ ํ๋ธ์ด๋ค.
DispatcherServlet์ ์ญํ ์ ๋ค์๊ณผ ๊ฐ๋ค.
- URL์ ๋ณด๊ณ ์ด๋ค Controller์ ํธ์ถํด์ผ ํ๋์ง ๊ฒฐ์
- Controller ์คํ
- Controller๊ฐ ๋ฐํ ๊ฐ์ ViewResolver๋ก ์ ๋ฌ
- ์ต์ข JSON, HTML์ ๋ง๋ค์ด์ ์๋ต ์์ฑ
์ฐธ๊ณ ๋ก Dispatch๋ ๊ธํํ๋ค, ํ๊ฒฌํ๋ค์ ์๋ฏธ๋ฅผ ๊ฐ์ง๊ณ ์์ผ๋ฉฐ ๋ฐ์ ์์ฒญ์ ์ด๋๊ฐ๋ก ๋นจ๋ฆฌ ๋ณด๋ด๋ ์๋ธ๋ฆฟ์ด๋ผ๋ ๋ป์ด๋ค.
Spring์ด ์๋ Java ๋ฐํ์์ ์ปจํธ๋กค๋ฌ๊ฐ ์กด์ฌํ์ง ์๊ธฐ ๋๋ฌธ์ ์๋ธ๋ฆฟ ๊ฐ์ฒด๋ฅผ ์์ฑํ๊ณ ๊ทธ๊ฒ์ web.xml์ ์ผ์ผํ ๋ค ๋ฑ๋กํด์ผ ํ๋๋ฐ, DispatcherServlet์ ์ด๋ฌํ ๋จ์ ์ ํด๊ฒฐํด์ค๋ค.
[์ฐธ๊ณ ์๋ฃ]
https://mangkyu.tistory.com/76
[SpringBoot] Spring Security๋?
๋๋ถ๋ถ์ ์์คํ ์์๋ ํ์์ ๊ด๋ฆฌ๋ฅผ ํ๊ณ ์๊ณ , ๊ทธ์ ๋ฐ๋ฅธ ์ธ์ฆ(Authentication)๊ณผ ์ธ๊ฐ(Authorization)์ ๋ํ ์ฒ๋ฆฌ๋ฅผ ํด์ฃผ์ด์ผ ํ๋ค. Spring์์๋ Spring Security๋ผ๋ ๋ณ๋์ ํ๋ ์์ํฌ์์ ๊ด๋ จ๋ ๊ธฐ๋ฅ
mangkyu.tistory.com
[Spring Security] Spring Security์ JWT ์ ์ฉ ๊ณผ์
์ฌ์ด๋ ํ๋ก์ ํธ๋ฅผ ์งํํ๋ฉด์ Spring Security์ JWT๋ฅผ ์ ์ฉํ์ฌ ํ์ ๊ธฐ๋ฅ์ ์์ฑํ๋ ์ญํ ์ ๋งก๊ฒ ๋์๋ค.JWT๋ ํ๋ก์ ํธ์ ์ ์ฉ์์ผ๋ณธ ๊ฒฝํ์ด ์์ผ๋ Spring Security๋ ์ฒ์์ด์๊ณ Spring Security์ JWT
prao.tistory.com
https://jiwon.oopy.io/1cfde91b-5fd3-4851-9419-9045d971d2cf
Spring Security ๊ตฌ์กฐ ์ดํดํ๊ธฐ
Spring Security๋?
jiwon.oopy.io
https://velog.io/@seculoper235/2.-DispatcherServlet-%EC%9D%B4%EB%9E%80
2. DispatcherServlet ์ด๋?
๋๋์ด ์์๋๋ DispatcherServlet!ํด๋น ํด๋์ค๋ Servlet์ ์ค์ฌ์ด์, Spring MVC์ ์ค์ฌ์ด๋ค.์ด๊ฒ์ ์ดํดํ๋ ๊ฒ์ด ๊ณง Spring MVC๋ฅผ ์ดํดํ๋ ๊ฒ๊ณผ ๊ฐ๋ค๊ณ ํ ์ ์์ ์ ๋๋ก ์ค์ํ ์์์ด๋ค : )๊ทธ๋ผ ํ๋ฒ
velog.io

'Backend๐ฅ๏ธ > Javaโ(Spring๐)' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| [Spring] Spring Security - application.yml๋ก URL๊ถํ์ ๋์ ์ผ๋ก ๊ด๋ฆฌํ๊ธฐ (0) | 2025.10.20 |
|---|---|
| [Java]API ํธ์ถํด์ ๋ฌธ์ ๋ณด๋ด๊ธฐ๐จ (4) | 2025.06.12 |
| [Java] length, length(), size()์ ์ฐจ์ด (2) | 2024.12.15 |