Spring Security์์ ์ค์ ์ธ์ฆ ๋ก์ง์ ๋ด๋นํ๋ ํต์ฌ ์ปดํฌ๋ํธ
Authentication)๋ฅผ ๋ฐํํจAuthenticationManager
โ (delegates)
AuthenticationProvider(s)
โ
authenticate() ์ํ โ ์ฑ๊ณต ์ Authentication ๊ฐ์ฒด ๋ฐํ
AuthenticationManager๋ ์ฌ๋ฌ Provider ์ค ํ๋์ ์์ํ์ฌ ์ธ์ฆ ์ํsupports() ๋ฉ์๋๋ก ์ด๋ค ํ์
์ ์ธ์ฆ์ ์ฒ๋ฆฌํ ์ง ํ๋จ@Component
public class JwtAuthenticationProvider implements AuthenticationProvider {
private final UserDetailsService userDetailsService;
private final JwtTokenProvider jwtTokenProvider;
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
String token = (String) authentication.getCredentials();
String username = jwtTokenProvider.getUsername(token);
UserDetails userDetails = userDetailsService.loadUserByUsername(username);
if (!jwtTokenProvider.validateToken(token, userDetails)) {
throw new BadCredentialsException("Invalid JWT token");
}
return new UsernamePasswordAuthenticationToken(userDetails, token, userDetails.getAuthorities());
}
public boolean supports(Class<?> authentication) {
return JwtAuthenticationToken.class.isAssignableFrom(authentication);
}
}
public class JwtAuthenticationToken extends UsernamePasswordAuthenticationToken {
public JwtAuthenticationToken(String token) {
super(null, token);
}
}
โAuthenticationProvider๋ Spring Security์์ ์ค์ง์ ์ธ ์ธ์ฆ ๋ก์ง์ ๋ด๋นํ๋ ์ปดํฌ๋ํธ์ ๋๋ค.
JWT ๊ธฐ๋ฐ ์ธ์ฆ์ ๊ฒฝ์ฐ ์ปค์คํ Provider๋ฅผ ๊ตฌํํด ํ ํฐ์์ ์ฌ์ฉ์ ์ ๋ณด๋ฅผ ์ถ์ถํ๊ณ ,
์ธ์ฆ๋ Authentication ๊ฐ์ฒด๋ฅผ ๋ฐํํจ์ผ๋ก์จ ์ ์ฐํ ์ธ์ฆ ๋ฐฉ์์ ๊ตฌ์ฑํ ์ ์์ต๋๋ค.โ