【发布时间】:2015-12-20 19:03:09
【问题描述】:
我已经实现了 HttpSessionListiner 但它不起作用。 使用调试器检查它 - 进入 servlet 后创建新会话,登录后 JSESSION_ID 更改,但 session.getCreateTime() 保持不变(会话保持不变?)。 使用注解,Spring Security。也许我错过了 Spring Security 中的一些配置?
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
import org.apache.log4j.Logger;
@WebListener
public class SessionListener implements HttpSessionListener {
private static int totalActiveSessions;
private static final Logger log = Logger.getLogger(SessionListener.class);
@Override
public void sessionCreated(HttpSessionEvent se) {
totalActiveSessions++;
log.warn("sessionCreated - add one session into counter");
}
@Override
public void sessionDestroyed(HttpSessionEvent se) {
totalActiveSessions--;
log.debug("sessionDestroyed - deleted one session from counter");
}
}
【问题讨论】:
-
你在项目中使用spring-security吗?
-
@NallaSrinivas 是的,但是我尝试在其他项目中实现这个监听器,它不使用 Spring Security,它也不起作用。
-
扩展 org.springframework.security.web.session。 ttpSessionEventPublisher 如果您使用的是 spring security 并在 web.xml 中指定此侦听器。如果你没有使用 spring security 然后检查 servlet api 版本,对你来说应该是 3.0
-
@NallaSrinivas 尝试扩展 HttpSessionEventPublisher,没有任何改变。我正在使用注释和 SpringBoot。 Servlet-api 3.1.0
-
您在 web.xml 中指定了 ttpSessionEventPublisher 吗?还要检查 web.xml 中的 spring 安全过滤器。如果您使用的是 spring security 那么 SessionListener 应该从 ttpSessionEventPublisher 扩展并覆盖两个方法。
标签: java spring spring-mvc servlets spring-boot