配置如下:
web.xml:
<listener> <listener-class>cn.edu.hbcf.common.listener.SessionListener</listener-class> </listener>
java类:
SessionListener.java
package cn.edu.hbcf.common.listener; import javax.servlet.http.HttpSession; import javax.servlet.http.HttpSessionEvent; import javax.servlet.http.HttpSessionListener; import cn.edu.hbcf.common.constants.WebConstants; import cn.edu.hbcf.common.springmvc.SpringContextHolder; import cn.edu.hbcf.privilege.dao.BaseUserLogMapper; import cn.edu.hbcf.privilege.pojo.BaseUserLog; import cn.edu.hbcf.privilege.pojo.BaseUsers; public class SessionListener implements HttpSessionListener { @Override public void sessionCreated(HttpSessionEvent arg0) {} @Override public void sessionDestroyed(HttpSessionEvent arg0) { HttpSession session = arg0.getSession(); BaseUsers user = (BaseUsers)session.getAttribute(WebConstants.CURRENT_USER); String name=user.getAccount(); if(name!=null && name.length()>0){//session失效时候移除记录 if(SessionAttributeListener.sessionMap.containsKey(name)){ BaseUserLogMapper baseUserLogMapper = SpringContextHolder.getBean("baseUserLogMapper"); BaseUserLog log = new BaseUserLog(); log.setLogType(2); log.setUser(user); log.setDescription("退出成功"); baseUserLogMapper.insert(log); SessionAttributeListener.sessionMap.remove(name); } } } }
时间: 2024-11-06 13:02:27