HttpSessionEventPublisher
Spring Security 에서 사용하는 클래스인데, 서블릿 컨테이너에서 발생하는 HttpSession 이벤트를
Spring Security 쪽 이벤트로 변환해서 전달해주는 다리 역할을 한다. 즉, 톰캣 같은 WAS 가
세션 생성 / 삭제 / ID 변경을 알리면, 그걸 Spring 어플리케이션 컨텍스트가 들을 수 있는 이벤트로 변환하여
전달한다.
즉, 쉽게 말하면
Tomcat 세션 이벤트
↓
HttpSessionEventPublisher
↓
Spring Security 이벤트
왜 필요한가
Spring Security 세션 관련 기능, 특히 동시 세션 제어나 세션 만료 감지는 컨테이너의 HttpSessionListener 이벤트만으로는
부족하다. HttpSessionEventPublisher 가 있어야 세션 생성/종료 시점에 Spring 쪽 SessionDestroyedEvent 같은 이벤트가
정상적으로 올라온다.
WAS는 세션이 생성/삭제될 때 아래 이벤트를 발생시킨다.
HttpSessionListener
하지만 Spring Security 는 이 이벤트를 직접 받지 못한다.
그래서 아래의 HttpSessionEventPublisher 가 대신 받아서 Spring 이벤트로 변환한다.
HttpSessionEventPublisher
내부 동작 흐름
예를 들어 세션 만료 시 아래와 같이 동작하는 구조이다.
1. Tomcat 이 세션 제거
2. HttpSessionListener 호출
3. HttpSessionEventPublisher 실행
4. Spring ApplicationEvent 발행
5. SessionDestroyedEvent 발생
6. ApplicationListener<SessionDestroyedEvent> 실행
실제 클래스 구조
public class HttpSessionEventPublisher
implements HttpSessionListener, HttpSessionIdListener
등록 방법(Spring Boot 3)
@Configuration
public class SessionConfig {
@Bean
public HttpSessionEventPublisher httpSessionEventPublisher() {
return new HttpSessionEventPublisher();
}
@Bean
ServletListenerRegistrationBean<HttpSessionEventPublisher> getHttpSessionEventPublisher() {
return new ServletListenerRegistrationBean<HttpSessionEventPublisher>(httpSessionEventPublisher());
}
}