-
Spring Security - Session 생성 및 파기 이벤트Spring/Security 2024. 5. 12. 15:07728x90반응형
GitHub - dukbong/Practice-Spring-Security: 세션 기반 인증 - 예시
세션 기반 인증 - 예시. Contribute to dukbong/Practice-Spring-Security development by creating an account on GitHub.
github.com
HttpSessionEventPublisher 등록
Spring Security에서 세션 생성 및 파기 이벤트를 처리하기 위해서는 HttpSessionEventPublisher를 사용합니다.
사용자가 웹 애플리케이션에 접속하여 세션이 생성, 파기 될 때마다 발생합니다.
이를 활용하여 사용자의 로그인 정보나 세션 정보를 로그로 남기는 등의 작업을 수행할 수 있습니다.
@Bean public HttpSessionEventPublisher httpSessionEventPublisher() { return new HttpSessionEventPublisher(); }
다양한 이벤트 처리
세션 생성 및 파기의 이벤트는 한번에 처리하는 방법과 따로 따로 처리하는 방법으로 나뉘어진다.
@Component @Slf4j public class CustomSessionEventListener implements HttpSessionListener { @Override public void sessionCreated(HttpSessionEvent se) { log.info("세션 이벤트 모니터링 - 생성 : {}", se.getSession().getId()); } @Override public void sessionDestroyed(HttpSessionEvent se) { log.info("세션 이벤트 모니터링 - 파기 : {}", se.getSession().getId()); } }
@Component @Slf4j public class SessionCreatedListener implements ApplicationListener<HttpSessionCreatedEvent> { @Override public void onApplicationEvent(HttpSessionCreatedEvent event) { log.info("세션 생성 = {}", event.getSession().getId(); } }
@Component @Slf4j public class SessionDestroyedListener implements ApplicationListener<HttpSessionDestroyedEvent> { @Override public void onApplicationEvent(HttpSessionDestroyedEvent event) { log.info("세션 파기 = {}", event.getSession().getId(); } }
728x90반응형'Spring > Security' 카테고리의 다른 글
[Spring-Security 6.3.0-M1] 권한 계층 RoleHierarchy (0) 2024.05.29 Spring Security - 세션 생성 정책 (0) 2024.05.13 Spring Security - 권한 계층 구조 설정 (RoleHierarchy) (0) 2024.05.10