BackEnd๐Ÿงต

[Spring Security] ์Šคํ”„๋ง ์‹œํ๋ฆฌํ‹ฐ์˜ ์ธ์ฆ/์ธ๊ฐ€ ์ฒ˜๋ฆฌ ํ๋ฆ„

hae02y 2023. 11. 14. 21:11
๋ฐ˜์‘ํ˜•

๋“ค์–ด๊ฐ€๊ธฐ์ „์—.

2023.11.07 - [Spring๐Ÿ•ธ๏ธ] - [Spring Security] ์Šคํ”„๋ง ์‹œํ๋ฆฌํ‹ฐ ๊ธฐ๋ณธ - 1

์ด์ „์˜ Security ๊ธฐ๋ณธ์„ ํ†ตํ•ด์„œ ์–ด๋– ํ•œ ๋ฐฉ์‹์œผ๋กœ ๋ณด์•ˆ์ž‘์—… ์ฒ˜๋ฆฌํ•˜๋Š”์ง€ ์•Œ์•„๋ณด์•˜๋‹ค. ํ•˜์ง€๋งŒ ๋‚ด๋ถ€์—์„œ ์–ด๋–ค ์‹์œผ๋กœ ์ฒ˜๋ฆฌ๊ฐ€ ๋˜๋Š”์ง€๋Š” ์ž์„ธํ•˜๊ฒŒ ์•Œ์•„๋ณด์ง€ ๋ชปํ–ˆ๋‹ค. ๊ทธ๋Ÿผ ๋‚ด๋ถ€์˜ ํ๋ฆ„์„ ํ•œ๋ฒˆ ์•Œ์•„๋ณด์ž.

์ธ์ฆ vs ์ธ๊ฐ€

์ธ์ฆ(Authentication)

์‚ฌ์ดํŠธ์— ์ ‘๊ทผํ•˜๋Š” ์‚ฌ๋žŒ์ด ๋ˆ„๊ตฌ์ธ์ง€ ์‹œ์Šคํ…œ์ด ์•Œ์•„์•ผ ํ•œ๋‹ค. ์ต๋ช…์‚ฌ์šฉ์ž(anonymous user)๋ฅผ ํ—ˆ์šฉํ•˜๋Š” ๊ฒฝ์šฐ๋„ ์žˆ์ง€๋งŒ, ํŠน์ • ๋ฆฌ์†Œ์Šค์— ์ ‘๊ทผํ•˜๊ฑฐ๋‚˜ ๊ฐœ์ธํ™”๋œ ์‚ฌ์šฉ์„ฑ์„ ๋ณด์žฅ ๋ฐ›๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋ฐ˜๋“œ์‹œ ๋กœ๊ทธ์ธํ•˜๋Š” ๊ณผ์ •์ด ํ•„์š”ํ•˜๋‹ค. ๋กœ๊ทธ์ธ์€ ๋ณดํ†ต username / password ๋ฅผ ์ž…๋ ฅํ•˜๊ณ  ๋กœ๊ทธ์ธํ•˜๋Š” ๊ฒฝ์šฐ์™€ sns ์‚ฌ์ดํŠธ๋ฅผ ํ†ตํ•ด ์ธ์ฆ์„ ๋Œ€๋ฆฌํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ๋‹ค.

  • UsernamePassword ์ธ์ฆ (์„ธ์…˜, ํ† ํฐ ๊ด€๋ฆฌ)
  • Sns ๋กœ๊ทธ์ธ (์†Œ์…œ ๋กœ๊ทธ์ธ) : ์ธ์ฆ ์œ„์ž„

์ธ๊ฐ€ / ๊ถŒํ•œ (Authorization)

