package com.whyc.aop; import com.whyc.constant.UserOperation; import com.whyc.service.OperationLogService; import com.whyc.util.ActionUtil; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.Signature; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Pointcut; import org.aspectj.lang.reflect.MethodSignature; 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 javax.servlet.http.HttpServletRequest; import java.util.Date; import java.util.HashMap; /** * 定义aop,对特定的接口进行内容捕捉,生成操作日志 * 切面为controller * 切点为Point表达式 * 具体连接点为JoinPoint * 处理方式为doAfterReturning */ @Component @Aspect public class OperationLogAspect { @Autowired private OperationLogService service; /**定义切点*/ @Pointcut(value = "execution(public * com.whyc..controller.*.add*(..))" + "|| execution(public * com.whyc..controller.*.delete*(..))" + "|| execution(public * com.whyc..controller.*.update*(..))" ) private void operationLogPointcut(){}; /**aop处理类,对捕捉到的切点接口,进行数据处理*/ @AfterReturning(pointcut = "operationLogPointcut()",returning = "resp") public void doAfterReturnOperation(JoinPoint point,Object resp){ //用户id Long uId = ActionUtil.getUser().getUId(); Signature signature = point.getSignature(); String methodSignature = signature.toString(); String methodName = signature.getName(); //执行的类全名 String fullClassName = signature.getDeclaringTypeName(); Integer operationType = 0; String operationTypeName = null; String operationTypeNameEn = null; if (methodName.contains("update")) { //提取单项-修改配置 if (fullClassName.contains("pageParam")) { operationTypeName = UserOperation.TYPE_PARAM_CHANGE.getTypeName(); operationTypeNameEn = UserOperation.TYPE_PARAM_CHANGE.getTypeNameEn(); operationType = UserOperation.TYPE_PARAM_CHANGE.getType(); } //提取单项-密码修改 else if (fullClassName.contains("updatePassword")) { operationTypeName = UserOperation.TYPE_PASSWORD_CHANGE.getTypeName(); operationTypeNameEn = UserOperation.TYPE_PASSWORD_CHANGE.getTypeNameEn(); operationType = UserOperation.TYPE_PASSWORD_CHANGE.getType(); } else { operationTypeName = UserOperation.TYPE_UPDATE.getTypeName(); operationTypeNameEn = UserOperation.TYPE_UPDATE.getTypeNameEn(); operationType = UserOperation.TYPE_UPDATE.getType(); } }else if (methodName.contains("add")){ operationTypeName = UserOperation.TYPE_ADD.getTypeName(); operationTypeNameEn = UserOperation.TYPE_ADD.getTypeNameEn(); operationType = UserOperation.TYPE_ADD.getType(); }else if (methodName.contains("delete")){ operationTypeName = UserOperation.TYPE_DELETE.getTypeName(); operationTypeNameEn = UserOperation.TYPE_DELETE.getTypeNameEn(); operationType = UserOperation.TYPE_DELETE.getType(); }/*else if (methodName.contains("login")){ operationTypeName = UserOperation.TYPE_LOGIN.getTypeName(); operationType = UserOperation.TYPE_LOGIN.getType(); }else if (methodName.contains("logout")){ operationTypeName = UserOperation.TYPE_LOGOUT.getTypeName(); operationType = UserOperation.TYPE_LOGOUT.getType(); }*/ //获取类型 String[] fullClassNameSplit = fullClassName.split("\\."); String classNameTrue = fullClassNameSplit[fullClassNameSplit.length - 1]; String module = "模块"; String app = "-安卓端"; String moduleEnus = "module"; String appEnus = "-android"; String className = classNameTrue.replace("Controller:", module); String classNameEnus = classNameTrue.replace("Controller:", moduleEnus); //排除语音触发的日志记录 if (!classNameTrue.equals("VoiceController")) { if (fullClassName.contains("app")) { className += app; classNameEnus += appEnus; } //操作时间 Date operationTime = new Date(); //客户端ip HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest(); String terminalIp = request.getRemoteAddr(); //操作信息 //获取切点方法信息 String[] parameterNames = ((MethodSignature) signature).getParameterNames(); Object[] args = point.getArgs(); HashMap map = new HashMap<>(); for (int i = 0; i < parameterNames.length; i++) { if (args[i] != null) { map.put(parameterNames[i], args[i].toString()); } } //人脸接口的添加参数是图片的base64字符串,不存储 if (methodSignature.contains("FaceController.add") || methodSignature.contains("FaceController.update")) { map = new HashMap<>(); } String operationMsg = "执行了" + className + "的" + operationTypeName + "操作."; String operationDetail = "具体调用方法为:" + methodName + ",具体参数为:" + map.toString(); String operationMsgEnUs = "executed:" + classNameEnus + " " + operationTypeNameEn + " operation "; String operationDetailEnUs = "Specific calling method is " + methodName + ",Specific parameters are " + map.toString(); service.record(uId, operationType, operationTime, terminalIp, operationMsg, operationDetail, operationMsgEnUs, operationDetailEnUs); } } }