[스프링] Spring Security
Spring Security란
spring 기반의 애플리케이션의 보안(인증과 권한, 인가 등)을 담당하는 스프링 하위 프레임 워크이다.
Spring Security는 인증과 권한에 대한 부분을 Filter 흐름에 따라 처리한다.
이 기능을 통해 보안과 관련해서 많은 옵션을 주기 때문에 개발자 입장에서는 보안관련 로직을 작성하지 않아도 된다는 장점이 있다.
인증(Authorizatoin)과 인가(Authentication)
- 인증(Authentication): 해당 사용자가 본인이 맞는지를 확인하는 절차
- 인가(Authorization): 인증된 사용자가 요청한 자원에 접근 가능한지를 결정하는 절차
Spring Security는 기본적으로 인증 절차를 거친 후에 인가 절차를 진행하게 되며, 인가 과정에서 해당 리소스에 대한 접근 권한이 있는지 확인을 하게 된다. Spring Security에서는 이러한 인증과 인가를 위해 Principal을 아이디로, Credential을 비밀번호로 사용하는 Credential 기반의 인증 방식을 사용한다.
- Principal(접근 주체): 보호받는 Resource에 접근하는 대상
- Credential(비밀번호): Resource에 접근하는 대상의 비밀번호
인증 관련 아키텍쳐
SecurityContextHolder
보안 주체의 세부 정보를 포함하여 응용프로그램의 현재 보안 컨텍스트에 대한 세부 정보가 저장된다.
SecurityContext
Authentication을 보관하는 역할을 하며, SecurityContext를 통해 Authentication 객체를 꺼내올 수 있다.
Authentication
Authentication는 현재 접근하는 주체의 정보와 권한을 담은 인터페이스이다. [ SecurityContextHolder -> SecurityContext -> Authentication 이렇게 접근 가능 ]
public interface Authentication extends Princupal, Serializable {
// 현재 사용자의 권한 목록을 가져옴
Collection<? extends GrantedAuthority> getAuthorities();
// credentials(주로 비밀번호)을 가져옴
Object getCredentials();
Object getDetails();
// Principal 객체를 가져옴.
Object getPrincipal();
// 인증 여부를 가져옴
boolean isAuthenticated();
// 인증 여부를 설정함
void setAuthenticated(boolean isAuthenticated) throws IllegalArgumentException;
}
UsernamePasswordAuthenticationToken
UsernamePasswordAuthenticationToken은 Authentication을 implements한 AbstractAuthenticationToken의 하위 클래스로, user의 Id가 Principal, password가 Credential의 역할을 한다. UsernamePasswordAuthenticationToken의 첫 생성자는 인증 전의 객체를 저장하고, 두번째 생성자는 인증이 완료된 객체를 생성한다
public class UsernamePasswordAuthenticationToken extends AbstractAuthenticationToken {
// 주로 사용자의 ID에 해당함
private final Object principal;
// 주로 사용자의 PW에 해당함
private Object credentials;
// 인증 완료 전의 객체 생성
public UsernamePasswordAuthenticationToken(Object principal, Object credentials) {
super(null);
this.principal = principal;
this.credentials = credentials;
setAuthenticated(false);
}
// 인증 완료 후의 객체 생성
public UsernamePasswordAuthenticationToken(Object principal, Object credentials,
Collection<? extends GrantedAuthority> authorities) {
super(authorities);
this.principal = principal;
this.credentials = credentials;
super.setAuthenticated(true); // must use super, as we override
}
}
AuthenticationProvider
AuthenticationProvider는 실제 인증에 대한 부분을 처리, 인증전의 Authentication객체를 받아 인증이 완료된 객체를 반환하는 역할을 한다. [ 실제로 인증 처리하는 메서드는 authenticate라는 메소드이다. ]
public interface AuthenticationProvider {
// 인증 전의 Authenticaion 객체를 받아서 인증된 Authentication 객체를 반환
Authentication authenticate(Authentication var1) throws AuthenticationException;
boolean supports(Class<?> var1);
}
Authentication Manager
인증은 실질적으로 Manager에 등록된 AuthenticationProvider에 의해서 처리된다. 인증이 성공하면 성공한 객체를 Security Context에 저장하고, 인증 상태를 유지하기 위해 세션에 보관하며, 실패하면 AuthenticationException발생.
GrantedAuthority
GrantedAuthority는 현재 사용자(Principal)가 가지고 있는 권한을 의미한다. ROLE_ADMIN이나 ROLE_USER와 같이 ROLE_* 형태로 사용하며, 보통 "roles"라고 한다. GrantedAuthority 객체는 UserDetailsService에 의해 불러올 수 있고, 특정 자워에 대한 권한이 있는지를 검사하여 접근 허용 여부를 결정한다
UserDetails
인증에 성공하여 생성된 UserDetails 객체는 Authentication 객체를 구현한 UsernamePasswordAuthenticationToken을 생성하기 위해 사용된다.
UserDetailsService
UserDetailsService 인터페이스는 UserDetails 객체를 반환하는 단 하나의 메소드를 가지고 있는데, 이를 구현한 클래스의 내부에 UserRepository를 주입받아 DB와 연결하여 처리한다.
public interface UserDetailsService {
UserDetails loadUserByUsername(String var1) throws UsernameNotFoundException;
}
참고 자료
https://mangkyu.tistory.com/76
[SpringBoot] Spring Security란?
대부분의 시스템에서는 회원의 관리를 하고 있고, 그에 따른 인증(Authentication)과 인가(Authorization)에 대한 처리를 해주어야 한다. Spring에서는 Spring Security라는 별도의 프레임워크에서 관련된 기능
mangkyu.tistory.com