springSecurityConcepts

์Šคํ”„๋ง ์‹œํ๋ฆฌํ‹ฐ ์ฃผ์š” ๊ฐœ๋… ์ •๋ฆฌ

  1. ์ธ์ฆ์„ ์ฒ˜๋ฆฌํ•˜๋Š” AuthenticationManager๋Š” authenticate๋ผ๋Š” ํ•˜๋‚˜์˜ ๋ฉ”์†Œ๋“œ๋งŒ์„ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, formLogin ์ง„ํ–‰ ์‹œ ์•„์ด๋””์™€ ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์ด ๋ฉ”์†Œ๋“œ๋ฅผ ํ†ตํ•ด ์ฒ˜๋ฆฌํ•œ๋‹ค.

  2. authenticateํ•จ์ˆ˜์— Authentication๊ฐ์ฒด๋ฅผ ์ „๋‹ฌํ•˜๊ณ  Authentication๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š”๋ฐ ์ธ์ฆ๋˜๊ธฐ ์ „์—๋Š” Authentication์˜ ํ•„๋“œ์ธ isAuthenticated๊ฐ€ false์ธ ์ƒํƒœ์ด๊ณ , ํ•จ์ˆ˜ ์‹คํ–‰ ํ›„์— ์ธ์ฆ์ด ์™„๋ฃŒ๋˜๋ฉด isAuthenticated๋Š” true๊ฐ€ ๋œ๋‹ค.

  3. AuthenticationManager ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•œ ๊ตฌํ˜„์ฒด๋Š” ์ผ๋ฐ˜์ ์œผ๋กœ ProviderManager์ด๋‹ค. AuthenticationManager์™€ ๊ธฐ๋Šฅ์ด ์œ ์‚ฌํ•˜์ง€๋งŒ ๋‹ค์–‘ํ•œ Authenticationํƒ€์ž…์„ ์ง€์›ํ•˜๋Š” supports๋ฉ”์†Œ๋“œ๊ฐ€ ์ถ”๊ฐ€๋˜์–ด ์žˆ๋‹ค.(ProviderManager -> AuthenticationProvider -> AuthenticationManager)

  4. ProviderManager๋Š” ์„ ํƒ์ ์œผ๋กœ parent๋ฅผ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋Š”๋ฐ ์ด parent๋Š” ๋ชจ๋“  supports๋ฉ”์†Œ๋“œ์—์„œ ํƒ€์ž… ์ง€์›์„ ์ฒดํฌํ–ˆ์Œ์—๋„ ์‹คํŒจํ•  ์‹œ fallback์ฒ˜๋Ÿผ ๋™์ž‘ํ•œ๋‹ค.

  5. ์ธ๊ฐ€์˜ ๊ถŒํ•œ์„ ํŒ๋‹จํ•˜๊ธฐ ์œ„ํ•ด์„œ AccessDecisionVoter๋ฅผ ์‚ฌ์šฉํ•˜๋Š”๋ฐ, vote๋ฉ”์†Œ๋“œ์—์„œ authentication๊ฐ์ฒด์™€ ์ ‘๊ทผ ๋Œ€์ƒ์ธ Object, ์ ‘๊ทผ ๊ถŒํ•œ์ธ ConfigAttribute ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๋„˜๊ธด๋‹ค. ์ดํ›„ ๋ฉ”์†Œ๋“œ๊ฐ€ ์‹คํ–‰๋˜๋ฉด vote์—ฌ๋ถ€(๊ถŒํ•œ ์ธ๊ฐ€ ์„ฑ๊ณต ์—ฌ๋ถ€)๋ฅผ int๋กœ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

  6. default AccessDecisionManager๋Š” ํ•œ ๊ฐœ์˜ voter๋ผ๋„ affirmํ•˜๋ฉด ํ•ด๋‹น ์ž์›์— ์•ก์„ธ์Šค๋ฅผ ํ—ˆ์šฉํ•œ๋‹ค.

  1. DelegatingFilterProxy๋Š” ์„œ๋ธ”๋ฆฟ ์ปจํ…Œ์ด๋„ˆ์˜ lifecycle๊ณผ Springโ€™s ApplicationContext๋ฅผ ์ž‡๋Š” ์—ญํ• ์„ ํ•œ๋‹ค. ์„œ๋ธ”๋ฆฟ ํ•„ํ„ฐ๋Š” ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ์˜ bean๋“ค์„ ์ธ์‹ํ•  ์ˆ˜ ์—†์œผ๋ฏ€๋กœ bean์ธ FilterChainProxy์— ๋ชจ๋“  ์ž‘์—…์„ ์œ„์ž„ํ•œ๋‹ค.

  2. ์„œ๋ธ”๋ฆฟ ์ปจํ…Œ์ด๋„ˆ์˜ DelegatingFilterProxy์— ์˜ํ•ด ์—ญํ• ์„ ์œ„์ž„๋ฐ›๋Š” FilterChainProxy๋Š” ์ผ๋ฐ˜์ ์œผ๋กœ bean์ด๋‹ค.

  3. FilterChainProxy๋Š” ์Šคํ”„๋ง ์‹œํ๋ฆฌํ‹ฐ์˜ ์„œ๋ธ”๋ฆฟ ์ง€์›์˜ ์‹œ์ž‘์ ์ด๋‹ค. ๋”ฐ๋ผ์„œ ๋””๋ฒ„๊ทธํ•˜๊ธฐ ์ข‹์€ ์œ„์น˜์ด๋‹ค.

  4. FilterChainProxy๋Š” ์Šคํ”„๋ง ์‹œํ๋ฆฌํ‹ฐ์˜ ํ•ต์‹ฌ์ด๋‹ค. ๋”ฐ๋ผ์„œ ์˜ต์…˜์œผ๋กœ ์ ์šฉ๋˜์ง€ ์•Š๋Š” ํ•„์ˆ˜์ ์ธ ๊ธฐ๋Šฅ์„ ์‹คํ–‰ํ•˜๊ธฐ์— ์ข‹์€ ์œ„์น˜์ด๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ๋ฉ”๋ชจ๋ฆฌ ๋ˆ„์ˆ˜๋ฅผ ๋ง‰๊ธฐ ์œ„ํ•ด ThreadLocal๋กœ ์‚ฌ์šฉ๋œ SecurityContext๋ฅผ clearํ•˜๋Š” ๊ฒƒ์ด ์žˆ๋‹ค.

  5. ์„œ๋ธ”๋ฆฟ ์ปจํ…Œ์ด๋„ˆ์—์„œ ์„œ๋ธ”๋ฆฟ Filter๋Š” ์˜ค์ง url์— ๊ทผ๊ฑฐํ•ด์„œ ๋ฐœ๋™๋˜์ง€๋งŒ, FilterChainProxy๋Š” HttpServletRequest๋‚ด๋ถ€์˜ ํ•„๋“œ๋“ค์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์–ด ์„ธ๋ฐ€ํ•œ ๋งค์นญ์ด ๊ฐ€๋Šฅํ•˜๋‹ค. ๋”ฐ๋ผ์„œ FilterChainProxy๋Š” ์–ด๋–ค Security Filter Chain์ด ๋ฐœ๋™๋˜์–ด์•ผ ํ•˜๋Š”์ง€๋ฅผ ๊ฒฐ์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.

  6. ์•„๋ž˜ ๊ทธ๋ฆผ์—์„œ Bean Filter๋ฅผ FilterChainProxy๋ผ๊ณ  ํ•ด์„ํ•  ์ˆ˜ ์žˆ๋‹ค.

