|
@@ -0,0 +1,145 @@
|
|
|
+package com.hb.proj.allconfig;
|
|
|
+
|
|
|
+import java.lang.reflect.Method;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+import java.util.regex.Matcher;
|
|
|
+import java.util.regex.Pattern;
|
|
|
+
|
|
|
+import org.aspectj.lang.JoinPoint;
|
|
|
+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.boot.autoconfigure.condition.ConditionalOnProperty;
|
|
|
+import org.springframework.expression.ExpressionParser;
|
|
|
+import org.springframework.expression.spel.standard.SpelExpressionParser;
|
|
|
+import org.springframework.expression.spel.support.StandardEvaluationContext;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+import org.springframework.web.context.request.RequestContextHolder;
|
|
|
+import org.springframework.web.context.request.ServletRequestAttributes;
|
|
|
+
|
|
|
+import com.hb.proj.model.OperationLog;
|
|
|
+import com.hb.proj.model.User;
|
|
|
+import com.hb.proj.sys.service.OperationLogService;
|
|
|
+import com.hb.proj.utils.JacksonUtils;
|
|
|
+import com.hb.proj.utils.RespVO;
|
|
|
+
|
|
|
+import jakarta.servlet.http.HttpServletRequest;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 系统日志切面类
|
|
|
+ * @author cwen
|
|
|
+ *
|
|
|
+ */
|
|
|
+
|
|
|
+@Aspect
|
|
|
+@Component
|
|
|
+@ConditionalOnProperty(prefix = "spring.syslog.aspect",name = "active")
|
|
|
+public class SysLogAspect {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private OperationLogService service;
|
|
|
+
|
|
|
+ @Pointcut("@annotation(com.hb.proj.allconfig.SysLog)")
|
|
|
+ public void logPoincut() {
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 切点通知配置,获取方法信息,调用上下文等作为日志内容,记录到库
|
|
|
+ * @param joinPoint
|
|
|
+ */
|
|
|
+ @AfterReturning(pointcut="logPoincut()",returning="respVO")
|
|
|
+ public void saveSysLog(JoinPoint joinPoint,Object respVO) {
|
|
|
+
|
|
|
+ if(respVO instanceof RespVO<?>) {
|
|
|
+ if(((RespVO<?>)respVO).getCode()!=0) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ MethodSignature signature = (MethodSignature) joinPoint.getSignature();
|
|
|
+ Method method = signature.getMethod();
|
|
|
+ SysLog sysLog = method.getAnnotation(SysLog.class);
|
|
|
+
|
|
|
+ if(sysLog==null) { //没有指定注解的方法,不需要记录日志
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
|
|
|
+ HttpServletRequest request = servletRequestAttributes.getRequest();
|
|
|
+
|
|
|
+ Object[] args = joinPoint.getArgs();
|
|
|
+
|
|
|
+ OperationLog log=new OperationLog(request.getHeader(CacheConfig.TOKEN_HEADER_NAME),format(sysLog.value(),args));
|
|
|
+
|
|
|
+ String clsMthd=joinPoint.getTarget().getClass().getName()+"."+method.getName();
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ log.setCallInfo(clsMthd+"("+JacksonUtils.getJSON(args)+")"); //记录调用的方法信息:类名.方法名|参数
|
|
|
+
|
|
|
+ log.setReqUri(request.getRequestURI());
|
|
|
+
|
|
|
+ service.add(log);
|
|
|
+ }
|
|
|
+
|
|
|
+ public String format(String msg,Object[] args) {
|
|
|
+ if(args==null||args.length==0) {
|
|
|
+ return msg;
|
|
|
+ }
|
|
|
+
|
|
|
+ List<String> holders=extractStrs(msg,"\\{([\\w\\.]+)\\}");
|
|
|
+ if(holders==null||holders.size()==0) {
|
|
|
+ return msg;
|
|
|
+ }
|
|
|
+
|
|
|
+ StandardEvaluationContext context = new StandardEvaluationContext(args);
|
|
|
+ ExpressionParser parser = new SpelExpressionParser();
|
|
|
+ Object val=null;
|
|
|
+ for(String holder : holders) {
|
|
|
+ val=parser.parseExpression(holder.replaceFirst("arg(\\d+)", "[$1]")).getValue(context);
|
|
|
+ msg=msg.replace("{"+holder+"}", val!=null?val.toString():"");
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ return msg;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ //提取所有符合要求的字符串
|
|
|
+ public List<String> extractStrs(String src,String exp){
|
|
|
+ if(src==null||exp==null){
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ List<String> extractAry=new ArrayList<String>();
|
|
|
+ Pattern p=Pattern.compile(exp);
|
|
|
+ Matcher m=p.matcher(src);
|
|
|
+ while(m.find()){
|
|
|
+ extractAry.add(m.group(1));
|
|
|
+ }
|
|
|
+ return extractAry.size()>0?extractAry:null;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void main(String[] args) {
|
|
|
+ SysLogAspect log=new SysLogAspect();
|
|
|
+ User us=new User();
|
|
|
+ us.setUserName("李四");
|
|
|
+ Object[] params=new Object[] {us,"张三","历史"};
|
|
|
+ System.out.println(log.format("更新数据{arg0.userName},{arg1}", params));
|
|
|
+
|
|
|
+ /*
|
|
|
+ StandardEvaluationContext context = new StandardEvaluationContext(params);
|
|
|
+ ExpressionParser parser = new SpelExpressionParser();
|
|
|
+ Expression expression=parser.parseExpression("'更新数据'+[0].userName");
|
|
|
+ System.out.println(expression.getValue(context));
|
|
|
+ */
|
|
|
+
|
|
|
+ //System.out.println(log.extractStrs("更新数据{arg0.userName}和编号{arg1}","\\{([\\w\\.]+)\\}"));
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+}
|