对于带有 Redis 的 sring-session 1.1+
https://docs.spring.io/spring-session/docs/current/reference/html5/#httpsession-httpsessionlistener
您必须配置 HttpSessionEventPublisher ,然后 spring-session 将传播 sessionDestroy 事件
@Configuration
@EnableRedisHttpSession
public class RedisHttpSessionConfig {
@Bean
public HttpSessionEventPublisher httpSessionEventPublisher() {
return new HttpSessionEventPublisher();
}
// ...
}
所以你可以使用标准的 spting SessionDestroyedEvent 监听器
@Component
public class SessionDestroyListener implements ApplicationListener<SessionDestroyedEvent> {
@Override
public void onApplicationEvent(SessionDestroyedEvent event) {
logger.debug("session destroyed {}", event.getId());
if(!event.getSecurityContexts().isEmpty()) {
...
}
}
}