Authentication

์ธ์ฆ ์ •๋ณด๋กœ ์‚ฌ์šฉ์ž์˜ ์ธ์ฆ์ •๋ณด๋ฅผ ์ €์žฅํ•˜๋Š” ํ† ํฐ๊ฐ™์€ ๊ฐœ๋…

์ธ์ฆ์‹œ id/password๋ฅผ ๋‹ด๊ณ  ์ธ์ฆ ๊ฒ€์ฆ์„ ์œ„ํ•ด ์ด๋ฅผ ์ „๋‹ฌ/์‚ฌ์šฉํ•œ๋‹ค.

์ธ์ฆ ํ›„์—๋Š” ์ตœ์ข… ๊ฒฐ๊ณผ(์ธ์ฆ ์ฃผ์ฒด/๊ฐ์ฒด/๋น„๋ฐ€๋ฒˆํ˜ธ/๊ถŒํ•œ์ •๋ณด ๋“ฑ)๋ฅผ ๋‹ด๊ณ  SecurityContext์— ์ €์žฅ๋˜์–ด ์ „์—ญ์œผ๋กœ ์ฐธ์กฐ ๊ฐ€๋Šฅํ•˜๋‹ค.

๊ตฌ์กฐ

  1. principal : ์ธ์ฆ ์ฃผ์ฒด(id๋‚˜ User๊ฐ์ฒด์™€ ๊ฐ™์ด ์‚ฌ์šฉ์ž๋ฅผ ์‹๋ณ„)

  2. credentials : ๋น„๋ฐ€๋ฒˆํ˜ธ

  3. authorities : ๊ถŒํ•œ, ์—ญํ• 

  4. details : ๋ถ€๊ฐ€ ์ •๋ณด

  5. Authenticated : ์ธ์ฆ ์—ฌ๋ถ€(T/F)

๊ณผ์ •

  1. Id/Pw๋ฅผ ์„œ๋ฒ„์— ์ „๋‹ฌ

  2. UsernamePasswordAuthenticationFilter์—์„œ ์ด๋ฅผ ํ•„ํ„ฐ๋ง

  3. ์š”์ฒญํ•œ Id/PW๋ฅผ ๋‹ด์€ Authentication ๊ฐ์ฒด ์ƒ์„ฑ

  4. AuthenticationManager์—๊ฒŒ ์ด ์ธ์ฆ ๊ฐ์ฒด ์ „๋‹ฌ(์ˆ˜์‹ )

  5. Manager๋Š” ์ ํ•ฉํ•œ ์ธ์ฆ๊ฐ์ฒด Provider์—๊ฒŒ ์ธ์ฆ๊ฐ์ฒด ์ „๋‹ฌ

  6. Provider๋Š” ์š”์ฒญ ์ •๋ณด๋ฅผ ์ด์šฉํ•ด ์ธ์ฆ๊ณผ์ • ์ˆ˜ํ–‰

  7. ์ธ์ฆ์ด ์„ฑ๊ณตํ•œ๋‹ค๋ฉด ๊ถŒํ•œ๊ณผ ์ธ์ฆ ์—ฌ๋ถ€๋ฅผ ์…‹ํŒ…ํ•œ ์ธ์ฆ๊ฐ์ฒด๋ฅผ SecurityContext์— ์ €์žฅ

UsernamePasswordAuthenticationFilter

์‚ฌ์šฉ์ž ์ธ์ฆ ์š”์ฒญ์„ Authentication ์ธํ„ฐํŽ˜์ด์Šค๋กœ ์ถ”์ƒํ™”ํ•˜๊ณ , AuthenticationManager๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค.

Authentication

์‹ค์ œ ์‚ฌ์šฉ์ž ์ธ์ฆ ์ •๋ณด๋ฅผ ์˜๋ฏธํ•˜๋Š” ์ธํ„ฐํŽ˜์ด์Šค

  • Principal : ์‚ฌ์šฉ์ž๋ฅผ ๊ตฌ๋ถ„(์ผ์ข…์˜ id) -> ์ ‘๊ทผ ์ฃผ์ฒด

  • Credentials : ์ ‘๊ทผ ์ฃผ์ฒด์˜ ๋น„๋ฐ€๋ฒˆํ˜ธ

  • Authorities : ์‚ฌ์šฉ์ž์˜ ROLE

getPricipal()(์ธ์ฆ ์•„์ด๋””), getCredentials()(์ธ์ฆ ๋น„๋ฐ€๋ฒˆํ˜ธ), getAuthorities()(์‚ฌ์šฉ์ž ๊ถŒํ•œ๋ชฉ๋ก), getDetails()(๋ถ€๊ฐ€์ •๋ณด), isAuthenticated()(์ธ์ฆ์—ฌ๋ถ€)์™€ ๊ฐ™์€ ๋ฉ”์„œ๋“œ ์ œ๊ณต

UsernamePasswordAuthenticationToken authentication= new UsernamePasswordAuthenticationToken(username, password);

AuthenticaitonManager

์‚ฌ์šฉ์ž ์•„์ด๋””/๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์ธ์ฆํ•˜๊ธฐ ์œ„ํ•ด ์ ์ ˆํ•œ AuthenticationProvider ์ฐพ์•„ ์ฒ˜๋ฆฌ๋ฅผ ์œ„์ž„ํ•œ๋‹ค. ๊ตฌํ˜„๊ฐ์ฒด๋กœ ProviderManager๋ฅผ ์ œ๊ณตํ•œ๋‹ค.