image

  1. SecurityContextHolder > SecurityContext > Authentication > principal, credentials, authorities ์ˆœ์œผ๋กœ ๊ฐ์ฒด์˜ ํ•„๋“œ๊ฐ€ ์ •ํ•ด์ง€๋Š” ๊ตฌ์กฐ์—์„œ SecurityContextHolder๋Š” ThreadLocal์„ ์‚ฌ์šฉํ•ด์„œ SecurityContext์— ๊ฐ’์„ ํ• ๋‹นํ•œ๋‹ค. ๋”ฐ๋ผ์„œ ๊ฐ™์€ ์Šค๋ ˆ๋“œ์˜ ๋ฉ”์†Œ๋“œ๋ผ๋ฉด ๊ตณ์ด ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๋„˜๊ธธ ํ•„์š” ์—†์ด ํ•ญ์ƒ availableํ•˜๋‹ค. image

  2. ์Šค๋ ˆ๋“œํ’€์„ ์‚ฌ์šฉํ•˜๋Š” ๊ตฌ์กฐ์—์„œ ๋ณด์•ˆ์„ ์ด์œ ๋กœ ํ•˜๊ฑฐ๋‚˜ ์ •ํ™•ํ•œ ์ฒ˜๋ฆฌ๋ฅผ ์ด์œ ๋กœ ํ•ด์„œ ThreadLocal์ธ SecurityContext๋ฅผ ์‚ฌ์šฉ ํ›„์—๋Š” ํ•ญ์ƒ thread๋ฅผ clearํ•ด์•ผ ํ•˜๋Š”๋ฐ, ์ด๋ฅผ FilterChainProxy์—์„œ ์ฒ˜๋ฆฌํ•ด์ค€๋‹ค.

  3. GrantedAuthority๋Š” ์œ ์ €์—๊ฒŒ ํ—ˆ๋ฝ๋œ ๊ถŒํ•œ์„ ๋‚˜ํƒ€๋‚ธ๋‹ค. ํ•˜๋‚˜์˜ ์—ญํ• ์€ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๊ถŒํ•œ์„ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋‹ค. ๊ถŒํ•œ์€ ๋ณดํ†ต ROLE_ADMINISTRATOR or ROLE_HR_SUPERVISOR์ด๋Ÿฌํ•œ ํ˜•ํƒœ๋กœ ์“ฐ์ธ๋‹ค. GrantedAuthoritys๋Š” ๋ณดํ†ต UserDetailsService์— ์˜ํ•ด load๋œ๋‹ค.

  4. AuthenticationEntryPoint๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ๊ฒƒ์€ ํด๋ผ์ด์–ธํŠธ๋กœ๋ถ€ํ„ฐ credential์„ ์š”์ฒญํ•˜๋Š” ์—ญํ• ์„ ํ•œ๋‹ค.์ด๋Š” ๋กœ๊ทธ์ธ ํŽ˜์ด์ง€๋กœ ๋ฆฌ๋‹ค์ด๋ ‰ํŠธํ•˜๊ฑฐ๋‚˜, WWW-Authenticate header๋กœ ์‘๋‹ตํ•˜๋Š” ๊ฒƒ์ด ๋  ์ˆ˜ ์žˆ๊ฒ ๋‹ค.

  5. AbstractAuthenticationProcessingFilter๋Š” ์œ ์ €์˜ credential์„ ์ธ์ฆํ•˜๋Š” base filter๋กœ ์ž‘๋™ํ•˜๊ณ , ์ „ํ˜•์ ์œผ๋กœ AuthenticationEntryPoint์„ ์‚ฌ์šฉํ•˜์—ฌ credential์„ ์š”์ฒญํ•œ๋‹ค. image

    1. ์œ ์ €๊ฐ€ credentials์„ ์ œ์ถœํ•˜๋ฉด, AbstractAuthenticationProcessingFilter๋Š” HttpServletRequest๋กœ๋ถ€ํ„ฐ ์ธ์ฆ ๊ณผ์ •์„ ๊ฑฐ์น  Authentication๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•œ๋‹ค. ์ƒ์„ฑ๋  Authentication ๊ฐ์ฒด๋Š” AbstractAuthenticationProcessingFilter์˜ ํ•˜์œ„ํด๋ž˜์Šค์— ๋”ฐ๋ผ ๋‹ค๋ฅด๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, For example, UsernamePasswordAuthenticationFilter๋Š” HttpServletRequest๋กœ๋ถ€ํ„ฐ UsernamePasswordAuthenticationToken์„ ์ƒ์„ฑํ•œ๋‹ค.

    2. Authentication๊ฐ์ฒด๋Š” ์ธ์ฆ ๊ณผ์ •์„ ๊ฑฐ์น˜๊ธฐ ์œ„ํ•ด AuthenticationManager๋กœ ์ „๋‹ฌ๋œ๋‹ค. ํ˜„์žฌ isAuthenticated์˜ ์ƒํƒœ๋Š” true๊ฐ€ ์•„๋‹Œ ๊ฒƒ์ด๋‹ค.

    3. ์ธ์ฆ์ด ์‹คํŒจํ•˜๋ฉด 3๋ฒˆ ๊ณผ์ •์„ ๊ฑฐ์น˜๋ฉฐ, SecurityContextHolder๋Š” clear out๋œ๋‹ค.

    4. ์ธ์ฆ์ด ์„ฑ๊ณตํ•˜๋ฉด 4๋ฒˆ ๊ณผ์ •์„ ๊ฑฐ์นœ๋‹ค.

      1. SessionAuthenticationStrategy๊ฐ€ ์ƒˆ๋กœ์šด ๋กœ๊ทธ์ธ์„ ๊ฐ์ง€ํ•œ๋‹ค.

      2. SecurityContextHolder์— Authentication๊ฐ์ฒด๊ฐ€ set๋œ๋‹ค. ๊ทธ ํ›„, SecurityContextPersistenceFilter๊ฐ€ HttpSession์— SecurityContext๋ฅผ ์ €์žฅํ•œ๋‹ค.

      3. ๋ฆฌ๋ฉค๋ฒ„๋ฏธ ๊ธฐ๋Šฅ์„ ํ™œ์„ฑํ™”ํ–ˆ์„ ๊ฒฝ์šฐ RememberMeServices.loginSuccess๊ฐ€ ๋ฐœ๋™๋œ๋‹ค.

      4. ApplicationEventPublisher๋Š” InteractiveAuthenticationSuccessEvent๋ฅผ ๋ฐœํ–‰ํ•œ๋‹ค. 5. AuthenticationSuccessHandler๊ฐ€ ๋™์ž‘ํ•œ๋‹ค.

  1. ์•„๋ž˜๋Š” Form Login์ธ์ฆ ๊ณผ์ •์ด๋‹ค. image

    1. ๋จผ์ € ์œ ์ €๊ฐ€ ์ธ์ฆ๋˜์ง€ ์•Š์€ ์š”์ฒญ์„ /private (์œ ์ €๋Š” ์ด url์— ๋Œ€ํ•ด ๊ถŒํ•œ์ด ์—†์Œ)๋กœ ๋ณด๋‚ธ๋‹ค.

    2. FilterSecurityInterceptor๋Š” ์ธ์ฆ๋˜์ง€ ์•Š์€ ์š”์ฒญ์— ๋Œ€ํ•ด AccessDeniedException์„ throwํ•œ๋‹ค.

    3. ์œ ์ €๊ฐ€ ์ธ์ฆ๋˜์ง€ ์•Š์•˜๊ธฐ ๋•Œ๋ฌธ์—, ExceptionTranslationFilter๋Š” ์„ค์ •๋œ AuthenticationEntryPoint์„ ์‚ฌ์šฉํ•ด ๋กœ๊ทธ์ธ ํŽ˜์ด์ง€๋กœ ์š”์ฒญ์„ ๋ฆฌ๋‹ค์ด๋ ‰ํŠธํ•œ๋‹ค. ๋Œ€๋ถ€๋ถ„์˜ ์ผ€์ด์Šค์—์„œ AuthenticationEntryPoint์€ LoginUrlAuthenticationEntryPoint์˜ ์ธ์Šคํ„ด์Šค๋กœ ๋‚˜ํƒ€๋‚œ๋‹ค.

    4. ๋ธŒ๋ผ์šฐ์ €๋Š” ๋กœ๊ทธ์ธ ํŽ˜์ด์ง€๋กœ get์š”์ฒญ์„ ๋ณด๋‚ธ๋‹ค.

    5. ์„œ๋ฒ„์—์„œ ๋กœ๊ทธ์ธ ํŽ˜์ด์ง€๋ฅผ ๋‚ด๋ ค์ค€๋‹ค.

  2. ์•„๋ž˜๋Š” username & password ์ธ์ฆ๊ณผ์ •์ด๋‹ค. Servlet Applications - Authentication - Authentication Architecture ์—์„œ ์‚ดํŽด๋ดค๋˜ AbstractAuthenticationProcessingFilter์˜ ์ธ์ฆ๊ณผ์ •๊ณผ ์ƒ๋‹นํžˆ ์œ ์‚ฌํ•˜๋ฉฐ, AbstractAuthenticationProcessingFilter์ด UsernamePasswordAuthenticationFilter๋กœ ๋ฐ”๋€Œ๊ณ  Authentication ๊ฐ์ฒด๊ฐ€ UsernamePasswordAuthenticationToken์œผ๋กœ ๋ฐ”๋€Œ์—ˆ๋‹ค. ์—ฌ๊ธฐ์—์„œ UsernamePasswordAuthenticationFilter๋Š” AbstractAuthenticationProcessingFilter์„ ์ƒ์†ํ•œ ํด๋ž˜์Šค์ด๋‹ค. image

  1. UserDetails๋Š” UserDetailsService์— ์˜ํ•ด ๋ฆฌํ„ด๋œ๋‹ค.

  2. DaoAuthenticationProvider๋Š” UserDetails์ด ์œ ํšจํ•œ์ง€ ์ฒดํฌํ•œ ํ›„ Authentication์„ ๋ฆฌํ„ดํ•œ๋‹ค.

  3. UserDetailsService๋Š” username, password, and other attributes๋ฅผ ๋ฐ›๊ธฐ ์œ„ํ•ด DaoAuthenticationProvider์— ์˜ํ•ด ์‚ฌ์šฉ๋œ๋‹ค. Spring Security๋Š” UserDetailsService๋ฅผ ๊ตฌํ˜„ํ•œ in-memory์™€ JDBC์˜ ๋‘ ๊ฐ€์ง€ ๋ฐฉ์‹์˜ ๊ตฌํ˜„์ฒด๋ฅผ ์ œ๊ณตํ•œ๋‹ค.

  4. DaoAuthenticationProvider๋Š” AuthenticationProvider์˜ ๊ตฌํ˜„์ฒด ์ค‘ ํ•˜๋‚˜๋กœ UserDetailsService์™€PasswordEncoder๋ฅผ ์‚ฌ์šฉํ•ด์„œ username๊ณผ password๋ฅผ ์ธ์ฆํ•œ๋‹ค. image

Last updated