์‚ฌ์šฉ์ž๊ฐ€ ๋ˆ„๊ตฌ์ธ์ง€ ์•Œ์•˜๋‹ค๋ฉด ์‚ฌ์ดํŠธ ๊ด€๋ฆฌ์ž ํ˜น์€ ์‹œ์Šคํ…œ์€ ๋กœ๊ทธ์ธํ•œ ์‚ฌ์šฉ์ž๊ฐ€ ์–ด๋–ค ์ผ์„ ํ•  ์ˆ˜ ์žˆ๋Š”์ง€ ๊ถŒํ•œ์„ ์„ค์ •ํ•œ๋‹ค. ๊ถŒํ•œ์€ ํŠน์ • ํŽ˜์ด์ง€์— ์ ‘๊ทผํ•˜๊ฑฐ๋‚˜ ํŠน์ • ๋ฆฌ์†Œ์Šค์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋Š” ๊ถŒํ•œ์—ฌ๋ถ€๋ฅผ ํŒ๋‹จํ•˜๋Š”๋ฐ ์‚ฌ์šฉ๋œ๋‹ค. ๊ฐœ๋ฐœ์ž๋Š” ๊ถŒํ•œ์ด ์žˆ๋Š” ์‚ฌ์šฉ์ž์—๊ฒŒ๋งŒ ํŽ˜์ด์ง€๋‚˜ ๋ฆฌ์†Œ์Šค ์ ‘๊ทผ์„ ํ—ˆ์šฉํ•˜๋„๋ก ์ฝ”๋”ฉํ•ด์•ผ ํ•˜๋Š”๋ฐ, ์ด๋Ÿฐ ์ฝ”๋“œ๋ฅผ ์‰ฝ๊ฒŒ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋„๋ก ํ”„๋ ˆ์ž„์›Œํฌ๋ฅผ ์ œ๊ณตํ•˜๋Š” ๊ฒƒ์ด ์Šคํ”„๋ง ์‹œํ๋ฆฌํ‹ฐ ํ”„๋ ˆ์ž„์›Œํฌ(Spring Security Framework) ์ด๋‹ค.

  • Secured : deprecated
  • PrePostAuthorize
  • AOP

 

์ผ๋ฐ˜์ ์ธ  ์š”์ฒญ์˜ ํ๋ฆ„

์„œ๋ธ”๋ฆฟ ์ปจํ…Œ์ด๋„ˆ

  • ํ†ฐ์บฃ๊ฐ™์€ ์›น ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜(WAS)๋ฅผ ์„œ๋ธ”๋ฆฟ ์ปจํ…Œ์ด๋„ˆ๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค. ์ด๋Ÿฌํ•œ ์›น ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์€ ๊ธฐ๋ณธ์ ์œผ๋กœ ํ•„ํ„ฐ์™€ ์„œ๋ธ”๋ฆฟ์œผ๋กœ ๊ตฌ์„ฑ๋œ๋‹ค.
  • ํ•„ํ„ฐ๋Š” ์ฒด์ธ์ฒ˜๋Ÿผ ์—ฎ์—ฌ์žˆ์–ด ํ•„ํ„ฐ์ฒด์ธ์ด๋ผ๊ณ ๋„ ๋ถˆ๋ฆฐ๋‹ค. ๋ชจ๋“  request๋Š” ์ด๋Ÿฌํ•œ ํ•„ํ„ฐ์ฒด์ธ์„ ๊ฑฐ์ณ์•ผ์ง€ ์„œ๋ธ”๋ฆฟ ์„œ๋น„์Šค์— ๋„์ฐฉํ• ์ˆ˜์žˆ๋‹ค.

๊ทธ๋ฆผ๊ณผ ๊ฐ™์ด DispatcherServlet์— ํด๋ผ์ด์–ธํŠธ์˜ ์š”์ฒญ์ด ์ „๋‹ฌ๋˜๊ธฐ ์ „์— ํ•„ํ„ฐ์ฒด์ธ์ด ๊ตฌ์„ฑ๋˜์–ด์žˆ๋‹ค. ์„œ๋ธ”๋ฆฟ ํ•„ํ„ฐ๋Š” ๊ฐ๊ฐ์˜ ํ•„ํ„ฐ๋“ค์ด doFilter() ๋ผ๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ๊ตฌํ˜„ํ•ด์•ผํ•œ๋‹ค. doFilter() ๋ฉ”์„œ๋“œ์˜ ํ˜ธ์ถœ์„ ํ†ตํ•ด ํ•„ํ„ฐ์ฒด์ธ์„ ํ˜•์„ฑํ•œ๋‹ค. 

 

 

