개요
유저가 로그아웃할 때 시간을 DB에 저장하기 위해 세션이 만료되기 직전 세션 내 attribute를 가져와 어느 유저인지 확인하고 해당 유저의 로그아웃 시간을 업데이트해야 했습니다.
해결방법
처음에는 ApplicationListener<LogoutSuccessEvent>를 상속받은 후 LogoutSuccessEvent가 발생했을 때 유저 정보를 가져와 업데이트하는 방식으로 접근했는데, 해당 이벤트는 유저가 직접 로그아웃 버튼을 눌러야만 발생하는 문제점이 있었습니다.
저는 세션이 만료됨에 따라 로그아웃 되는 유저의 로그아웃 시간도 기록하고 싶었기 때문에 HttpSessionListener를 상속받은 뒤 sessionDestroyed 메서드를 오버라이드 하여 문제를 해결했습니다.
LogoutSuccessEvent가 발생했을 때 호출되는 코드
@Component | |
@Slf4j | |
public class LogoutSuccessListener implements ApplicationListener<LogoutSuccessEvent> { | |
@Override | |
public void onApplicationEvent(LogoutSuccessEvent event) { | |
String userName = event.getAuthentication().getName(); | |
// 비즈니스 로직 | |
} | |
} |
HttpSessionListener 코드
@Component | |
@WebListener | |
@Slf4j | |
public class SessionListener implements HttpSessionListener { | |
@Override | |
public void sessionDestroyed(HttpSessionEvent event) { | |
HttpSession session = event.getSession(); | |
// session 내 모든 attribute들을 가져오는 코드 | |
Enumeration e = (Enumeration) session.getAttributeNames(); | |
while (e.hasMoreElements()) { | |
Object attributeName; | |
if (ObjectUtils.isNotEmpty(attributeName = e.nextElement())) { | |
log.info("attribute: {}", session.getValue((String) attributeName)); | |
} | |
} | |
// 찾고자 하는 attribute에서 유저정보를 받아와 로그아웃 시간 업데이트 | |
} | |
} |
* 추가: HttpSessionListener 코드가 로컬에서는 잘 적용이 됐지만 상용 서버에서는 어떤 이유인지는 모르겠지만 잘 작동이 안되는 것을 확인했습니다. 따라서, 아래의 SessionDestroyedListener를 통해 해결했습니다. (2021-05-30)
SessionDestroyedListener
@Component | |
public class SessionDestroyedListener implements ApplicationListener<SessionDestroyedEvent> { | |
@Override | |
public void onApplicationEvent(SessionDestroyedEvent event) { | |
Session session = event.getSession(); | |
User sessionUser = session.getAttribute("USER"); | |
// 비즈니스 로직 | |
} | |
} | |
// @Configuration에 httpSessionEventPublisher 등록 | |
@Configuration | |
public class SessionConfig { | |
@Bean | |
public HttpSessionEventPublisher httpSessionEventPublisher() { | |
return new HttpSessionEventPublisher(); | |
} | |
} |
출처
stackoverflow.com/questions/33368963/how-to-get-all-session-values-and-names
How to get all Session values and names?
if i have values in a session and i need to get all the values in a session like String[] name = request.getParameterValues("values"); HttpSession session = request.getSession(); for(String temp:...
stackoverflow.com
HttpSessionListener - Will sessionDestroyed method be called on session timeout?
I have an implementation of HttpSessionListener where 'locked' resources in the application are released with sessionDestroyed method. The 'lock' information is maintained in database, and the rel...
stackoverflow.com
LogoutSuccessEvent (spring-security-docs-manual 5.5.0-M1 API)
All Implemented Interfaces: java.io.Serializable public class LogoutSuccessEvent extends AbstractAuthenticationEvent Application event which indicates successful logout Since: 5.2.0 See Also: Serialized Form
docs.spring.io
'[DEV] 기록' 카테고리의 다른 글
티스토리 블로그 내 gist css 수정하는 방법 (0) | 2021.04.25 |
---|---|
[javascript] 클립보드로 복사하는 방법 (0) | 2021.04.25 |
[c++] 문자열 내 특정 부분문자열 위치 찾기 (1) | 2021.04.24 |
[크롤링] 경향신문 크롤링 (6) | 2021.04.12 |
[Spring] Maven 정리 (0) | 2021.03.03 |