๐Ÿ”

Spring Security

Tags
Backend
Date
[ Spring Security Official Docs ] Getting Started
[Spring Blog ] Spring security without the websecurityconfigureradapter
Spring Security 5.7.0-M2 ์—์„œ WebSecurityConfigurerAdapter deprecated ๋จ
์›น ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ์ฃผ์š” ๋ณด์•ˆ ์œ„ํ˜‘ ์š”์†Œ
๋กœ๊ทธ์ธ ๊ธฐ๋Šฅ ๊ตฌํ˜„ ์‹œ ์ฐธ๊ณ ์‚ฌํ•ญ(๊ฒฝ์ผ๋‹˜ ์นœ๊ตฌ๋ถ„ )

1. ๋กœ๊ทธ์ธ

  • Spring Security๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ์ง์ ‘ ์ธ์ฆ/์ธ๊ฐ€ ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ–ˆ์Šต๋‹ˆ๋‹ค.
    • ์ธ์ฆ (Authentication)
      • WAS์— ๋“ค์–ด์˜ค๋Š” ์š”์ฒญ์„ ๊ฒ€์ฆํ•˜๊ธฐ ์œ„ํ•œ AuthorizationInterceptor ๊ตฌํ˜„ํ–ˆ์Šต๋‹ˆ๋‹ค.
        • Http ํ—ค๋”์— ๋“ค์–ด์žˆ๋Š” ์—‘์„ธ์Šค ํ† ํฐ(JWT) ์กด์žฌ ์—ฌ๋ถ€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค.
        • ์˜ฌ๋ฐ”๋ฅธ ์—‘์„ธ์Šค ํ† ํฐ์ธ์ง€ ๊ฒ€์ฆํ•ฉ๋‹ˆ๋‹ค.
    • ์ธ๊ฐ€ (Authorization)
      • ์ปจํŠธ๋กค๋Ÿฌ์— ์ ‘๊ทผํ•˜๊ธฐ ์ „์— ์š”์ฒญํ•œ ์œ ์ €์˜ ๊ถŒํ•œ์„ ๊ฒ€์ฆํ•˜๊ธฐ ์œ„ํ•œ CurrentUserArgumentResolver ๊ตฌํ˜„ํ–ˆ์Šต๋‹ˆ๋‹ค.
        • Http ํ—ค๋”์— ๋“ค์–ด์žˆ๋Š” ์—‘์„ธ์Šค ํ† ํฐ์œผ๋กœ ์œ ์ € ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
        • ์œ ์ € ๊ฐ์ฒด๊ฐ€ ์•Œ๋งž์€ ๊ถŒํ•œ์„ ๊ฐ–๊ณ  ์žˆ๋Š”์ง€ ๊ฒ€์ฆํ•ฉ๋‹ˆ๋‹ค.
  • OAuth ๋กœ๊ทธ์ธ ๊ตฌํ˜„
    • ํด๋ผ์ด์–ธํŠธ์—์„œ OAuth ๊ณต๊ธ‰์ž์™€ ์ธ๊ฐ€ ์ฝ”๋“œ(AuthorizationCode)๊ฐ€ ์š”์ฒญ์œผ๋กœ ์˜ค๋ฉด ๊ณต๊ธ‰์ž์— ์•Œ๋งž๋Š” ์†Œ์…œ ๋กœ๊ทธ์ธ ์„œ๋ฒ„(Authorization Server)์—๊ฒŒ ์—‘์„ธ์Šค ํ† ํฐ์„ ์š”์ฒญํ•ฉ๋‹ˆ๋‹ค.
    • ์ „๋‹ฌ ๋ฐ›์€ ์—‘์„ธ์Šค ํ† ํฐ์œผ๋กœ ์†Œ์…œ ๋กœ๊ทธ์ธ ์„œ๋ฒ„(Resource Server)์—๊ฒŒ ์œ ์ € ์ •๋ณด๋ฅผ ์š”์ฒญํ•ฉ๋‹ˆ๋‹ค.
    • ์ „๋‹ฌ ๋ฐ›์€ ์œ ์ € ์ •๋ณด์—์„œ ํ•„์š”ํ•œ ์ •๋ณด๋ฅผ Redis์— ์ €์žฅํ•œ ๋’ค ์—‘์„ธ์Šค ํ† ํฐ๊ณผ ๋ฆฌํ”„๋ ˆ์‰ฌ ํ† ํฐ์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
    • ์ƒ์„ฑํ•œ ์—‘์„ธ์Šค ํ† ํฐ๊ณผ ๋ฆฌํ”„๋ ˆ์‰ฌ ํ† ํฐ์„ ํด๋ผ์ด์–ธํŠธ์—๊ฒŒ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
  • ๋ฆฌํ”„๋ ˆ์‰ฌ ํ† ํฐ์„ ํ™œ์šฉํ•œ ์ž๋™ ๋กœ๊ทธ์ธ ๊ตฌํ˜„
    • ์‚ฌ์šฉ์ž๊ฐ€ ๋กœ๊ทธ์ธ์„ ํ•˜๋ฉด ์—‘์„ธ์Šค ํ† ํฐ๊ณผ ๋ฆฌํ”„๋ ˆ์‰ฌ ํ† ํฐ์„ ๋ฐœ๊ธ‰ํ•ฉ๋‹ˆ๋‹ค.
    • ์—‘์„ธ์Šค ํ† ํฐ์ด ๋งŒ๋ฃŒ๋˜๋ฉด ๋ฆฌํ”„๋ ˆ์‰ฌ ํ† ํฐ์„ ์‚ฌ์šฉํ•ด ์—‘์„ธ์Šค ํ† ํฐ์„ ๋‹ค์‹œ ๋ฐœ๊ธ‰ํ•ฉ๋‹ˆ๋‹ค.
    • ๋ฆฌํ”„๋ ˆ์‰ฌ ํ† ํฐ์˜ ๋งŒ๋ฃŒ๊ธฐ๊ฐ„์ด 24์‹œ๊ฐ„ ๋ฏธ๋งŒ์œผ๋กœ ๋‚จ์•˜์„ ๋•Œ, ๊ธฐ๊ธฐ ๊ณ ์œ ๋ฒˆํ˜ธ๊ฐ€ ์ผ์น˜ํ•  ๊ฒฝ์šฐ ๋ฆฌํ”„๋ ˆ์‰ฌ ํ† ํฐ์„ ๋‹ค์‹œ ๋ฐœ๊ธ‰ํ•ฉ๋‹ˆ๋‹ค.
  • ์—‘์„ธ์Šค ํ† ํฐ, ๋ฆฌํ”„๋ ˆ์‰ฌ ํ† ํฐ ์ทจ์•ฝ์  ๊ฐœ์„ 
    • ์—‘์„ธ์Šค ํ† ํฐ์ด ํƒˆ์ทจ๋  ์ˆ˜ ์žˆ๋Š” ๋ฌธ์ œ
      • ์—‘์„ธ์Šค ํ† ํฐ์˜ ๋งŒ๋ฃŒ ์‹œ๊ฐ„์„ 3๋ถ„์œผ๋กœ ์„ค์ •ํ•˜์—ฌ ๊ฐœ์„ ํ–ˆ์Šต๋‹ˆ๋‹ค.
    • ๋ฆฌํ”„๋ ˆ์‰ฌ ํ† ํฐ ํƒˆ์ทจ๋  ์ˆ˜ ์žˆ๋Š” ๋ฌธ์ œ
      • ๋กœ๊ทธ์ธ ์‹œ ๊ธฐ๊ธฐ ๊ณ ์œ ๋ฒˆํ˜ธ๋ฅผ ๋ฐ›์•„ Redis์— ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.
      • ๋ฆฌํ”„๋ ˆ์‰ฌ ํ† ํฐ์œผ๋กœ ์ƒˆ๋กœ์šด ์—‘์„ธ์Šค ํ† ํฐ์„ ๋ฐœ๊ธ‰ํ•  ๋•Œ ๊ธฐ๊ธฐ ๊ณ ์œ ๋ฒˆํ˜ธ๋„ ๋ฐ›์•„ Redis์— ์ €์žฅ๋œ ๊ฐ’๊ณผ ๋‹ค๋ฅด๋ฉด ๋‹ค์‹œ ๋กœ๊ทธ์ธํ•˜๋„๋ก ํ•˜์—ฌ ๊ฐœ์„ ํ–ˆ์Šต๋‹ˆ๋‹ค.