Spring Security ์š”์ฒญ์˜ ํ๋ฆ„

์Šคํ”„๋ง ์‹œํ๋ฆฌํ‹ฐ์—์„œ์˜ ์ ์šฉ

๊ทธ๋ฆผ์„ ๋จผ์ € ์‚ดํŽด๋ณด์ž. ์‹œํ๋ฆฌํ‹ฐ๋Š” DelegatingFiterProxy ๋ผ๋Š” ํ•„ํ„ฐ๋ฅผ ๋งŒ๋“ค์–ด ๋ฉ”์ธ ํ•„ํ„ฐ์ฒด์ธ์— ๋ผ์›Œ๋„ฃ๊ณ , ๊ทธ์•„๋ž˜์— ๋‹ค์‹œ SecurityFilterChain ๊ทธ๋ฃน์„ ๋“ฑ๋กํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ๋™์ž‘ํ•œ๋‹ค.

  • ์ด๋Ÿฌํ•œ ํ•„ํ„ฐ์ฒด์ธ์€ ๋ฐ˜๋“œ์‹œ ํ•œ๊ฐœ ์ด์ƒ์œผ๋กœ ๊ตฌ์„ฑ๋œ๋‹ค.
  • url ํŒจํ„ด์— ๋”ฐ๋ผ ์ ์šฉ๋˜๋Š” ํ•„ํ„ฐ์ฒด์ธ์„ ๋‹ค๋ฅด๊ฒŒ ๊ตฌ์„ฑํ• ์ˆ˜์žˆ๋‹ค.
  • web resource์˜ ๊ฒฝ์šฐ ํŒจํ„ด์„ ๋”ฐ๋ฅด๋”๋ผ๋„ ํ•„ํ„ฐ๋ฅผ ๋ฌด์‹œํ•˜๊ณ  ํ†ต๊ณผ์‹œํ‚ฌ์ˆ˜๋„ ์žˆ๋‹ค.

์•„๋ž˜ ๊ทธ๋ฆผ์œผ๋กœ ์กฐ๊ธˆ ๋” ์ž์„ธํ•˜๊ฒŒ ์‚ดํŽด๋ณด์ž.

์„œ๋ธ”๋ฆฟํ•„ํ„ฐ์— SpringSecurityFilter๊ฐ€ ์ถ”๊ฐ€๋œ ๋ชจ์Šต์ด๋‹ค. ๋นจ๊ฐ„ ์ ์„ ์œผ๋กœ ๋œ ์˜์—ญ์„ ๋ณด๋ฉด 2๊ฐœ์˜ Proxy๊ฐ€ ๋ณด์ธ๋‹ค. DelegatingFilterProxy์™€ FilterChainProxy๋„ Filter ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•˜๊ณ  ์žˆ๋Š” ์„œ๋ธ”๋ฆฟ ํ•„ํ„ฐ์ด๋‹ค. ๊ทธ๋Ÿผ ๋‘๊ฐ€์ง€ ํ•„ํ„ฐ์˜ ์—ญํ• ์„ ์•Œ์•„๋ณด์ž.

 

 

1. DelegatingFilterProxy

  • ๋ณด์•ˆ์— ๊ด€๋ จ๋œ ์ž‘์—…์„ ์ฒ˜๋ฆฌํ•˜์ง€๋Š” ์•Š์Œ
  • ApplicationContext์— Bean์œผ๋กœ ๋“ฑ๋ก๋œ Spring Security์˜ ํ•„ํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์‹œ์ž‘์ 
  • ์„œ๋ธ”๋ฆฟ ์ปจํ…Œ์ด๋„ˆ ์˜์—ญ์˜ ํ•„ํ„ฐ์™€ ApplicationContext์— Bean์œผ๋กœ ๋“ฑ๋ก๋œ ํ•„ํ„ฐ๋“ค์„ ์—ฐ๊ฒฐํ•ด ์ฃผ๋Š” ๋ธŒ๋ฆฌ์ง€ ์—ญํ• 

