

์คํ๋ง ์ํ๋ฆฌํฐ์์ ์ฌ์ฉํ๋ ์ฌ๋ฌ ๊ฐ์ ํํฐ
์ธ์ฆ๊ณผ ์ธ๊ฐ์ ๋ํ ํํฐ๋ค ์ ์
SecurityFilterChain์ @Configuration ์ด๋
ธํ
์ด์
์ ๋ถ์ธ ์ค์ ์ ๋ณด ํด๋์ค๋ฅผ ํตํด ์คํ๋ง ๋น (@Bean)์ผ๋ก ๋ฑ๋ก ๊ฐ๋ฅ

์ฌ๋ฌ ๊ฐ์ SecurityFilterChain์ ๊ตฌ์ฑํ์ฌ ๋งค์นญ๋๋ URL์ ๋ฐ๋ผ ๋ค๋ฅธ SecurityFilterChain์ด ์ฌ์ฉ๋๋๋ก ํ ์ ์์

1. ์ ์ ๊ฐ ๋ก๊ทธ์ธ์ ์์ฒญํ๋ค. (Http Request)
2. AuthenticationFilter ์์ UsernamePasswordAuthenticationToken ์์ฑํ๋ค.
3. ํด๋น Token์ AuthenticationManager ์๊ฒ ์ ๋ฌํ๋ค.
4. AuthenticationManager ๋ ๋ฑ๋ก๋ AuthenticationProvider ๋ค์ ์กฐํํ์ฌ ์ธ์ฆ์ ์๊ตฌํ๋ค.
5. ์ค์ DB์์ ์ฌ์ฉ์ ์ธ์ฆ์ ๋ณด๋ฅผ ๊ฐ์ ธ์ค๋ UserDetailsService ์ ์ฌ์ฉ์ ์ ๋ณด๋ฅผ ๋๊ฒจ์ค๋ค.
6. ๋๊ฒจ๋ฐ์ ์ฌ์ฉ์ ์ ๋ณด๋ฅผ ํตํด DB์์ ์ฐพ์ ์ฌ์ฉ์ ์ ๋ณด๋ฅผ ๋ด์ UserDetails ๊ฐ์ฒด๋ฅผ ๋ง๋ ๋ค.
7. AuthenticationProvider ๋ UserDetails ๋ฅผ ๋๊ฒจ๋ฐ๊ณ ์ฌ์ฉ์ ์ ๋ณด๋ฅผ ๋น๊ตํ๋ค.
8. ์ธ์ฆ์ด ์๋ฃ๋๋ฉด ๊ถํ ๋ฑ์ ์ฌ์ฉ์ ์ ๋ณด๋ฅผ ๋ด์ Authentication ๊ฐ์ฒด๋ฅผ ๋ฐํํ๋ค.
9. ๋ค์ AuthenticationFilter ์ Authentication ๊ฐ์ฒด๊ฐ ๋ฐํ๋๋ค.
10. Authenticaton ๊ฐ์ฒด๋ฅผ SecurityContext ์ ์ ์ฅํ๋ค.
(์ธ์
-์ฟ ํค ๊ธฐ๋ฐ์ ์ธ์ฆ ๋ฐฉ์)
public class UsernamePasswordAuthenticationToken extends AbstractAuthenticationToken {
private static final long serialVersionUID = 570L;
// ์ฃผ๋ก ์ฌ์ฉ์์ UserID ์ฌ์ฉ
private final Object principal;
// ์ฃผ๋ก ์ฌ์ฉ์์ password ์ฌ์ฉ
private Object credentials;
// ์ธ์ฆ ์๋ฃ ์ ์ ๊ฐ์ฒด ์์ฑ
public UsernamePasswordAuthenticationToken(Object principal, Object credentials) {
super((Collection)null);
this.principal = principal;
this.credentials = credentials;
this.setAuthenticated(false);
}
// ์ธ์ฆ ์๋ฃ ํ์ ๊ฐ์ฒด ์์ฑ
public UsernamePasswordAuthenticationToken(Object principal, Object credentials, Collection<? extends GrantedAuthority> authorities) {
super(authorities);
this.principal = principal;
this.credentials = credentials;
super.setAuthenticated(true);
}
public static UsernamePasswordAuthenticationToken unauthenticated(Object principal, Object credentials) {
return new UsernamePasswordAuthenticationToken(principal, credentials);
}
public static UsernamePasswordAuthenticationToken authenticated(Object principal, Object credentials, Collection<? extends GrantedAuthority> authorities) {
return new UsernamePasswordAuthenticationToken(principal, credentials, authorities);
}
}
public interface AuthenticationManager {
Authentication authenticate(Authentication authentication) throws AuthenticationException;
}
public interface AuthenticationProvider {
Authentication authenticate(Authentication authentication) throws AuthenticationException;
boolean supports(Class<?> authentication);
}
public interface UserDetailsService {
UserDetails loadUserByUsername(String username) throws UsernameNotFoundException;
}
public interface UserDetails extends Serializable {
// ๊ถํ ๋ชฉ๋ก
Collection<? extends GrantedAuthority> getAuthorities();
String getPassword();
String getUsername();
// ๊ณ์ ๋ง๋ฃ ์ฌ๋ถ
boolean isAccountNonExpired();
// ๊ณ์ ์ ๊น ์ฌ๋ถ
boolean isAccountNonLocked();
// ๋น๋ฐ๋ฒํธ ๋ง๋ฃ ์ฌ๋ถ
boolean isCredentialsNonExpired();
// ์ฌ์ฉ์ ํ์ฑํ ์ฌ๋ถ
boolean isEnabled();
}
SecurityContextHolder.getContext().setAuthentication(authentication);
SecurityContextHolder.getContext().getAuthentication(authentication);
public interface Authentication extends Principal, Serializable {
// ํ์ฌ ์ฌ์ฉ์์ ๊ถํ ๋ชฉ๋ก
Collection<? extends GrantedAuthority> getAuthorities();
// ์ฃผ๋ก password๋ฅผ ๊ฐ์ ธ์ด
Object getCredentials();
Object getDetails();
// Principal (๋ณดํธ๋ ๋์์ ์ ๊ทผํ๋ ์ ์ ) ๊ฐ์ฒด๋ฅผ ๊ฐ์ ธ์ด
Object getPrincipal();
// ์ธ์ฆ ์ฌ๋ถ ๊ฐ์ ธ์ด
boolean isAuthenticated();
// ์ธ์ฆ ์ฌ๋ถ ์ค์
void setAuthenticated(boolean isAuthenticated) throws IllegalArgumentException;
}
์ถ์ฒ : https://velog.io/@dlthgml0108/Spring-Security-%EC%9D%B8%EC%A6%9D%EA%B3%BC-%EC%9D%B8%EA%B0%80