spring session监听(2种方式) 作者:马育民 • 2020-07-22 19:39 • 阅读:10164 # 使用HttpSessionListener监听器 配置类: ``` @Configuration public class Config { @Bean public SessionEventHttpSessionListenerAdapter getSessionEventHttpSessionListenerAdapter() { List httpSessionListeners = new ArrayList(); httpSessionListeners.add(new SessionListener()); return new SessionEventHttpSessionListenerAdapter(httpSessionListeners); } } ``` 监听器: ``` package top.malaoshi.stdsession.listener; import javax.servlet.http.HttpSession; import javax.servlet.http.HttpSessionEvent; import javax.servlet.http.HttpSessionListener; public class SessionListener implements HttpSessionListener { @Override public void sessionCreated(HttpSessionEvent se) { HttpSession session=se.getSession(); System.out.println("session创建:"+ session.getId()+",有效时间:"+session.getMaxInactiveInterval()); } @Override public void sessionDestroyed(HttpSessionEvent se) { HttpSession session=se.getSession(); System.out.println("session失效:"+ session.getId()); } } ``` 用浏览器访问,执行结果: ``` session创建:11d41ce9-e26c-4b5e-b3d6-a56937e0604a,有效时间:1800 session失效:a614cee7-5094-4933-9849-d89c90b94186 ``` ### 注意 通过 [Spring Session配置session有效时间](https://www.malaoshi.top/show_1EF5w3RqdvhF.html "Spring Session配置session有效时间") 修改session有效时间后,在`sessionCreated () `方法中打印的有效时间仍然是 `1800`,即默认的30分钟 而不是修改后的时间,但经过亲测,有效时间确实改变了,只是打印错误 # 使用 @EventListener - 在spring-Session中session的创建、删除、过期都会接收到redis相关事件的通知 - spring-session中接收到相关通知后再由Spring发布相关ApplicationEvent(SessionCreatedEvent 、SessionDeletedEvent 、SessionExpiredEvent) - 需要注解 `@Configuration` 和 `@EventListener` 实现事件监听 ``` package top.malaoshi.stdsession.config; import org.springframework.context.annotation.Configuration; import org.springframework.context.event.EventListener; import org.springframework.session.Session; import org.springframework.session.events.SessionCreatedEvent; import org.springframework.session.events.SessionDeletedEvent; import org.springframework.session.events.SessionExpiredEvent; @Configuration public class RedisHttpSessionListenerConfig { /** * 监听session创建 */ @EventListener public void onCreated(SessionCreatedEvent event) { String sessionId = event.getSessionId(); // spring-session提供的session Session session = event.getSession(); System.out.println("创建:" + sessionId+",有效时间:"+session.getMaxInactiveInterval()); } /** * 监听session删除 */ @EventListener public void onDeleted(SessionDeletedEvent event) { String sessionId = event.getSessionId(); // spring-session提供的session Session session = event.getSession(); System.out.println("删除:" + sessionId); } /** * 监听session过期 */ @EventListener public void onExpired(SessionExpiredEvent event) { String sessionId = event.getSessionId(); // spring-session提供的session Session session = event.getSession(); System.out.println("过期:" + sessionId); } } ``` ### 注意 通过 [Spring Session配置session有效时间](https://www.malaoshi.top/show_1EF5w3RqdvhF.html "Spring Session配置session有效时间") 修改session有效时间后,在`onCreated() `方法中打印的有效时间仍然是 `PT30M`,即默认的30分钟 而不是修改后的时间,但经过亲测,有效时间确实改变了,只是打印错误 感谢: https://blog.csdn.net/Ajlzhu/article/details/105382429 https://blog.csdn.net/baidu_38307690/article/details/80199539?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.channel_param&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.channel_param 原文出处:http://malaoshi.top/show_1EF5w4KHSfhL.html