2. FilterChainProxy

  • ๋ณด์•ˆ์„ ์œ„ํ•œ ์ž‘์—…์„ ์ฒ˜๋ฆฌํ•˜๋Š” ํ•„ํ„ฐ์˜ ๋ชจ์Œ
  • Spring Security์˜ Filter๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ์œ„ํ•œ ์ง„์ž…์ 
  • FiterChainProxy๋ถ€ํ„ฐ ์‹œํ๋ฆฌํ‹ฐ์—์„œ ์ œ๊ณตํ•˜๋Š” ๋ณด์•ˆํ•„ํ„ฐ๋“ค์ด ํ•„์š”ํ•œ ์ž‘์—…์„ ์ˆ˜ํ–‰

์ด๋ ‡๊ฒŒ ๋“ฑ๋ก๋œ FiterChainProxy์—์„œ ์‚ฌ์šฉํ• ์ˆ˜์žˆ๋Š” ํ•„ํ„ฐ์˜ ์ข…๋ฅ˜๋Š” ์•„๋ž˜์—์„œ ์‚ดํŽด๋ณด์ž.

 

 

Spring Security ํ•„ํ„ฐ์˜ ์ข…๋ฅ˜

๋ชจ๋“  ํ•„ํ„ฐ๋“ค์€ ๊ฐ๊ธฐ ์ž์‹ ์˜ ์—ญํ• ์ด ์žˆ๋‹ค. ์ด๋Ÿฐ ์—ญํ• ์„ ์ •๋ฆฌํ•ด์„œ ์•Œ์•„๋ณด์ž.

 

  • HeaderWriterFilter : Http ํ•ด๋”๋ฅผ ๊ฒ€์‚ฌํ•œ๋‹ค. ์จ์•ผ ํ•  ๊ฑด ์ž˜ ์จ์žˆ๋Š”์ง€, ํ•„์š”ํ•œ ํ•ด๋”๋ฅผ ๋”ํ•ด์ค˜์•ผ ํ•  ๊ฑด ์—†๋Š”๊ฐ€?
  • CorsFilter : ํ—ˆ๊ฐ€๋œ ์‚ฌ์ดํŠธ๋‚˜ ํด๋ผ์ด์–ธํŠธ์˜ ์š”์ฒญ์ธ๊ฐ€?
  • CsrfFilter : ๋‚ด๊ฐ€ ๋‚ด๋ณด๋‚ธ ๋ฆฌ์†Œ์Šค์—์„œ ์˜ฌ๋ผ์˜จ ์š”์ฒญ์ธ๊ฐ€?
  • LogoutFilter : ์ง€๊ธˆ ๋กœ๊ทธ์•„์›ƒํ•˜๊ฒ ๋‹ค๊ณ  ํ•˜๋Š”๊ฑด๊ฐ€?
  • UsernamePasswordAuthenticationFilter : username / password ๋กœ ๋กœ๊ทธ์ธ์„ ํ•˜๋ ค๊ณ  ํ•˜๋Š”๊ฐ€? ๋งŒ์•ฝ ๋กœ๊ทธ์ธ์ด๋ฉด ์—ฌ๊ธฐ์„œ ์ฒ˜๋ฆฌํ•˜๊ณ  ๊ฐ€์•ผ ํ•  ํŽ˜์ด์ง€๋กœ ๋ณด๋‚ด ์ค„๊ป˜.
  • ConcurrentSessionFilter : ์—ฌ๊ฑฐ์ €๊ธฐ์„œ ๋กœ๊ทธ์ธ ํ•˜๋Š”๊ฑธ ํ—ˆ์šฉํ•  ๊ฒƒ์ธ๊ฐ€?
  • BearerTokenAuthenticationFilter : Authorization ํ•ด๋”์— Bearer ํ† ํฐ์ด ์˜ค๋ฉด ์ธ์ฆ ์ฒ˜๋ฆฌ ํ•ด์ค„๊ป˜.
  • BasicAuthenticationFilter : Authorization ํ•ด๋”์— Basic ํ† ํฐ์„ ์ฃผ๋ฉด ๊ฒ€์‚ฌํ•ด์„œ ์ธ์ฆ์ฒ˜๋ฆฌ ํ•ด์ค„๊ป˜.
  • RequestCacheAwareFilter : ๋ฐฉ๊ธˆ ์š”์ฒญํ•œ request ์ด๋ ฅ์ด ๋‹ค์Œ์— ํ•„์š”ํ•  ์ˆ˜ ์žˆ์œผ๋‹ˆ ์บ์‹œ์— ๋‹ด์•„๋†“์„๊ป˜.
  • SecurityContextHolderAwareRequestFilter : ๋ณด์•ˆ ๊ด€๋ จ Servlet 3 ์ŠคํŽ™์„ ์ง€์›ํ•˜๊ธฐ ์œ„ํ•œ ํ•„ํ„ฐ๋ผ๊ณ  ํ•œ๋‹ค.(?)
  • RememberMeAuthenticationFilter : ์•„์ง Authentication ์ธ์ฆ์ด ์•ˆ๋œ ๊ฒฝ์šฐ๋ผ๋ฉด RememberMe ์ฟ ํ‚ค๋ฅผ ๊ฒ€์‚ฌํ•ด์„œ ์ธ์ฆ ์ฒ˜๋ฆฌํ•ด์ค„๊ป˜
  • AnonymousAuthenticationFilter : ์•„์ง๋„ ์ธ์ฆ์ด ์•ˆ๋˜์—ˆ์œผ๋ฉด ๋„ˆ๋Š” Anonymous ์‚ฌ์šฉ์ž์•ผ
  • SessionManagementFilter : ์„œ๋ฒ„์—์„œ ์ง€์ •ํ•œ ์„ธ์…˜์ •์ฑ…์„ ๊ฒ€์‚ฌํ• ๊ป˜.
  • ExcpetionTranslationFilter : ๋‚˜ ์ดํ›„์— ์ธ์ฆ์ด๋‚˜ ๊ถŒํ•œ ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด ๋‚ด๊ฐ€ ์žก์•„์„œ ์ฒ˜๋ฆฌํ•ด ์ค„๊ป˜.
  • FilterSecurityInterceptor : ์—ฌ๊ธฐ๊นŒ์ง€ ์‚ด์•„์„œ ์™”๋‹ค๋ฉด ์ธ์ฆ์ด ์žˆ๋‹ค๋Š” ๊ฑฐ๋‹ˆ, ๋‹ˆ๊ฐ€ ๋“ค์–ด๊ฐ€๋ ค๊ณ  ํ•˜๋Š” request ์— ๋“ค์–ด๊ฐˆ ์ž๊ฒฉ์ด ์žˆ๋Š”์ง€ ๊ทธ๋ฆฌ๊ณ  ๋ฆฌํ„ดํ•œ ๊ฒฐ๊ณผ๋ฅผ ๋„ˆ์—๊ฒŒ ๋ณด๋‚ด์ค˜๋„ ๋˜๋Š”๊ฑด์ง€ ๋งˆ์ง€๋ง‰์œผ๋กœ ๋‚ด๊ฐ€ ์ ๊ฒ€ํ•ด ์ค„๊ป˜.
  • ๊ทธ ๋ฐ–์—... OAuth2 ๋‚˜ Saml2, Cas, X509 ๋“ฑ์— ๊ด€ํ•œ ํ•„ํ„ฐ๋“ค๋„ ์กด์žฌ.
  • ํ•„ํ„ฐ์˜ ์ˆœ์„œ๋Š” ๊ต‰์žฅํžˆ ์ค‘์š”ํ•˜๋‹ค. ๊ธฐ๋ณธํ•„ํ„ฐ์˜ ์ˆœ์„œ๋Š” ์ •ํ•ด์ ธ์žˆ๊ณ , ๊ทธ์— ๋”ฐ๋ผ ์ž‘์„ฑ ๋˜์•ผ ํ•œ๋‹ค.

