shuzheng 9 лет назад
Родитель
Сommit
4c356ed6d0

+ 98 - 0
zheng-upms/zheng-upms-client/src/main/java/com/zheng/upms/client/interceptor/LogAspect.java

@@ -0,0 +1,98 @@
+package com.zheng.upms.client.interceptor;
+
+import com.zheng.common.util.RequestUtil;
+import com.zheng.upms.dao.model.UpmsLog;
+import com.zheng.upms.rpc.api.UpmsApiService;
+import io.swagger.annotations.ApiOperation;
+import org.apache.commons.lang.ObjectUtils;
+import org.apache.shiro.authz.annotation.RequiresPermissions;
+import org.aspectj.lang.JoinPoint;
+import org.aspectj.lang.ProceedingJoinPoint;
+import org.aspectj.lang.Signature;
+import org.aspectj.lang.annotation.After;
+import org.aspectj.lang.annotation.Around;
+import org.aspectj.lang.annotation.Aspect;
+import org.aspectj.lang.annotation.Before;
+import org.aspectj.lang.reflect.MethodSignature;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.context.request.RequestAttributes;
+import org.springframework.web.context.request.RequestContextHolder;
+import org.springframework.web.context.request.ServletRequestAttributes;
+
+import javax.servlet.http.HttpServletRequest;
+import java.lang.reflect.Method;
+
+/**
+ * 日志记录AOP实现
+ * Created by ZhangShuzheng on 2017/3/14.
+ */
+@Aspect
+public class LogAspect {
+
+	private static Logger _log = LoggerFactory.getLogger(LogAspect.class);
+
+	// 开始时间
+	private long startTime = 0L;
+	// 结束时间
+	private long endTime = 0L;
+
+	@Autowired
+	UpmsApiService upmsApiService;
+
+	@Before("execution(* *..controller..*.*(..))")
+	public void doBeforeInServiceLayer(JoinPoint joinPoint) {
+		_log.debug("doBeforeInServiceLayer");
+		startTime = System.currentTimeMillis();
+	}
+
+	@After("execution(* *..controller..*.*(..))")
+	public void doAfterInServiceLayer(JoinPoint joinPoint) {
+		_log.debug("doAfterInServiceLayer");
+	}
+
+	@Around("execution(* *..controller..*.*(..))")
+	public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
+		// 获取request
+		RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
+		ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) requestAttributes;
+		HttpServletRequest request = servletRequestAttributes.getRequest();
+
+		UpmsLog upmsLog = new UpmsLog();
+		// 从注解中获取操作名称、获取响应结果
+		Object result = pjp.proceed();
+		Signature signature = pjp.getSignature();
+		MethodSignature methodSignature = (MethodSignature) signature;
+		Method method = methodSignature.getMethod();
+		if (method.isAnnotationPresent(ApiOperation.class)) {
+			ApiOperation log = method.getAnnotation(ApiOperation.class);
+			upmsLog.setDescription(log.value());
+		}
+		if (method.isAnnotationPresent(RequiresPermissions.class)) {
+			RequiresPermissions permissions = method.getAnnotation(RequiresPermissions.class);
+			upmsLog.setPermissions(ObjectUtils.toString(permissions.value()));
+		}
+		endTime = System.currentTimeMillis();
+		_log.debug("doAround>>>result={},耗时:{}", result, endTime - startTime);
+
+		upmsLog.setBasePath(RequestUtil.getBasePath(request));
+		upmsLog.setIp(RequestUtil.getIpAddr(request));
+		upmsLog.setMethod(request.getMethod());
+		if (request.getMethod().equalsIgnoreCase("GET")) {
+			upmsLog.setParameter(request.getQueryString());
+		} else {
+			upmsLog.setParameter(ObjectUtils.toString(request.getParameterMap()));
+		}
+		upmsLog.setResult(ObjectUtils.toString(result));
+		upmsLog.setSpendTime((int) (endTime - startTime));
+		upmsLog.setStartTime(startTime);
+		upmsLog.setUri(request.getRequestURI());
+		upmsLog.setUrl(ObjectUtils.toString(request.getRequestURL()));
+		upmsLog.setUserAgent(request.getHeader("User-Agent"));
+		upmsLog.setUsername(ObjectUtils.toString(request.getUserPrincipal()));
+		upmsApiService.insertUpmsLogSelective(upmsLog);
+		return result;
+	}
+
+}

+ 0 - 25
zheng-upms/zheng-upms-client/src/main/java/com/zheng/upms/client/shiro/session/UpmsSessionFactory.java

@@ -27,29 +27,4 @@ public class UpmsSessionFactory implements SessionFactory {
         return session;
     }
 
-    /**
-     * 获取ip工具类,除了getRemoteAddr,其他ip均可伪造
-     * @param request
-     * @return
-     */
-    public String getIpAddr(HttpServletRequest request) {
-        String ip = request.getHeader("Cdn-Src-Ip");    // 网宿cdn的真实ip
-        if (ip == null || ip.length() == 0 || " unknown".equalsIgnoreCase(ip)) {
-            ip = request.getHeader("HTTP_CLIENT_IP");   // 蓝讯cdn的真实ip
-        }
-        if (ip == null || ip.length() == 0 || " unknown".equalsIgnoreCase(ip)) {
-            ip = request.getHeader("X-Forwarded-For");  // 获取代理ip
-        }
-        if (ip == null || ip.length() == 0 || " unknown".equalsIgnoreCase(ip)) {
-            ip = request.getHeader("Proxy-Client-IP"); // 获取代理ip
-        }
-        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
-            ip = request.getHeader("WL-Proxy-Client-IP"); // 获取代理ip
-        }
-        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
-            ip = request.getRemoteAddr(); // 获取真实ip
-        }
-        return ip;
-    }
-
 }

+ 15 - 0
zheng-upms/zheng-upms-client/src/main/resources/springMVC-servlet.xml

@@ -0,0 +1,15 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<beans xmlns="http://www.springframework.org/schema/beans"
+       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+       xmlns:aop="http://www.springframework.org/schema/aop"
+       xsi:schemaLocation="http://www.springframework.org/schema/beans
+        http://www.springframework.org/schema/beans/spring-beans.xsd
+
+         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
+
+    <aop:aspectj-autoproxy proxy-target-class="true"/>
+
+    <!-- 日志记录AOP实现 -->
+    <bean class="com.zheng.upms.client.interceptor.LogAspect"/>
+
+</beans>