我已经实现了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");
}
}
3条答案
按热度按时间k97glaaz1#
此Bean注册了我的侦听器。我尚未找到其他解决方案。
5us2dqdw2#
虽然不是发布者的特定问题,但另一个问题是会话实际上没有被创建,这意味着您的侦听器没有被正确地触发。如果您使用Spring Security,默认会话创建策略是SessionCreationPolicy.IF_REQUIRED。
您可以根据需要在Web安全Java配置中更改此设置:
来源:https://www.baeldung.com/spring-security-session
2eafrhcq3#
为了避免会话固定攻击,Spring在用户通过身份验证后更改会话ID。