์ž ๊ทธ๋Ÿผ ์ด์ œ ์Šคํ”„๋ง ์‹œํ๋ฆฌํ‹ฐ์—์„œ์˜ ์ธ์ฆ์ฒ˜๋ฆฌ ํ๋ฆ„์„ ์•Œ์•„๋ณด์ž!

 

Spring Security ์ธ์ฆ ์ฒ˜๋ฆฌ ํ๋ฆ„

(1) ์‚ฌ์šฉ์ž๊ฐ€ ๋กœ๊ทธ์ธ ํผ์„ ์ด์šฉํ•ด Username, Password๋ฅผ ํฌํ•จํ•œ request๋ฅผ ์‹œํ๋ฆฌํ‹ฐ๊ฐ€ ์ ์šฉ๋œ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์— ์ „์†กํ•œ๋‹ค. ์ฆ‰ ๋กœ๊ทธ์ธ ์š”์ฒญ์ด ๋“ค์–ด์™”์„๋•Œ, Filter๋“ค ์ค‘์— usernamePasswordAuthenticationFilter๊ฐ€ ํ•ด๋‹น ์š”์ฒญ์„ ์ „๋‹ฌ ๋ฐ›๋Š”๋‹ค.

 

(2) ์ด๋•Œ ์‚ฌ์šฉ์ž์˜ ์š”์ฒญ์„ ์ „๋‹ฌ๋ฐ›์€ UsernamePasswordAuthenticationFilter๊ฐ€ Username, Password๋ฅผ ์ด์šฉํ•ด UsernamePasswordAuthenticationToken์„ ์ƒ์„ฑํ•œ๋‹ค. ์ด Token์€ Authentication ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•œ ๊ตฌํ˜„ ํด๋ž˜์Šค์ด๋ฉฐ, ์—ฌ๊ธฐ์—์„œ Authentication์€ ์•„์ง ์ธ์ฆ๋˜์ง€ ์•Š์€ Authentication์ด๋‹ค.

 

