这似乎是一个非常普遍的问题.但我找不到任何有效的解决方案.我们使用的是Richafaces 4,Myfaces 2.0.5和Spring security 3.0.X.
在ajax/non ajax请求的会话时间内,应该将用户重定向到登录页面.在记录回来之后,应该显示先前执行的ajax/non ajax操作.
我们没有遇到任何非Ajax请求的问题.但是对于ajax请求,用户不会被重定向到登录页面.
我已经按照这个链接https://community.jboss.org/message/729913#729913 并实现了servlet方法.该解决方案适用于Firefox,而不适用于IE 8.
即使在会话超时时正确地重定向到登录页面,也可能存在一个问题.我希望在成功登录以前调用的ajax请求时出现ViewExpiredException.
我想带来ViewExpiredException,因为这两个问题可能相互关联.
任何解决方案/线索将不胜感激.
我想在用户从会话超时中注销时执行自定义事件。在我的 application.properties 指定的时间长度后,用户成功注销:
server.servlet.session.timeout=10
server.servlet.session.cookie.max-age=10
Run Code Online (Sandbox Code Playgroud)
我发现了一些涉及 SessionDestroyedEvent 的类似解决方案,例如:
@Slf4j
@Component
public class SessionExpiredListener implements ApplicationListener<SessionDestroyedEvent> {
@Override
public void onApplicationEvent(SessionDestroyedEvent event) {
for (SecurityContext securityContext : event.getSecurityContexts()) {
Authentication authentication = securityContext.getAuthentication();
UserPrincipal user = (UserPrincipal) authentication.getPrincipal(); // UserPrincipal is my custom Principal class
log.debug("Session expired!" + user.getUsername());
// do custom event handling
}
}
}
Run Code Online (Sandbox Code Playgroud)
问题是 SessionDestroyedEvent 未与会话超时同时触发,在我的测试中,它在会话过期后最多 5 分钟触发。
我也尝试过在 HttpSessionListener 中使用 sessionDestroyed 但结果相似。
是否有一个事件会在会话到期时触发,或者有什么方法可以实现这一点?