(๊ธฐ๋ณธ ProviderMansger์—์„œ Form์ธ์ฆ(DaoAuthenticationProvider)์€ , RememberMe์ธ์ฆ(RememberMeAuthenticationProvider)์„ ์ œ๊ณต )

๊ธฐ๋ณธ์ ์œผ๋กœ ์ œ๊ณตํ•˜๋Š” Provider๊ฐ€ ์—†๋Š” JWT, OAuth์™€ ๊ฐ™์€ ๋ณ„๋„์˜ ์ธ์ฆ์„ ์ถ”๊ฐ€ ํ•˜๊ณ ์ž ํ•˜๋ฉด ๋ถ€๋ชจProviderManager๋ฅผ ํƒ์ƒ‰ํ•ด์„œ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋Š” provider๋ฅผ ์ฐพ๋Š”๋‹ค.

spring ๋ณด์•ˆ ์ธ์ฆ ํ”„๋กœ์„ธ์Šค์˜ ํ•ต์‹ฌ์œผ๋กœ ๋ฐฉ๋ฒ•์„ ์ •์˜ํ•˜๋Š” api

public interface AuthenticationManager {
    Authentication authenticate(Authentication authentication) throwsAuthenticationException;
}

์ปค์Šคํ…€ provider๋ฅผ ๋งŒ๋“ค์—ˆ๋‹ค๋ฉด ๋“ฑ๋กํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค

@Autowired
public void configureAuthentication(AuthenticationManagerBuilder builder, JwtAuthenticationProvider jwtAuthenticationProvider) {
    builder.authenticationProvider(jwtAuthenticationProvider);
}

AuthenticationProvider

์‹ค์งˆ์ ์œผ๋กœ ์‚ฌ์šฉ์ž ์ธ์ฆ์„ ์ฒ˜๋ฆฌํ•˜๊ณ , ์ธ์ฆ ๊ฒฐ๊ณผ๋ฅผ Authentication ์ธํ„ฐํŽ˜์ด์Šค๋กœ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

์ปค์Šคํ…€ provider๋ฅผ ๋งŒ๋“ค๊ณ ์ž ํ•œ๋‹ค๋ฉด ์ด๋ฅผ implementsํ•˜์—ฌ ๊ตฌํ˜„

authenticate()์—์„œ ์‹ค์ œ ์ธ์ฆ์„ ์ฒ˜๋ฆฌํ•˜๊ณ  supports()๋Š” AuthenticationManager์—์„œ ์š”์ฒญ์ธ์ฆ๊ฐ์ฒด์— ์ ํ•ฉํ•จ Provider๋ฅผ ์ฐพ๊ธฐ ์œ„ํ•œ ๋ฉ”์„œ๋“œ์ด๋‹ค.

public interface AuthenticationProvider {
    Authentication authenticate(Authentication authentication) throws AuthenticationException;
    boolean supports(Class<?> authentication);
}

UserDetails

์‚ฌ์šฉ์ž์˜ ์ •๋ณด๋ฅผ ๋‹ด๋Š” ์ธํ„ฐํŽ˜์ด์Šค๋กœ VO์—ญํ• 

  • getAuthorities() : ๊ณ„์ •์ด ๊ฐ–๊ณ ์žˆ๋Š” ๊ถŒํ•œ ๋ชฉ๋ก์„ ๋ฆฌํ„ด

  • getPassword() : ๊ณ„์ •์˜ ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ๋ฆฌํ„ด

  • getUsername() : ๊ณ„์ •์˜ ์ด๋ฆ„์„ ๋ฆฌํ„ด

  • isAccountNonExpired() : ๊ณ„์ •์ด ๋งŒ๋ฃŒ๋˜์ง€ ์•Š์•˜๋Š” ์ง€ ๋ฆฌํ„ด (true: ๋งŒ๋ฃŒ์•ˆ๋จ)

  • isAccountNonLocked() : ๊ณ„์ •์ด ์ž ๊ฒจ์žˆ์ง€ ์•Š์•˜๋Š” ์ง€ ๋ฆฌํ„ด (true: ์ž ๊ธฐ์ง€ ์•Š์Œ)

  • isCredentialNonExpired() : ๋น„๋ฐ€๋ฒˆํ˜ธ๊ฐ€ ๋งŒ๋ฃŒ๋˜์ง€ ์•Š์•˜๋Š” ์ง€ ๋ฆฌํ„ด (true: ๋งŒ๋ฃŒ์•ˆ๋จ)

  • isEnabled() : ๊ณ„์ •์ด ํ™œ์„ฑํ™”(์‚ฌ์šฉ๊ฐ€๋Šฅ)์ธ ์ง€ ๋ฆฌํ„ด (true: ํ™œ์„ฑํ™”)

  • UserDetailsService : DAO๋กœ ์‚ฌ์šฉ๋˜๋ฉฐ ์‚ฌ์šฉ์ž ์ •๋ณด๋ฅผ ๋กœ๋“œํ•˜๊ธฐ ์œ„ํ•œ ์ธํ„ฐํŽ˜์ด์Šค๋กœ UserDetails๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•˜๋‚˜์˜ ๋ฉ”์„œ๋“œ๋ฅผ ๊ฐ–๊ณ ์žˆ๋‹ค.

Last updated