(3) ์ธ์ฆ๋˜์ง€ ์•Š์€ Authentication์„ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” UsernamePasswordAuthenticationFilter๊ฐ€ Authentication์„ AuthenticationManager์—๊ฒŒ ์ „๋‹ฌํ•œ๋‹ค. AuthenticationManager๋Š” ์ธ์ฆ์ฒ˜๋ฆฌ๋ฅผ ์ด๊ด„ํ•˜๋Š” ๋งค๋‹ˆ์ ธ์—ญํ• ์„ ํ•˜๋Š” ์ธํ„ฐํŽ˜์ด์Šค๋กœ, AuthenticationManager๋ฅผ ๊ตฌํ˜„ํ•œ ํด๋ž˜์Šค๊ฐ€ ProviderManager์ด๋‹ค. ์ฆ‰ ProviderManager๊ฐ€ ์ธ์ฆ์ด๋ผ๋Š” ์ž‘์—…์„ ์ด๊ด„ํ•˜๋Š” ์‹ค์งˆ์ ์ธ ๋งค๋‹ˆ์ ธ์ธ ๊ฒƒ์ด๋‹ค.

 

(4) ProviderManager๋กœ ๋ถ€ํ„ฐ Authentication์„ AuthenticationProvider์—๊ฒŒ ์ „๋‹ฌํ•œ๋‹ค. ์ด์ฒ˜๋Ÿผ ProviderManager๊ฐ€ ์ง์ ‘ ์ธ์ฆ์„ ์ฒ˜๋ฆฌํ•˜๋Š”๊ฒƒ์ด ์•„๋‹Œ AuthenticationProvider์—๊ฒŒ ์ธ์ฆ์ฒ˜๋ฆฌ๋ฅผ ๋งก๊ธฐ๋Š” ๊ฒƒ์ด๋‹ค.

 

(5) AuthenticationProvider๋Š” UserDetailsService๋ฅผ ์ด์šฉํ•ด UserDetails๋ฅผ ์กฐํšŒํ•œ๋‹ค.

 

(6) ๋ฐ์ดํ„ฐ ๋ฒ ์ด์Šค๋“ฑ์˜ ์ €์žฅ์†Œ์—์„œ ์กฐํšŒํ•œ ์‚ฌ์šฉ์ž์˜ ํฌ๋ ˆ๋ด์…œ์„ ํฌํ•จํ•œ ์‚ฌ์šฉ์ž์˜ ์ •๋ณด๋ฅผ ๊ฐ€์ง€๊ณ  ์˜จ๋‹ค.

 

(7) ๊ฐ€์ง€๊ณ ์˜จ ์‚ฌ์šฉ์ž์˜ ์ •๋ณด๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ UserDeatails๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.

 

