package com.manage.controller; /** *

Title: LogAopAction.java

*

Description: 操作日志Controller

*/ import com.manage.annotation.OptionalLog; import com.manage.entity.Power_Log; import com.manage.service.LogService; import com.manage.util.ExceptionPrintUtil; import org.apache.commons.lang3.CharUtils; import org.apache.commons.lang3.StringUtils; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.Signature; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Pointcut; import org.aspectj.lang.reflect.MethodSignature; import org.springframework.core.DefaultParameterNameDiscoverer; import org.springframework.stereotype.Component; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.util.HashMap; import java.util.Map; @Aspect @Component public class LogAopAction { // 注入service,用来将日志信息保存在数据库 @Resource private LogService logService; // 配置接入点,即为所要记录的action操作目录 @Pointcut("@annotation(com.manage.annotation.OptionalLog)") private void controllerAspect() { } @Around("controllerAspect()") public Object around(ProceedingJoinPoint pjp) throws Throwable { HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()) .getRequest(); // 拦截的实体类,就是当前正在执行的controller Object target = pjp.getTarget(); // 拦截的方法名称。当前正在执行的方法 String methodName = pjp.getSignature().getName(); // 拦截的放参数类型 Signature sig = pjp.getSignature(); MethodSignature msig = null; if (!(sig instanceof MethodSignature)) { throw new IllegalArgumentException("该注解只能用于方法"); } msig = (MethodSignature) sig; Class[] parameterTypes = msig.getMethod().getParameterTypes(); Object object = null; // 获得被拦截的方法 Method method = null; try { method = target.getClass().getMethod(methodName, parameterTypes); } catch (NoSuchMethodException e1) { e1.printStackTrace(); } if (null != method) { //插入表操作 insertLog(method,pjp); } object = pjp.proceed(); //接受客户端的数据 Map map = request.getParameterMap(); // 解决获取参数乱码 Map newmap = new HashMap(); for(Map.Entry entry : map.entrySet()){ String name = entry.getKey(); String[] values = entry.getValue(); if(values==null){ newmap.put(name, new String[]{}); continue; } String[] newvalues = new String[values.length]; for(int i=0; i obj = logService.selectObjectByKey(tableName, sqlWhere); Object o = null; if(obj != null){ o = obj.get(fieldName); String remark = ""; //第一个字段 if(o != null){ remark = obj.get(fieldName).toString(); }else{ //否则取备用字段 fieldName = propertyToField(op.fieldName1()); if(StringUtils.isNotBlank(fieldName)){ o = obj.get(fieldName); if(o != null){ remark = obj.get(fieldName).toString(); } } } logBo.setRemark(remark); } } } // 获取注解的modules 设为操作模块 logBo.setLogTitle(module); // 获取注解的methods 设为执行方法 logBo.setLogContent(op.methods()); // 添加到数据库 logService.insert(logBo); } catch (Exception e) { ExceptionPrintUtil.printException(e); e.printStackTrace(); throw new RuntimeException(); } } } }