인프런 커뮤니티 질문&답변
securityContext 삭제 질문
작성
·
588
1
12:17 강의시간에서
SecurityContextLogoutHandler.java 의 logout 메소드에서 securityContextHolder.clearContext(); 호출이 있습니다.
사용자 A, B, C 의 접속으로 인해 securityContextHolder에는 3개의 securityContext가 저장되어 있는 상황에서
사용자 A만 /logout 요청을 진행했을 때,
위 clearContext() 메소드로 사용자A의 securityContext만 삭제할 수 있는 원리가 궁금합니다.
언뜻 봐서 메소드명에서 느껴지는 의미로는 securityContextHolder안에 있는 모든 securityContext를 삭제하라 라고 해석되기 떄문입니다.
답변 1
0
네
SecurityContext 는 요청 스레드별로 ThreadLocal 에 저장되는 개념입니다.
그래서 securityContextHolder 에 SecurityContext 가 사용자별로 저장되어 있고 각 SecurityContext 는 서로간 간섭이 없는 독립적인 공간입니다.
그렇기 때문에 사용자 A의 /logout 처리 중에 securityContextHolder.clearContext() 를 실행하게 되면 요청 스레드 즉 사용자 A 의 요청 전용공간인 ThreadLocal 에 저장되어 있는 SecurityContext 만 삭제가 되는 원리로 되어 있습니다.
securityContextHolder 와 SecurityContext 를 설명하는 챕터를 참고해 주시기 바랍니다.





와.... 정말 머리가 간지러울 정도로 궁금했던 점이 해결됐습니다.
명쾌한 답변 감사드립니다...!!