(8) ์ƒ์„ฑ๋œ UserDetails๋ฅผ ๋‹ค์‹œ AuthenticationProvider์—๊ฒŒ ์ „๋‹ฌํ•œ๋‹ค.

 

(9) UserDetails๋ฅผ ์ „๋‹ฌ๋ฐ›์€ AuthenticationProvider๋Š” PasswordEncoder๋ฅผ ์ด์šฉํ•ด UserDetails์— ํฌํ•จ๋œ ์•”ํ˜ธํ™”๋œ Password์™€ ์ธ์ฆ์„ ์œ„ํ•œ Authentiation ์•ˆ์— ํฌํ•จ๋œ Password๊ฐ€ ์ผ์น˜ํ•˜๋Š”์ง€ ๊ฒ€์ฆํ•œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๊ฒ€์ฆ์— ์„ฑ๊ณต์„ ํ•˜๋ฉด, UserDetails๋ฅผ ์ด์šฉํ•ด ์ธ์ฆ๋œ Authentication์„ ์ƒ์„ฑํ•œ๋‹ค. ์—ฌ๊ธฐ์„œ ๊ฒ€์ฆ์— ์„ฑ๊ณตํ•˜์ง€ ๋ชปํ•˜๋ฉด Exception์„ ๋ฑ‰๊ณ , ์ธ์ฆ์ฒ˜๋ฆฌ๋ฅผ ์ค‘๋‹จํ•œ๋‹ค.

 

(10) AuthenticationProvider๋Š” ์ธ์ฆ๋œ Authentication์„ ProviderManager์—๊ฒŒ ์ „๋‹ฌํ•œ๋‹ค. ์ด๋•Œ ProviderManager์—๊ฒŒ ์ „๋‹ฌํ•œ Authentication์€ ์ธ์ฆ์— ์„ฑ๊ณตํ•œ ์‚ฌ์šฉ์ž ์ •๋ณด(Principal, Credential, GrantedAuthorities)๋ฅผ ๋‹ด๊ณ ์žˆ๋‹ค.

 

(11) ProviderManager๋Š” ์ธ์ฆ๋œ Authentication์„ ๋‹ค์‹œ UsernamePasswordAuthenticationFilter์—๊ฒŒ ์ „๋‹ฌํ•œ๋‹ค.

 

(12) ๋งˆ์ง€๋ง‰์œผ๋กœ  UsernamePasswordAuthenticationFilter๋Š” SecurityContextHolder๋ฅผ ์ด์šฉํ•ด SecurityContext์— ์ธ์ฆ๋œ Authentication์„ ์ €์žฅํ•œ๋‹ค. 

 

์ดํ›„, SpringSecurity์˜ ์„ธ์…˜์ •์ฑ…์— ๋”ฐ๋ผ์„œ, HttpSession์— ์ €์žฅ๋˜์–ด ์‚ฌ์šฉ์ž์˜ ์ธ์ฆ์ƒํƒœ๋ฅผ ์œ ์ง€ํ•˜๊ธฐ๋„ํ•˜๊ณ ,, HttpSession์„ ์ƒ์„ฑํ•˜์ง€ ์•Š๊ณ  ๋ฌด์ƒํƒœ๋ฅผ ์œ ์ง€ํ•˜๋Š” ๋“ฑ์˜ ์ •์ฑ…์„ ๋„ฃ์–ด์ค„์ˆ˜์žˆ๋‹ค.(JWT ์ฐธ๊ณ )

 

Spring Security์˜ ๊ถŒํ•œ ์ฒ˜๋ฆฌ ํ๋ฆ„

๋‹ค์Œ์œผ๋กœ ๊ถŒํ•œ์ฒ˜๋ฆฌ์˜ ํ๋ฆ„์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด์ž.

๊ถŒํ•œ์ฒ˜๋ฆฌ๋Š” ๋กœ๊ทธ์ธ ๋“ฑ์˜ ์ธ์ฆ์— ์„ฑ๊ณตํ•œ ์ดํ›„์˜ ํ๋ฆ„์ด๋‹ค. ์ธ์ฆ์ด ์„ฑ๊ณตํ•œ ์ดํ›„, ์ธ์ฆ๋œ ์‚ฌ์šฉ์ž์—๊ฒŒ Spring Security์—์„œ ์–ด๋–ป๊ฒŒ ๊ถŒํ•œ์„ ๋ถ€์—ฌํ•˜๋Š”์ง€ ์•Œ์•„๋ณด์ž.

 