์ธ์ฆ


๐Ÿ“–
Spring Security Architecture & Configure
๐Ÿ–ผ๏ธ
Spring Security ํ•ต์‹ฌ(ThreadLocal, SecurityContextHolder, Authentication)
๐Ÿšœ
์ธ์ฆ์ฒ˜๋ฆฌ (AuthenticationManager, AbstractAuthenticationProcessingFilter)
๐Ÿ”‘
PasswordEncoder
๐Ÿ“
๋กœ๊ทธ ์•„์›ƒ ํ•„ํ„ฐ
๐ŸŒ‚
[DB์—์„œ user์ •๋ณด ๊ด€๋ฆฌํ•˜๊ธฐ] UserDetail, UsetDetailsService
๐ŸŒ˜
Custom AuthenticationProvider ์ ์šฉ
SAML ์ธ์ฆ

์ธ๊ฐ€


๐Ÿš›
ExceptionTranslationFilter
๐Ÿ“‘
FilterSecurityInterceptor(์ธ๊ฐ€ ์ฒ˜๋ฆฌ)
๐ŸŒš
Voter
 
endpoint ์ธ๊ฐ€& ์ธ์ฆ ์ ์šฉ ๋ฐฉ๋ฒ•
endpoint ์ธ๊ฐ€& ์ธ์ฆ ์ ์šฉ ๋ฐฉ๋ฒ•

