[ODOP] 85일차 - 인증 저장소 필터
SecurityContextPersistenceFilter
- SecurityContext 객체의 생성, 저장, 조회를 담당하는 필터
익명 사용자
- 새로운 SecurityContext 객체를 생성하여 SecurityContextHolder 에 저장
- AnonymousAuthenticationFilter 에서 AnonymousAuthenticationToken 객체를 SecurityContext 에 저장
인증 시
- 새로운 SecurityContext 객체를 생성하여 SecurityContextHolder 에 저장
- UsernamePasswordAuthenticationFilter 에서 인증 성공 후 SecurityContext 에 UsernamePasswordAuthentication 객체를 SecurityContext 에 저장
- 인증이 최종 완료되면 Session 에 SecurityContext 를 저장
인증 후
- Session 에서 SecurityContext 를 꺼내어 SecurityContextHolder 에서 저장
- SecurityContext 안에 Authentication 객체가 존재하면 계속 인증을 유지
최종 응답 시 공통
- SecurityContextHolder.clearContext()
동작 요약
- 로그인 되지 않은 사용자의 경우 그림에서 왼쪽 처럼 SecurityContext 에 저장되어 있는 데이터가 없다. 따라서 SecurityContextPersistenceFilter 는 SecurityContext 를 만들고 Authentication 정보를 넣는다
- 로그인에 성공할 경우 오른쪽 처럼 SecurityContextHolder 에 인증 정보가 담긴 Authentication 객체가 생성 되어 저장 된다.
- 만약 이 때 새로운 요청이 들어올 경우, SecurityContextHolder 에 담겨있는 (위에서 담은) 인증객체(Authentication) 를 session 에 담아서 처리