Spring Security Filter Chain์—์„œ URL์„ ํ†ตํ•ด ์‚ฌ์šฉ์ž์˜ ์—‘์„ธ์Šค๋ฅผ ์ œํ•œํ•˜๋Š” ๊ถŒํ•œ ๋ถ€์—ฌ Fitler๋Š” AuthorizationFilter์ด๋‹ค.

 

(1) AuthorizationFilter๋Š” SecurityContextHolder๋กœ ๋ถ€ํ„ฐ Authentication์„ ํš๋“ํ•œ๋‹ค. (์ธ์ฆ์ด ์™„๋ฃŒ๋œ Authentication)

 

(2) SecurityContextHolder๋กœ ๋ถ€ํ„ฐ ํš๋“ํ•œ Authentication๊ณผ HttpServletRequest๋ฅผ AuthorizationManager์—๊ฒŒ ์ „๋‹ฌํ•œ๋‹ค. AuthorizationManager๋Š” ๊ถŒํ•œ ๋ถ€์—ฌ ์ฒ˜๋ฆฌ๋ฅผ ์ด๊ด„ํ•˜๋Š” ๋งค๋‹ˆ์ ธ ์—ญํ• ์˜ ์ธํ„ฐํŽ˜์ด์Šค์ด๊ณ , RequestMatcherDelegatingAuthorizationManager๋Š” AuthorizationManager๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ๊ตฌํ˜„์ฒด์ค‘ ํ•˜๋‚˜์ด๋‹ค. RequestMatcherDelegatingAuthorizationManager๋Š” RequestMatcher ํ‰๊ฐ€์‹์„ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•ด๋‹น ํ‰๊ฐ€์‹์— ๋งค์นญ๋˜๋Š” AuthorizationManager์—๊ฒŒ ๊ถŒํ•œ ๋ถ€์—ฌ ์ฒ˜๋ฆฌ๋ฅผ ์œ„์ž„ํ•˜๋Š” ์—ญํ• ์„ ํ•œ๋‹ค. ์ฆ‰, RequestMatcherDelegatingAuthorizationManager๊ฐ€ ์ง์ ‘ ๊ถŒํ•œ์„ ๋ถ€์—ฌํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹Œ, RequestMatcher ๋ฅผ ํ†ตํ•ด ๋งค์นญ๋˜๋Š” AuthorizationManager ๊ตฌํ˜„ ํด๋ž˜์Šค์—๊ฒŒ ์œ„์ž„์„ ํ•œ๋‹ค.

 

(3) RequestMatcherDelegatingAuthorizationManager ๋‚ด๋ถ€์—์„œ ๋งค์นญ๋˜๋Š” AuthorizationManager ๊ตฌํ˜„ ํด๋ž˜์Šค๊ฐ€ ์žˆ์œผ๋ฉด, ํ•ด๋‹น AuthorizationManager ๊ตฌํ˜„ ํด๋ž˜์Šค๊ฐ€ ์‚ฌ์šฉ์ž์˜ ๊ถŒํ•œ์„ ์ฒดํฌํ•œ๋‹ค.

 

(4) ์ ์ ˆํ•œ ๊ถŒํ•œ์ด๋ฉด ๋‹ค์Œ์œผ๋กœ ์š”์ฒญ๋œ ํ”„๋กœ์„ธ์Šค๋กœ ์ด๋™ํ•œ๋‹ค.

 

(5) ์ ์ ˆํ•œ ๊ถŒํ•œ์ด ์•„๋‹ˆ๋ผ๋ฉด, AccessDeniedException์ด throws ๋˜๊ณ , ExceptionTranslationFilter๊ฐ€ AccessDeniedException์„ ์ฒ˜๋ฆฌํ•œ๋‹ค.

 

 

 

 

 

 

๋ฐ˜์‘ํ˜•