https://docs.spring.io/spring-security/reference/servlet/authentication/architecture.html

image.png

SecurityContextHolder - 인증 되어진 사용자의 정보를 가지고 있는 스프링 시큐리티의 저장소이다.

SecurityContextHolder의 메소드는 모두다 static 으로 선언되기 때문에 어디서든 접근할 수 있다.

다수의 사용자인 멀티 쓰레드 환경에서 SecurityContextHolder를 통해 SecurityContext를 부여하는 관리 전략은 위임하여 다른 클래스(**SecurityContextHolderStrategy )**에게 맡긴다.

→ 다수의 사용자가 각각의 SecurityContext를 부여 받아야 한다는 것.

(사용자별로 다른 저장소를 제공해야 인증 정보가 겹치는 일이 발생하지 않는다.)

즉, SecurityContextHolder는 SecurityContext들을 관리하는 메소드를 제공하지만 실제로 등록, 초기화, 읽기와 같은 작업은 SecurityContextHolderStrategy 인터페이스를 활용한다.

→ 코드 내에서 strategy라는 객체(클래스)가 해주는 것을 할 수 있음.

private static SecurityContextHolderStrategy strategy;

// get, clear 등의 요청 모두 strategy객체가 대신 해줌.

public static SecurityContext getContext() {
        return strategy.getContext();
    }
    ...

보통 threadlocal방식을 사용한다.(ThreadLocalSecurityContextHolderStrategy)