spring session 导致 HttpSessionListener 失效

@ServletComponentScan(basePackages = "com")
@EnableRedisHttpSession
public class StartAuthApplication {
    public static void main(String[] args) {
        SpringApplication.run(StartAuthApplication.class, args);
    }
}


    @WebListener
    public static class MyHttpSessionListener implements HttpSessionListener {
        public MyHttpSessionListener() {
            System.out.println("MyHttpSessionListener");
        }

        @Override
        public void sessionCreated(HttpSessionEvent se) {
            log.info("sessionCreated new session {}", se.getSession().getId());
        }

        @Override
        public void sessionDestroyed(HttpSessionEvent se) {
            log.info("sessionDestroyed session destroyed {}", se.getSession().getId());
        }
    }

上面的代码,看上去没有什么问题吧。本意思是想通过ServletComponentScan+WebListener来添加一个监听器。但是 使用到了spring session (redis),会导致上面的代码, MyHttpSessionListener 里的代码不起作用,即 本文的主题 HttpSessionListener 失效

其实原因也很简单,就是因为 HttpSerssionListener实现类,原本应该由servlet容调(比如TOMCAT)来调用,现在因为spring session的存在(可以理解为spring从中倒腾了下),调用顺序变成了 tomcat --> spring --> HttpSessionListener。 直接上代码

public class SessionEventHttpSessionListenerAdapter
		implements ApplicationListener<AbstractSessionEvent>, ServletContextAware {

public void onApplicationEvent(AbstractSessionEvent event) {
		if (this.listeners.isEmpty()) {
			return;
		}

		HttpSessionEvent httpSessionEvent = createHttpSessionEvent(event);

        // 重点在这里 this.listeners。 这是spring代码里的一个对象
		for (HttpSessionListener listener : this.listeners) {
			if (event instanceof SessionDestroyedEvent) {
				listener.sessionDestroyed(httpSessionEvent);
			}
			else if (event instanceof SessionCreatedEvent) {
				listener.sessionCreated(httpSessionEvent);
			}
		}
	}

}

spring 抽象了一层 AbstractSessionEvent, this.listeners, 又是熟悉的套路,从这里可以看出,我们写的HttpSessionListener,变成了二等公民,由spring 的SessionEventHttpSessionListenerAdapter 这个 一等公民来决定什么时候调用。

所以,问题的根本原因,我们写的MyHttpSessionListener,之所以没有被调用,是因为spring的变量 this.listeners 没有得到我们写的实现类。继续想一下,spring 为什么没有拿到呢??

解决办法,把这个实现类,变成一个bean吧。

    @WebListener
    // 添加一个注解就可以了
    @Component
    public static class MyHttpSessionListener implements HttpSessionListener {
    }

通过上面的分析能得出一个结论:由于spring的存在,原本本该由servlet直接调用我们的代码,有可能会变成由servlet 调用spring, 再由 spring 来调用我们的代码。而spring在调用我们的代码时候,它是怎们我们的代码的呢(比如某个接口的实现类有哪些),很有可能会以BEAN的形式进行查找某对象,从而调用对象的方法,原因也很好理解,毕意spring的另一个功能就是IOC,不难理解吧。

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Session 是一个轻量级的会话管理框架,它基于 Spring Security 和 Java Servlet API,旨在提供跨域的会话管理解决方案,简化在分布式应用中管理用户会话的需求。Spring Session 的主要目标是: 1. **跨域会话共享**:Spring Session 允许你在不同的域(通常是指不同域名或端口)之间共享同一个用户的会话,这对于现代的微服务架构和API网关非常有用。 2. **持久化会话**:支持会话数据的持久化,即使用户关闭浏览器,也可以通过服务器存储会话信息,再次访问时能够恢复登录状态。 3. **可配置性**:提供了多种会话存储机制,包括内存、Redis、Memcached、数据库等,可以根据应用需求灵活选择。 4. **与Spring Security集成**:无缝集成到Spring Security框架中,可以轻松管理用户认证和授权。 5. **可扩展性**:Spring Session 提供了对Spring Cloud的集成,使得在分布式环境中会话管理更加容易。 使用 Spring Session 的关键步骤包括: - 配置一个会话工厂,指定会话存储机制。 - 创建一个HttpSessionListener监听会话创建和销毁事件。 - 配置Spring Security以使用Spring Session进行会话管理。 以下是一个简单的配置示例: ```java @Configuration @EnableCaching @EnableGlobalMethodSecurity(prePostEnabled = true) public class WebSecurityConfig extends WebSecurityConfigurerAdapter { @Autowired private HttpSessionStrategy httpSessionStrategy; @Bean public HttpSessionStrategy httpSessionStrategy() { return new SaveOnlyIfNewHttpSessionStrategy(); } @Override protected void configure(HttpSecurity http) throws Exception { // ... http .sessionManagement() .sessionCreationPolicy(SessionCreationPolicy.STATELESS) .and() .sessionAuthenticationStrategy(sessionAuthenticationStrategy()); } @Bean public SaveOnlyIfNewHttpSessionStrategy sessionAuthenticationStrategy() { return new SaveOnlyIfNewHttpSessionStrategy(httpSessionStrategy); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值