๐ณSpring/๐ฑSpring Security
[Spring Security] UsernamePasswordAuthenticationFilter
junbin2
2025. 5. 7. 05:10
- UsernamePasswordAuthenticationFilter ๋ SecurityFilterChain ์ 3๋ฒ์งธ ํํฐ์ ํด๋นํ๋ ์ธ์ฆ์ ์ฒ๋ฆฌํด์ฃผ๋ ํํฐ์ด๋ค.
โ 1. UsernamePasswordAuthenticationFilter
- ์ ์ ์์ด๋์ ๋น๋ฐ๋ฒํธ๋ฅผ ๋ฐ์ ์ธ์ฆ์ ํด์ฃผ๋ ํํฐ์ด๋ฉฐ, ๋ํดํธ๋ก ๊ตฌํ์ด ๋์ด์๋ ํํฐ์ด๋ค.
- AbstractAuthenticationProcessingFilter ์ถ์ ํด๋์ค๋ฅผ ์์๋ฐ์์ ๊ตฌํํ ํด๋์ค์ด๋ค.
- ์ฝ๊ฒ๋งํด, ํด๋น ํํฐ๋ ๊ตฌํ์ด ๋์ด์์ด์ ๋ฐ๋ก ์ด์ฉ์ด ๊ฐ๋ฅํ์ง๋ง, ๋ณดํต ์์๋ฐ์์ ์ฌ์ ์๋ฅผ ํด์ ์ด์ฉ์ ํ๋ค๋ ์๋ฏธ์ด๋ค.
โ 2. UsernamePasswordAuthenticationFilter ํต์ฌ ๋ฉ์๋
- attemptAuthentication: ์ ์ ์ ์ ๋ณด๋ฅผ ๋ฐ์์ ๊ฒ์ฆ์ ํด์ฃผ๋ ๋ฉ์๋ ( ์์ด๋, ํจ์ค์๋ )
- successfulAuthentication: attemptAuthentication ๋ฉ์๋์์ ๊ฒ์ฆ ์ฑ๊ณต์ ํธ์ถ๋๋ ๋ฉ์๋
- unsuccessfulAuthentication: attemptAuthentication ๋ฉ์๋์์ ๊ฒ์ฆ ์คํจ์ ํธ์ถ๋๋ ๋ฉ์๋
โ 3. attemptAuthentication ๋ฉ์๋
// AbstractAuthenticationProcessingFilter.class
public abstract Authentication attemptAuthentication(
HttpServletRequest request,
HttpServletResponse response)
throws AuthenticationException, IOException, ServletException;
- AbstractAuthenticationProcessingFilter ์ถ์ ํด๋์ค๋ก๋ถํฐ, ์ถ์ ๋ฉ์๋๋ฅผ ์์๋ฐ์ ๋ง๋ค์ด์ง ๋ฉ์๋์.
- ์ธ์ฆ ์ฑ๊ณต์ Authentication ํด๋์ค๋ฅผ ๋ฐํํ๋ฉฐ, ์คํจ์ ์์ธ๋ฅผ ๋์ง๋ค.
- ์ดํ, ์ธ์ฆ ์ฑ๊ณต์ successfulAuthentication ๋ฉ์๋๋ฅผ ํธ์ถ, ์คํจ์ unsucessfulAuthentication ๋ฉ์๋ ์์ธ๋์ง๋ฉฐ ํธ์ถ
- ํด๋น ๋ฉ์๋์ ๋ฐํํ์ ์ Authentication ํด๋์ค์ด๋ฉฐ, ์ด ๋ถ๋ถ์ด ์ ์ผ ์ค์ํ ๋ถ๋ถ์ด๋ค.
โ 4. Authentication
public interface Authentication extends Principal, Serializable {
Collection<? extends GrantedAuthority> getAuthorities();
Object getCredentials();
Object getDetails();
Object getPrincipal();
boolean isAuthenticated();
void setAuthenticated(boolean isAuthenticated) throws IllegalArgumentException;
}
- Authentication ํด๋์ค๋ ์ฌ์ฉ์์ ์ธ์ฆ ์ ๋ณด๋ฅผ ๋ด๋ ์ธํฐํ์ด์ค์ด๋ค.
- ์ฌ๋ฌ ๊ตฌํ์ฒด๊ฐ ์กด์ฌํ๋ฉฐ, ๋ํ์ ์ธ ๊ตฌํ์ฒด๋ก๋ UsernamePasswordAuthenticationToken ํด๋์ค๊ฐ ์๋ค.
- ํด๋น ๊ตฌํ์ฒด ํด๋์ค๋ฅผ ์กฐ๊ฑด์ผ๋ก ์ด๋ค AuthenticationProvider๊ฐ ๋์๋ ์ง๊ฐ ๊ฒฐ์ ์ด ๋๋ค.
โ 5. AuthenticationManager
public interface AuthenticationManager {
Authentication authenticate(Authentication authentication) throws AuthenticationException;
}
- Authentication ์ ๊ตฌํ์ฒด๋ฅผ ๋ฐ์์ ์ธ์ฆ์ ์ฒ๋ฆฌํด์ฃผ๋ ์ ๋ต ์ธํฐํ์ด์ค์ด๋ค.
- Authentication ์ธ์ฆ ์ ๋ณด๋ฅผ ๋ฐ์์ ์ฑ๊ณต์ ๊ฐ์ฒด ๋ฐํ ์คํจ์ ์์ธ๋ฅผ ๋์ง๋ ๋ฐฉ์์ด๋ค.
- ๊ตฌํ์ฒด๋ก๋ ProviderManager ๊ฐ ์๋ค.
โ 6. ProviderManager ( AuthenticationManager ๊ตฌํ์ฒด )
public class ProviderManager implements AuthenticationManager, ... {
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
...
}
}
- AuthenticationManager ์ธํฐํ์ด์ค ๊ตฌํ์ฒด์ด๋ฉฐ, AuthenticationProvider ๊ด๋ฆฌ๋ฅผ ํ๋ ์ญํ ์ ํ๋ค.
- ์ธ์ฆ ์์ฒญ์ ์ ์ ํ AuthenticationProvider ์ ์์์ ํด์ค๋ค.
- ๋ฆฌ์คํธ๋ก ์ฌ๋ฌ Provider๋ฅผ ๊ฐ์ง๊ณ ์์ผ๋ฉฐ, Authentication ๊ตฌํ์ฒด์ ๋ฐ๋ผ์ ๋ง๋ Provider๋ฅผ ๋งค์นญ์์ผ์ค๋ค.
- ์ฆ, ์ฌ๊ธฐ์ AuthenticationProvider ๋ฅผ ํธ์ถํด์, ์ธ์ฆ์ ํ๋ ๊ฒ์ด๋ค.
โ 7. AuthenticationProvider
public interface AuthenticationProvider {
Authentication authenticate(Authentication authentication) throws AuthenticationException;
boolean supports(Class<?> authentication);
}
// ์ถ์ํด๋์ค -> AuthenticationProvider ๊ตฌํ
public abstract class AbstractUserDetailsAuthenticationProvider implements AuthenticationProvider, ... {
...
}
// AbstractUserDetailsAuthenticationProvider ์ถ์ ํด๋์ค ์ง์ ๊ตฌํ
public class DaoAuthenticationProvider extends AbstractUserDetailsAuthenticationProvider {
...
}
- ์ธ์ฆ์ ์ฒ๋ฆฌํ๋ ์ธํฐํ์ด์ค์ด๋ฉฐ, ์ฌ๋ฌ Provider๊ฐ ๊ตฌํํ๊ณ ์๋ค.
- ๋ํ์ ์ธ ๊ตฌํ์ฒด๋ก๋ DaoAuthenticationProvider ํด๋์ค๊ฐ ์์.
- ๊ณ์ธต ๊ตฌ์กฐ๋ก๋ AbstractUserDetailsAuthenticationProvider ์ถ์ํด๋์ค๊ฐ ์์ผ๋ฉฐ,
- AbstractUserDetailsAuthenticationProvider ์ถ์ํด๋์ค๋ฅผ DaoAuthenticationProvider๊ฐ ๊ตฌํํ๊ณ ์์
โ 8. DaoAuthenticationProvider ( AuthenicationProvider ์ต์ข ๊ตฌํ์ฒด )
- AbstractUserDetailsAuthenticationProvider ์ถ์ํด๋์ค๊ฐ AuthenticationProvider๋ฅผ ๊ตฌํํ๊ณ ์์.
- DaoAuthenticationProvider ๋ AbstractUserDetailsAuthenticationProvider ์ถ์ํด๋์ค๋ฅผ ์์ ๋ฐ์์ ๊ตฌํ์ค
- ๋ฐ์ดํฐ๋ฒ ์ด์ค ๊ธฐ๋ฐ ์ฌ์ฉ์ ์ธ์ฆ์ ์ฒ๋ฆฌํ๋ ๊ตฌํ์ฒด์ด๋ค.
- UserDetailsService๋ฅผ ์ฌ์ฉํ์ฌ ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์ ์ฅ๋ ์ฌ์ฉ์ ์ ๋ณด๋ฅผ ์กฐํํ๋ค.
- ๋ด๋ถ์ ์ผ๋ก PasswordEncoder๋ฅผ ์ฌ์ฉํ์ฌ ์ ๋ ฅ ๋น๋ฐ๋ฒํธ์ DB ๋น๋ฐ๋ฒํธ๋ฅผ ๋น๊ตํ์ฌ ์ธ์ฆ์ ์ํํจ.
- ์ฆ, PasswordEncoder๋ก DB์ ๋น๋ฐ๋ฒํธ ์ํธํ๋ฅผ ํด์ ๋ฃ์ด์ค์ผ์ง๋ง ๋์์ด ๋จ.
โ ์ ๋ฆฌ
- Spring Security ๋ ์ด๋ฏธ ํํฐํํ๋ก ๊ตฌํ์ด ๋์ด์์ผ๋ฉฐ, UsernamePasswordAuthenticationFilter๋ ๊ตฌํ๋์ด์์
- ํต์ฌ์ UsernamePasswordAuthenticationFilter ์ attemptAuthentication ๋ฉ์๋๊ฐ ์ธ์ฆ์ ๋ฐ์์ ์ฒ๋ฆฌ๋ฅผ ํ๋ค๋ ๋ถ๋ถ
- ํด๋น UsernamePasswordAuthenticationFilter ๋ฅผ ์์๋ฐ์์ ์ด์ฉํ๋ฉด attemptAuthenication ์ ์ฌ์ ์ ํด์ค์ผํจ.
- attemptAuthentication ๋ฉ์๋์ ๋ฐํ ํ์ ์ Authentication ํ์ ์ด๋ฉฐ, ํด๋น ํด๋์ค๋ก ๋ฐํ์ ํด์ค์ผํจ.
- Authentication ๊ตฌํ์ฒด ์ค ์ฌ์ฉํ ๊ตฌํ์ฒด๋ฅผ ๋ฃ์ด์ฃผ๊ฒ ๋๋ฉด, ๋ด๋ถ์์ ํด๋น ๊ตฌํ์ฒด์ ๋ง๋ ์ธ์ฆ ๋ก์ง์ด ์ํ์ด ๋จ.
- ๊ฒฐ๋ก ์ UsernamePasswordAuthenticationToken ๋ฅผ ์ด์ฉํ๋ฉด DaoAuthenticationProvider ๊ฐ ์คํ์ด ๋จ.