스프링 시큐리티 - Authentication Architecture

HUSII·2023년 8월 21일
0

스프링 시큐리티

목록 보기
3/7

스프링 시큐리티 공식 문서를 읽고, 라이브러리 코드들을 뜯어보면서 공부했습니다.
인증은 username/password 인증을 기준으로 설명하겠습니다.

Authentication 객체를 통해 인증된 유저의 정보를 확인할 수 있습니다.

스프링 시큐리티는 Authentication 객체를 SecurityContext에 담아서 보관합니다.

구조는 다음과 같습니다.

SecurityContextHolder

인증된 사용자에 대한 세부 정보를 저장하는 곳

SecurityContext

SecurityContextHolder로 부터 얻을 수 있는 객체, 여기에 Authentication 객체가 저장되어 있습니다.

Authentication

인증된 유저를 표현하는 객체

SecurityContext로부터 현재 인증된 유저(Authentication)를 얻을 수 있습니다.

Authentication은 3가지를 포함합니다.
1. principal - 사용자 세부 정보의 인스턴스가 저장되어 있습니다. 이 객체를 통해 사용자를 식별합니다.
2. credentials - 암호가 저장되어 있습니다. 웬만하면 사용자가 인증된 후 삭제되어 유출되지 않습니다.
3. authorities - 유저의 권한을 표현합니다.

> GrantedAuthority 인스턴스가 저장됩니다.

GrantedAuthority

인증 주체에게 부여된 권한(예: 역할, 범위 등)입니다.

Authentication.getAuthorities()를 통해 얻을 수 있습니다.


Authentication을 이용해서 인증할때

SecurityContextHolder.getContext()를 사용하는 대신 새 SecurityContext 인스턴스를 만들어야 여러 스레드에서 RACE CONDITION을 피할 수 있습니다.

SecurityContext context = SecurityContextHolder.createEmptyContext(); 
Authentication authentication =
    new TestingAuthenticationToken("username", "password", "ROLE_USER"); 
context.setAuthentication(authentication);

SecurityContextHolder.setContext(context);

클라이언트로 부터 request를 요청받았을때, 해당 유저의 정보를 알고싶을때 SecurityContextHolder.getContext().getAuthentication()을 통해 얻습니다.

만약 클라이언트가 인증을 하지않은 상태라면, AnonymousAuthenticationFilter에 의해 익명 유저가 SecurityContext에 입력되어 있음 -> 익명유저가 조회됩니다.

여기서 SecurityContextHolder는 ThreadLocal을 사용하기 때문에, 명시적으로 메서드의 인수로 전달되지 않더라도 동일한 스레드에서 항상 SecurityContext를 사용할 수 있습니다.

개인적으로 공부한 내용을 정리한 글입니다.
지적, 피드백 환영합니다.

profile
공부하다가 생긴 궁금한 것들을 정리하는 공간

0개의 댓글