springSecurityConcepts
Last updated
Last updated
์ธ์ฆ์ ์ฒ๋ฆฌํ๋ AuthenticationManager
๋ authenticate๋ผ๋ ํ๋์ ๋ฉ์๋๋ง์ ๊ฐ์ง๊ณ ์๋ค. ์๋ฅผ ๋ค์ด, formLogin ์งํ ์ ์์ด๋์ ๋น๋ฐ๋ฒํธ๋ฅผ ์ด ๋ฉ์๋๋ฅผ ํตํด ์ฒ๋ฆฌํ๋ค.
authenticate
ํจ์์ Authentication๊ฐ์ฒด
๋ฅผ ์ ๋ฌํ๊ณ Authentication๊ฐ์ฒด
๋ฅผ ๋ฐํํ๋๋ฐ ์ธ์ฆ๋๊ธฐ ์ ์๋ Authentication
์ ํ๋์ธ isAuthenticated
๊ฐ false์ธ ์ํ์ด๊ณ , ํจ์ ์คํ ํ์ ์ธ์ฆ์ด ์๋ฃ๋๋ฉด isAuthenticated
๋ true๊ฐ ๋๋ค.
AuthenticationManager
์ธํฐํ์ด์ค๋ฅผ ๊ตฌํํ ๊ตฌํ์ฒด๋ ์ผ๋ฐ์ ์ผ๋ก ProviderManager
์ด๋ค. AuthenticationManager
์ ๊ธฐ๋ฅ์ด ์ ์ฌํ์ง๋ง ๋ค์ํ Authentication
ํ์
์ ์ง์ํ๋ supports
๋ฉ์๋๊ฐ ์ถ๊ฐ๋์ด ์๋ค.(ProviderManager -> AuthenticationProvider -> AuthenticationManager)
ProviderManager
๋ ์ ํ์ ์ผ๋ก parent๋ฅผ ๊ฐ์ง ์ ์๋๋ฐ ์ด parent๋ ๋ชจ๋ supports๋ฉ์๋์์ ํ์
์ง์์ ์ฒดํฌํ์์๋ ์คํจํ ์ fallback
์ฒ๋ผ ๋์ํ๋ค.
์ธ๊ฐ์ ๊ถํ์ ํ๋จํ๊ธฐ ์ํด์ AccessDecisionVoter
๋ฅผ ์ฌ์ฉํ๋๋ฐ, vote๋ฉ์๋์์ authentication
๊ฐ์ฒด์ ์ ๊ทผ ๋์์ธ Object
, ์ ๊ทผ ๊ถํ์ธ ConfigAttribute
ํ๋ผ๋ฏธํฐ๋ก ๋๊ธด๋ค. ์ดํ ๋ฉ์๋๊ฐ ์คํ๋๋ฉด vote์ฌ๋ถ(๊ถํ ์ธ๊ฐ ์ฑ๊ณต ์ฌ๋ถ)๋ฅผ int๋ก ๋ฐํํ๋ค.
default AccessDecisionManager
๋ ํ ๊ฐ์ voter๋ผ๋ affirmํ๋ฉด ํด๋น ์์์ ์ก์ธ์ค๋ฅผ ํ์ฉํ๋ค.
DelegatingFilterProxy
๋ ์๋ธ๋ฆฟ ์ปจํ
์ด๋์ lifecycle๊ณผ Springโs ApplicationContext๋ฅผ ์๋ ์ญํ ์ ํ๋ค. ์๋ธ๋ฆฟ ํํฐ๋ ์คํ๋ง ์ปจํ
์ด๋์ bean๋ค์ ์ธ์ํ ์ ์์ผ๋ฏ๋ก bean์ธ FilterChainProxy
์ ๋ชจ๋ ์์
์ ์์ํ๋ค.
์๋ธ๋ฆฟ ์ปจํ
์ด๋์ DelegatingFilterProxy
์ ์ํด ์ญํ ์ ์์๋ฐ๋ FilterChainProxy
๋ ์ผ๋ฐ์ ์ผ๋ก bean์ด๋ค.
FilterChainProxy
๋ ์คํ๋ง ์ํ๋ฆฌํฐ์ ์๋ธ๋ฆฟ ์ง์์ ์์์ ์ด๋ค. ๋ฐ๋ผ์ ๋๋ฒ๊ทธํ๊ธฐ ์ข์ ์์น์ด๋ค.
FilterChainProxy
๋ ์คํ๋ง ์ํ๋ฆฌํฐ์ ํต์ฌ์ด๋ค. ๋ฐ๋ผ์ ์ต์
์ผ๋ก ์ ์ฉ๋์ง ์๋ ํ์์ ์ธ ๊ธฐ๋ฅ์ ์คํํ๊ธฐ์ ์ข์ ์์น์ด๋ค. ์๋ฅผ ๋ค์ด, ๋ฉ๋ชจ๋ฆฌ ๋์๋ฅผ ๋ง๊ธฐ ์ํด ThreadLocal
๋ก ์ฌ์ฉ๋ SecurityContext๋ฅผ clearํ๋ ๊ฒ์ด ์๋ค.
์๋ธ๋ฆฟ ์ปจํ
์ด๋์์ ์๋ธ๋ฆฟ Filter๋ ์ค์ง url
์ ๊ทผ๊ฑฐํด์ ๋ฐ๋๋์ง๋ง, FilterChainProxy
๋ HttpServletRequest
๋ด๋ถ์ ํ๋๋ค์ ์ฌ์ฉํ ์ ์์ด ์ธ๋ฐํ ๋งค์นญ์ด ๊ฐ๋ฅํ๋ค. ๋ฐ๋ผ์ FilterChainProxy
๋ ์ด๋ค Security Filter Chain
์ด ๋ฐ๋๋์ด์ผ ํ๋์ง๋ฅผ ๊ฒฐ์ ํ ์ ์๋ค.
์๋ ๊ทธ๋ฆผ์์ Bean Filter๋ฅผ FilterChainProxy
๋ผ๊ณ ํด์ํ ์ ์๋ค.
์ค๋ ๋ํ์ ์ฌ์ฉํ๋ ๊ตฌ์กฐ์์ ๋ณด์์ ์ด์ ๋ก ํ๊ฑฐ๋ ์ ํํ ์ฒ๋ฆฌ๋ฅผ ์ด์ ๋ก ํด์ ThreadLocal์ธ SecurityContext๋ฅผ ์ฌ์ฉ ํ์๋ ํญ์ thread๋ฅผ clearํด์ผ ํ๋๋ฐ, ์ด๋ฅผ FilterChainProxy
์์ ์ฒ๋ฆฌํด์ค๋ค.
GrantedAuthority
๋ ์ ์ ์๊ฒ ํ๋ฝ๋ ๊ถํ์ ๋ํ๋ธ๋ค. ํ๋์ ์ญํ ์ ์ฌ๋ฌ ๊ฐ์ ๊ถํ์ ๊ฐ์ง ์ ์๋ค. ๊ถํ์ ๋ณดํต ROLE_ADMINISTRATOR
or ROLE_HR_SUPERVISOR
์ด๋ฌํ ํํ๋ก ์ฐ์ธ๋ค. GrantedAuthority
s๋ ๋ณดํต UserDetailsService
์ ์ํด load๋๋ค.
AuthenticationEntryPoint
๋ฅผ ๊ตฌํํ๋ ๊ฒ์ ํด๋ผ์ด์ธํธ๋ก๋ถํฐ credential
์ ์์ฒญํ๋ ์ญํ ์ ํ๋ค.์ด๋ ๋ก๊ทธ์ธ ํ์ด์ง๋ก ๋ฆฌ๋ค์ด๋ ํธํ๊ฑฐ๋, WWW-Authenticate header๋ก ์๋ตํ๋ ๊ฒ์ด ๋ ์ ์๊ฒ ๋ค.
์ ์ ๊ฐ credentials
์ ์ ์ถํ๋ฉด, AbstractAuthenticationProcessingFilter
๋ HttpServletRequest
๋ก๋ถํฐ ์ธ์ฆ ๊ณผ์ ์ ๊ฑฐ์น Authentication
๊ฐ์ฒด๋ฅผ ์์ฑํ๋ค. ์์ฑ๋ Authentication
๊ฐ์ฒด๋ AbstractAuthenticationProcessingFilter
์ ํ์ํด๋์ค์ ๋ฐ๋ผ ๋ค๋ฅด๋ค. ์๋ฅผ ๋ค์ด, For example, UsernamePasswordAuthenticationFilter
๋ HttpServletRequest
๋ก๋ถํฐ UsernamePasswordAuthenticationToken
์ ์์ฑํ๋ค.
Authentication
๊ฐ์ฒด๋ ์ธ์ฆ ๊ณผ์ ์ ๊ฑฐ์น๊ธฐ ์ํด AuthenticationManager
๋ก ์ ๋ฌ๋๋ค. ํ์ฌ isAuthenticated
์ ์ํ๋ true๊ฐ ์๋ ๊ฒ์ด๋ค.
์ธ์ฆ์ด ์คํจํ๋ฉด 3๋ฒ ๊ณผ์ ์ ๊ฑฐ์น๋ฉฐ, SecurityContextHolder
๋ clear out๋๋ค.
์ธ์ฆ์ด ์ฑ๊ณตํ๋ฉด 4๋ฒ ๊ณผ์ ์ ๊ฑฐ์น๋ค.
SessionAuthenticationStrategy
๊ฐ ์๋ก์ด ๋ก๊ทธ์ธ์ ๊ฐ์งํ๋ค.
SecurityContextHolder
์ Authentication
๊ฐ์ฒด๊ฐ set๋๋ค. ๊ทธ ํ, SecurityContextPersistenceFilter
๊ฐ HttpSession
์ SecurityContext
๋ฅผ ์ ์ฅํ๋ค.
๋ฆฌ๋ฉค๋ฒ๋ฏธ ๊ธฐ๋ฅ์ ํ์ฑํํ์ ๊ฒฝ์ฐ RememberMeServices.loginSuccess
๊ฐ ๋ฐ๋๋๋ค.
ApplicationEventPublisher
๋ InteractiveAuthenticationSuccessEvent
๋ฅผ ๋ฐํํ๋ค. 5. AuthenticationSuccessHandler
๊ฐ ๋์ํ๋ค.
๋จผ์ ์ ์ ๊ฐ ์ธ์ฆ๋์ง ์์ ์์ฒญ์ /private (์ ์ ๋ ์ด url์ ๋ํด ๊ถํ์ด ์์)๋ก ๋ณด๋ธ๋ค.
FilterSecurityInterceptor
๋ ์ธ์ฆ๋์ง ์์ ์์ฒญ์ ๋ํด AccessDeniedException
์ throwํ๋ค.
์ ์ ๊ฐ ์ธ์ฆ๋์ง ์์๊ธฐ ๋๋ฌธ์, ExceptionTranslationFilter
๋ ์ค์ ๋ AuthenticationEntryPoint
์ ์ฌ์ฉํด ๋ก๊ทธ์ธ ํ์ด์ง๋ก ์์ฒญ์ ๋ฆฌ๋ค์ด๋ ํธํ๋ค. ๋๋ถ๋ถ์ ์ผ์ด์ค์์ AuthenticationEntryPoint
์ LoginUrlAuthenticationEntryPoint
์ ์ธ์คํด์ค๋ก ๋ํ๋๋ค.
๋ธ๋ผ์ฐ์ ๋ ๋ก๊ทธ์ธ ํ์ด์ง๋ก get์์ฒญ์ ๋ณด๋ธ๋ค.
์๋ฒ์์ ๋ก๊ทธ์ธ ํ์ด์ง๋ฅผ ๋ด๋ ค์ค๋ค.
UserDetails
๋ UserDetailsService
์ ์ํด ๋ฆฌํด๋๋ค.
DaoAuthenticationProvider
๋ UserDetails
์ด ์ ํจํ์ง ์ฒดํฌํ ํ Authentication
์ ๋ฆฌํดํ๋ค.
UserDetailsService
๋ username, password, and other attributes๋ฅผ ๋ฐ๊ธฐ ์ํด DaoAuthenticationProvider
์ ์ํด ์ฌ์ฉ๋๋ค. Spring Security๋ UserDetailsService
๋ฅผ ๊ตฌํํ in-memory์ JDBC์ ๋ ๊ฐ์ง ๋ฐฉ์์ ๊ตฌํ์ฒด๋ฅผ ์ ๊ณตํ๋ค.
SecurityContextHolder > SecurityContext > Authentication > principal, credentials, authorities
์์ผ๋ก ๊ฐ์ฒด์ ํ๋๊ฐ ์ ํด์ง๋ ๊ตฌ์กฐ์์ SecurityContextHolder
๋ ThreadLocal
์ ์ฌ์ฉํด์ SecurityContext
์ ๊ฐ์ ํ ๋นํ๋ค. ๋ฐ๋ผ์ ๊ฐ์ ์ค๋ ๋์ ๋ฉ์๋๋ผ๋ฉด ๊ตณ์ด ํ๋ผ๋ฏธํฐ๋ก ๋๊ธธ ํ์ ์์ด ํญ์ availableํ๋ค.
AbstractAuthenticationProcessingFilter
๋ ์ ์ ์ credential
์ ์ธ์ฆํ๋ base filter๋ก ์๋ํ๊ณ , ์ ํ์ ์ผ๋ก AuthenticationEntryPoint
์ ์ฌ์ฉํ์ฌ credential
์ ์์ฒญํ๋ค.
์๋๋ Form Login์ธ์ฆ ๊ณผ์ ์ด๋ค.
์๋๋ username & password ์ธ์ฆ๊ณผ์ ์ด๋ค. Servlet Applications - Authentication - Authentication Architecture ์์ ์ดํด๋ดค๋ AbstractAuthenticationProcessingFilter
์ ์ธ์ฆ๊ณผ์ ๊ณผ ์๋นํ ์ ์ฌํ๋ฉฐ, AbstractAuthenticationProcessingFilter
์ด UsernamePasswordAuthenticationFilter
๋ก ๋ฐ๋๊ณ Authentication
๊ฐ์ฒด๊ฐ UsernamePasswordAuthenticationToken
์ผ๋ก ๋ฐ๋์๋ค. ์ฌ๊ธฐ์์ UsernamePasswordAuthenticationFilter
๋ AbstractAuthenticationProcessingFilter
์ ์์ํ ํด๋์ค์ด๋ค.
DaoAuthenticationProvider
๋ AuthenticationProvider
์ ๊ตฌํ์ฒด ์ค ํ๋๋ก UserDetailsService
์PasswordEncoder
๋ฅผ ์ฌ์ฉํด์ username๊ณผ password๋ฅผ ์ธ์ฆํ๋ค.