Jelajahi Sumber

登录系统日志

git-svn-id: https://192.168.57.71/svn/jsgkj@1179 931142cf-59ea-a443-aa0e-51397b428577
ld_zhouk 8 tahun lalu
induk
melakukan
46632bcc19

+ 7 - 4
gkaqv2/trunk/modules/web/src/main/java/com/xt/js/gkaq/common/log/aspect/SysLogAspect.java

@@ -25,6 +25,8 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
+import org.springframework.web.context.request.RequestContextHolder;
+import org.springframework.web.context.request.ServletRequestAttributes;
 
 import com.xt.js.gkaq.common.Constants;
 import com.xt.js.gkaq.common.ex.BusinessException;
@@ -124,9 +126,8 @@ public class SysLogAspect {
                     logMsg += "[发生异常:" + ex.getMessage() + "]";
                 }
             }
-            HttpServletRequest request = null;
-            String ip = "";//IpUtil.getIpAddr(request);
-            logger.info("生成系统日志[日志类型:{},日志路径:{},日志参数:{},日志描述:{},客户端IP:{}]", logType, logPath, logParam, logMsg);
+            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
+            String clientIp = IpUtil.getIpAddr(request);
 
             // 数据库日志
             SysLogModel log = new SysLogModel();
@@ -134,9 +135,11 @@ public class SysLogAspect {
             log.setLogPath(logPath);
             log.setLogParam(logParam);
             log.setLogMsg(logMsg);
-            log.setClientIp(ip);
+            log.setClientIp(clientIp);
             // 保存数据库
             sysLogService.save(log);
+            logger.info("生成系统日志[日志类型:{},日志路径:{},日志参数:{},日志描述:{},客户端IP:{}]",
+                    logType, logPath, logParam, logMsg, clientIp);
         } catch (Exception exp) {
             logger.error("生成系统日志发生异常:", exp);
         }

+ 1 - 1
gkaqv2/trunk/modules/web/src/main/java/com/xt/js/gkaq/common/util/IpUtil.java

@@ -32,7 +32,7 @@ public class IpUtil {
         }
         if (unknownIp(ip)) {
             ip = request.getRemoteAddr();
-            if (ip.equals("127.0.0.1")) {
+            if ("127.0.0.1".equals(ip) || "0:0:0:0:0:0:0:1".equals(ip)) {
                 // ¸ù¾ÝÍø¿¨È¡±¾»úÅäÖõÄIP
                 InetAddress inet = null;
                 try {

+ 63 - 14
gkaqv2/trunk/modules/web/src/main/java/com/xt/js/gkaq/common/util/UserSessionListener.java

@@ -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);
     }
 }

+ 2 - 1
gkaqv2/trunk/modules/web/src/main/java/com/xt/js/gkaq/web/ctl/UserCtl.java

@@ -129,7 +129,8 @@ public class UserCtl extends BaseCtl {
         } else {
             // ¸üÐÂ
             UserModel model = userService.findById(vo.getId());
-            BeanUtils.copyProperties(vo, model);
+            // TODO
+            BeanUtils.copyProperties(vo, model, new String[] { "loginPassword" });
             userService.update(model);
             UserInfoModel userInfoModel = new UserInfoModel();
             BeanUtils.copyProperties(vo, userInfoModel);

+ 5 - 1
gkaqv2/trunk/modules/web/src/main/resources/spring/spring-shiro.xml

@@ -33,6 +33,9 @@
 		<property name="hashAlgorithmName" value="SHA-256" />
 	</bean>
 	<bean id="gkaqFormAuthenticationFilter" class="com.xt.js.gkaq.frame.system.GkaqFormAuthenticationFilter"></bean>
+	<!-- <bean id="gkaqLogoutFilter" class="com.xt.js.gkaq.frame.system.GkaqLogoutFilter">
+		<property name="redirectUrl" value="/login" />
+	</bean> -->
 	<bean id="captchaValidateFilter"   class="com.xt.js.gkaq.frame.system.CaptchaValidateFilter">  
 	    <property name="captchaEbabled" value="false"/>  
 	    <property name="captchaParam" value="captcha"/>  
@@ -45,7 +48,8 @@
 		<property name="filters">
 			<map>
 				<entry key="authc" value-ref="gkaqFormAuthenticationFilter" />
-				<entry key="captchaValidate" value-ref="captchaValidateFilter"/>  
+				<entry key="captchaValidate" value-ref="captchaValidateFilter"/>
+				<!-- <entry key="logout" value-ref="gkaqLogoutFilter" /> -->
 			</map>
 		</property>
 		<property name="filterChainDefinitions">

+ 1 - 1
gkaqv2/trunk/modules/web/src/main/webapp/WEB-INF/view/layouts/navbar.jsp

@@ -26,7 +26,7 @@
 		<img src="<%=base%>/static/images/map/index_home.png" class="h_sysimg"/>
 		<img src="<%=base%>/static/images/map/map.png" class="h_sysimg"/>
 		<img src="<%=base%>/static/images/map/help.png" class="h_sysimg"/>
-		<img src="<%=base%>/static/images/map/index_close.png" class="h_sysimg"/>
+		<a href="#" onclick="linkUrl('logout')"><img src="<%=base%>/static/images/map/index_close.png" class="h_sysimg"/></a>
 	</div>
 </div>
 <div class="h_usermenu">

+ 5 - 1
gkaqv2/trunk/modules/web/src/main/webapp/WEB-INF/web.xml

@@ -86,9 +86,13 @@
 		<load-on-startup>99</load-on-startup>
 	</servlet>
 
+	<listener>
+	  <listener-class>com.xt.js.gkaq.common.util.UserSessionListener</listener-class>
+	</listener>
+
 	<!-- session过期时间 -->
 	<session-config>
-		<session-timeout>120</session-timeout>
+		<session-timeout>5</session-timeout>
 	</session-config>
 
 	<!-- 错误页面设置 -->