ํ•„ํ„ฐ ์ดํ•ด


๐Ÿš‘
ChannelProcessingFilter (Spring Security์— HTTPS ์ ์šฉํ•˜๊ธฐ)
๐ŸŒ‡
RequestCacheAwareFilter
โ˜€๏ธ
AnonymousAuthenticationFilter
๐Ÿ–‡๏ธ
Custom Filter ์ด์šฉ ( addFilterAt )

๋กœ๊ทธ์ธ ์ง€์† ์ง€์› ํ•„ํ„ฐ

๐Ÿชฆ
RemembermeAuthenticationFilter
์„ธ์…˜ ์ฒ˜๋ฆฌ(SecurityContextPersistenceFilter, SessionManagementFilter)

๊ทธ์™ธ ํ•„ํ„ฐ


๐ŸŽ‡
HeaderWriterFilter
๐Ÿ˜ฎ
CsrfFilter
BasicAuthenticationFilter
๐Ÿช•
WebAsyncManagerIntegrationFilter
 

ํ™œ์šฉ


๐Ÿ›ท
Spring Session, Session Cluster
๐Ÿฅผ
Multi Filter Chain ์ด์šฉ (์›น, ๋ชจ๋ฐ”์ผ ๋™์‹œ ๊ฐœ๋ฐœ)
Spring Security ์ธ์ฆ ์ด๋ฒคํŠธ
JWT ํ† ํฐ
JWT ํ† ํฐ
API Key๋ฅผ ์ด์šฉํ•œ REST API ๋ณด์•ˆ
๐Ÿ’ฟ
@AuthenticationPrincipal
OAuth2
์ธ์ฆ/์ธ๊ฐ€ ์˜ˆ์™ธ ์ฒ˜๋ฆฌ (AuthenticationEntryPoint)