|
@@ -1,29 +1,78 @@
|
|
|
package com.xt.js.gkaq.common.util;
|
|
|
|
|
|
-import java.util.Date;
|
|
|
+import javax.servlet.ServletContext;
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
+import javax.servlet.http.HttpSession;
|
|
|
+import javax.servlet.http.HttpSessionEvent;
|
|
|
+import javax.servlet.http.HttpSessionListener;
|
|
|
|
|
|
-import javax.servlet.http.HttpSessionBindingEvent;
|
|
|
-import javax.servlet.http.HttpSessionBindingListener;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+import org.springframework.context.ApplicationContext;
|
|
|
+import org.springframework.web.context.request.RequestAttributes;
|
|
|
+import org.springframework.web.context.request.RequestContextHolder;
|
|
|
+import org.springframework.web.context.request.ServletRequestAttributes;
|
|
|
+import org.springframework.web.context.support.WebApplicationContextUtils;
|
|
|
+
|
|
|
+import com.xt.js.gkaq.common.Constants;
|
|
|
+import com.xt.js.gkaq.frame.model.SysLogModel;
|
|
|
+import com.xt.js.gkaq.frame.model.UserModel;
|
|
|
+import com.xt.js.gkaq.frame.service.SysLogService;
|
|
|
|
|
|
/**
|
|
|
* 用户会话监听
|
|
|
*/
|
|
|
-public class UserSessionListener implements HttpSessionBindingListener {
|
|
|
- int uid;
|
|
|
+public class UserSessionListener implements HttpSessionListener {
|
|
|
+ // 日志对象
|
|
|
+ private static final Logger logger = LoggerFactory.getLogger(UserSessionListener.class);
|
|
|
|
|
|
- public int getUid() {
|
|
|
- return uid;
|
|
|
+ @Override
|
|
|
+ public void sessionCreated(HttpSessionEvent event) {
|
|
|
+ logger.debug("新建会话!");
|
|
|
}
|
|
|
|
|
|
- public void setUid(int uid) {
|
|
|
- this.uid = uid;
|
|
|
- }
|
|
|
+ @Override
|
|
|
+ public void sessionDestroyed(HttpSessionEvent event) {
|
|
|
+ // 登出日志
|
|
|
+ HttpSession session = event.getSession();
|
|
|
+ UserModel user = (UserModel) session.getAttribute(Constants.SESSION_KEY_USER);
|
|
|
+ if (user == null) {
|
|
|
+ // 未登录,不记录日志。
|
|
|
+ logger.debug("会话过期!");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ SysLogService sysLogService = (SysLogService) getObjectFromApplication(session.getServletContext());
|
|
|
+ if (sysLogService == null) {
|
|
|
+ logger.debug("日志对象为空!");
|
|
|
+ return;
|
|
|
+ }
|
|
|
|
|
|
- public void valueBound(HttpSessionBindingEvent arg0) {
|
|
|
- System.out.println(uid + "登录了" + new Date());
|
|
|
+ String clientIp = "";
|
|
|
+ RequestAttributes reqAttrs = RequestContextHolder.getRequestAttributes();
|
|
|
+ if (reqAttrs != null) {
|
|
|
+ HttpServletRequest request = ((ServletRequestAttributes) reqAttrs).getRequest();
|
|
|
+ clientIp = IpUtil.getIpAddr(request);
|
|
|
+ } else {
|
|
|
+ clientIp = (String) session.getAttribute(Constants.SESSION_KEY_IP);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 数据库日志
|
|
|
+ SysLogModel log = new SysLogModel();
|
|
|
+ log.setLogType(Constants.LOG_TYPE_4);
|
|
|
+ log.setLogPath(UserSessionListener.class.getName());
|
|
|
+ log.setLogParam("");
|
|
|
+ log.setLogMsg("用户:[" + user.getLoginName() + "|" + user.getRealName() + "]登出系统!");
|
|
|
+ log.setClientIp(clientIp);
|
|
|
+ // 保存数据库
|
|
|
+ sysLogService.add(log, user);
|
|
|
+ logger.info("生成系统日志[日志描述:{},客户端IP:{}]", log.getLogMsg(), clientIp);
|
|
|
}
|
|
|
|
|
|
- public void valueUnbound(HttpSessionBindingEvent arg0) {
|
|
|
- System.out.println(uid + "下线了" + new Date());
|
|
|
+ private Object getObjectFromApplication(ServletContext servletContext) {
|
|
|
+ // 通过WebApplicationContextUtils 得到Spring容器的实例。
|
|
|
+ ApplicationContext application = WebApplicationContextUtils.getWebApplicationContext(servletContext);
|
|
|
+
|
|
|
+ return application.getBean(SysLogService.class);
|
|
|
}
|
|
|
}
|