인증된 사용자의 Authentication객체가 저장되는 보관소로 필요시 언제든 Authentication객체를 꺼내어 쓸 수 있도록 제공되는 클래스
ThreadLocal에 저장되어 있어 아무 곳에서나 참고가 가능하도록 설계되어 있다.
인증이 완료되면 HttpSession에 저장되어 어플리케이션 전반에 걸쳐 전역적으로 참고가 가능해진다.
💡클라이언트한테 세션이 response되면 securityContext는 지워진다.
SecurityContextholder.clearContext()
: SecurityContext 기본 정보 초기화 메서드Authentication authentication = SecurityContextHolder.getContext().getAuthentication()
을 통해 인증 객체를 어디서든 꺼내 쓸 수 있다.
SecurityContext context = SecurityContextHolder.createEmptyContext();
//인증 전 빈 SecurityContext를 만든다.
Authentication auth = new TestingAuthenticationToken("username", "password", "ROLE_USER");
//인증 객체를 생성한다.
context.setAuthentication(authentication);
SecurityContextHolder.setContext(context);
//인증 성공시 SecurityContext, SecurityContextHolder에 저장된다.
ThreadLocal : Thread마다 할당된 고유 공간(공유x)
ThreadLocal에 대해 더 자세히알고 싶다면 ->
https://javacan.tistory.com/entry/ThreadLocalUsage
reference : https://catsbi.oopy.io/f9b0d83c-4775-47da-9c81-2261851fe0d0