인프런 커뮤니티 질문&답변
작성자 없음
작성자 정보가 삭제된 글입니다.
초보같은 질문 하나만 하겠습니당..
작성
·
320
0
SecurityContext contextAfterChainExecution = SecurityContextHolder.getContext();
// Crucial removal of SecurityContextHolder contents before anything else.
SecurityContextHolder.clearContext();
this.repo.saveContext(contextAfterChainExecution, holder.getRequest(), holder.getResponse());
답변 1
5
네
SecurityContext contextAfterChainExecution = SecurityContextHolder.getContext();
// Crucial removal of SecurityContextHolder contents before anything else.
SecurityContextHolder.clearContext();
this.repo.saveContext(contextAfterChainExecution, holder.getRequest(), holder.getResponse());
에서
SecurityContextHolder.clearContext();
은 내부적으로 ThreadLocal 에서 contextAfterChainExecution 을 삭제하고 있습니다.
private static final ThreadLocal<SecurityContext> contextHolder = new ThreadLocal<>();
// ~ Methods
// ========================================================================================================
public void clearContext() {
contextHolder.remove();
}
그렇기 때문에 사용자마다 할당된 ThreadLocal 에서 contextAfterChainExecution 가 비워지는 것이지
contextAfterChainExecution 객체가 없어지거나 null 이 되는 것은 아닙니다.
그래서 ThreadLocal 에서 비워지기 전에
SecurityContext contextAfterChainExecution = SecurityContextHolder.getContext();
으로 저장해놓고 ThreadLocal 에서 비운다고 이해하시면 될 것 같습니다.
즉 contextAfterChainExecution 가 SecurityContextImpl 객체를 참조하게 됩니다.
마치
List<String> list = new ArrayList<>();
list.add("user");
String name = list.get(0);
list.remove(0) ;
이렇게 하더라도 name 의 값에는 "user" 가 여전히 참조되고 있는 것과 같은 이치라고 보시면 됩니다.
다만 list 에는 더이상 "user" 값이 참조되지 않겠죠^^




