java - HttpSessionListener 不起作用

标签 java spring spring-mvc servlets spring-boot

我已经实现了 HttpSessionListiner,但它不起作用。 用调试器检查它——进入 servlet 后创建新 session ,登录后 JSESSION_ID 发生变化,但 session.getCreateTime() 保持不变( session 保持不变?)。 使用注解,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");
    }
}

最佳答案

@Bean
public ServletListenerRegistrationBean<HttpSessionListener> sessionListener() {
    return new ServletListenerRegistrationBean<HttpSessionListener>(new sessionListener());
}

这个 bean 注册了我的监听器。我还没有找到其他解决方案。

关于java - HttpSessionListener 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32739957/

相关文章:

java - 未找到带有 URI [/PostTest/home/segundaVista2] 的 HTTP 请求的映射

java.lang.UnsatisfiedLinkError : com. sun.deploy.config.WinPlatform.getPlatformUserHome()Ljava/lang/String

java - 没有 xml 配置的 Spring @Async

spring - 运行时出现错误

java - Spring:嵌套的应用程序上下文

spring - ResponseEntity 中的重复 header

spring-mvc - 将 Redis/Spring Session 合并到我的应用程序中会阻止我部署该应用程序

java - 如何使用 API 将数据插入条形图

java - 如何在 IntelliJ 中打开当前符号的突出显示?

java - InvalidTypeException : Invalid type for value 1 of CQL type text, 需要类 java.lang.String 但类 [Ljava.lang.Object;假如