개발이야기

Spring Security 이해하기

클라인STR 2018. 10. 11. 22:40

1. Spring Security KeyWord

  • Authentication (인증) : 사용자가 누구인지 확인하는 과정
  • Authorization (권한) : 현재 사용자가 특정 대상 (URL, 기능 등)을 사용할 권한이 있는지 검사하는것
  • Principal (접근주체) : 보호된 대상에 접근하는 사용자

Spring Security 는 인증과 권한과정을 Spring Security 에서 제공하는 Filter들의 흐름에 따라 이루어 지도록한다.

2. Authentication 와 SecurityContext

  • Authentication 의 용도 
    • 현재 접근 주체 정보를 담는 목적
    • 인증 요청 할때, 요청정보를 담는 목적
  • SecurityContext 
    • Authentication 을 보관
    • 스프링 시큐리티는 현재 사용자에 대한 Authentication 객체를 구할 때 SecurityContext로 부터 구한다.

Authentication 주요 메서드

  • String getName() : 사용자의 이름
  • Object getCredential() : 증명 값 (비밀번호)
  • Object getPrincipal() : 인증 주체 정보
  • boolean isAuthenticated() : 인증되었는지 여부
  • Collection getAuthorities() : 현재 사용자가 가진 권한

3. SecurityContextHolder

  • SecurityContext를 보관 
    • 기본 쓰레드로컬에 SecurityContext를 보관

4. AuthenticationManager

  • AuthenticationManager 인증을 처리함
  • 인증에 성공하면 인증 정보를 담고 있는 Authentication 객체 리턴 한다 
    • 스프링 시큐리티는 리턴한 Authenticatoin 객체를 SecurityContext에 보관 및 인증 상태를 유지하기 위해 세션에 보관
  • 인증 실패시 AuthenticationException을 발생시킨다.
public interface AuthenticationManager {
Authentication authenticate(Authentication authentication)
throws AuthenticationException
}

5. SecurityInterceptor

  • 인가를 처리한다. 
    • 웹의 경우 FilterSecurityInterceptor 구현 사용
    • AccessDecisionManager에 권한 검사 위임
  • 사용자가 자원의 보안 설정 기준으로, 접근 권한이 없을 경우 익셉션이 발생