我正在使用Session 1.0.1。我需要在用户注销时执行一些逻辑,并且我需要依赖于HTTP会话无效,以解决用户无法显式注销的情况。
标准的Security包含任何适用的SecurityContext,但是SessionDestroyedEvent的Session版本只包含会话id。当此事件触发时,会话不再由SessionRepository持有,因此不能由id查找。
有没有任何方法可以使用Spring会话从过期的会话中检索SecurityContext?
发布于 2015-07-21 03:51:47
不幸的是没有。问题是,在Redis触发事件时,会话已经结束。此外,从Redis收到的事件不包含原始信息。这意味着无法检索SecurityContext。
有关此方面的更新,请跟踪春季-项目/春季-会议/议题/4
发布于 2017-11-23 11:31:32
对于sring会话1.1+和Redis https://docs.spring.io/spring-session/docs/current/reference/html5/#httpsession-httpsessionlistener
您必须配置HttpSessionEventPublisher,并且在那个春季会话之后将传播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()) {
...
}
}
}
https://stackoverflow.com/questions/31465177
复制相似问题