inflearn logo
강의

강의

N
챌린지

챌린지

멘토링

멘토링

N
클립

클립

로드맵

로드맵

지식공유

스프링 시큐리티

5) 인증 저장소 필터 - SecurityContextPersistenceFilter

초보같은 질문 하나만 하겠습니당..

325

작성자 없음

작성한 질문수 0

0

SecurityContext contextAfterChainExecution = SecurityContextHolder.getContext();
// Crucial removal of SecurityContextHolder contents before anything else.
SecurityContextHolder.clearContext();
this.repo.saveContext(contextAfterChainExecution, holder.getRequest(), holder.getResponse());
SecurityContextPersistenceFilter의 코드를 보면 위와 같이 되어있습니다.
 
중간에 clearContext()를 하면 contextAfterChainExecution 도 null이 된다고 생각했는데, 실제로는 null이 안되더라구요...
 
왜 그런건지 궁금합니다...
 
 

java spring-boot Spring Security

답변 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" 값이 참조되지 않겠죠^^

 

 

 

시큐리티 공부 버전 질문

0

188

1

[해결 방법] MethodSecurityConfig.customMethodSecurityMetadataSource() 호출하지 않는 이슈

0

196

1

AbstractSecurityInterceptor.class.beforeInvocation()를 2번 실행하는 경우

0

185

1

강의 코드가 왜이렇게 뒤죽박죽인가요...

0

270

1

메인 페이지로 접속해도 login url로 리다이렉트가 되지 않습니다..

0

247

1

파라미터값이 넘어가지 않습니다 ....

0

381

1

security filterChain 설정 질문이 있습니다.

0

336

1

소스 부분 질문 드립니다.

0

211

2

섹션4 7번 강의 문제가 있는거 같네요.

0

351

2

파일이 수시로 이름이 바껴있네요 ㄷㄷ

0

308

1

HttpSessionSecurityContextRepository를 사용안하는 문제

0

563

2

error , exception 이 잘 안됩니다.

0

288

2

thymeleaf tag 질문합니다.

0

200

2

버전업하면서 deprecated된 것들이 너무많아요

0

482

1

spring security 패치 관련

0

442

1

모바일을 사용할때 토큰말고 세션

0

863

2

DB 연동한 인가 부분에 대한 질문입니다!

0

267

1

Ajax방식도 똑같이 Session방식을 사용하는건가요?

0

311

1

Config 파일 생성 시 질문이 있습니다.

0

233

1

강사님 몇일동안 구글 검색만 100개 했는데도 이유를 모르겠습니다..

1

440

2

403 에러 뜹니다.

0

817

2

login_proc의 존재에 대한 간략한 설명입니다

0

279

1

top.html에 로그인 링크를 만들어서 로그인을 해봤습니다

0

289

2

안녕하세요. DB에 저장될 때 이해 안 가는 값이 있어서 질문드립니다!

0

193

1