实际业务,有时候需要记录服务的操作日志,我们可以利用SpringAOP 切面来拦截记录用户操作;用户使用session或者前端传值都可以。
1.创建日志记录接口首先我们得有一个接口,这个接口可以记录用户访问服务器的方法
例:
//合并
import java.lang.annotation.*;
@Target(ElementType.METHOD) //注解放置的目标位置,METHOD是可注解在方法级别上
@Retention(RetentionPolicy.RUNTIME) //注解在哪个阶段执行
@Documented //生成文档
public @interface MyLog {
String value() default "";
}
2.使用接口
注解使用
注:你想捕获那个方法用户操作,就给那个方法加注解,不需要的忽略
@GetMapping("/deleteServiceByServNo")
@MyLog(value = "删除服务")
public ReturnMsg deleteServiceByServNo(@RequestParam("servNo")String servNo){
interfaceConfigurationService.deleteServiceByServNo(servNo);
return new ReturnMsg("200","success");
}
3.配置切面类 通知
package com.df.security.config;
//合并
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.commons.lang3.ArrayUtils;
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.stereotype.Component;
import org.springframework.util.StreamUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import com.alibaba.fastjson.JSON;
import com.df.bean.SysLog;
import com.df.bean.User;
import com.df.service.IpUtil;
import com.df.service.SysLogService;
import java.util.stream.*;
@Aspect
@Component
public class SysLogAspect {
@Autowired
private SysLogService sysLogService;
@Pointcut("@annotation(MyLog)")
public void logPoinCut() {
}
//切面 配置通知
@AfterReturning("logPoinCut()")
public void saveSysLog(JoinPoint joinPoint) {
ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
SysLog sysLog = new SysLog();
//从切面织入点处通过反射机制获取织入点处的方法
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
//获取切入点所在的方法
Method method = signature.getMethod();
System.out.println("获取的方法有:"+method);
//获取操作
MyLog myLog = method.getAnnotation(MyLog.class);
if (myLog != null) {
String value = myLog.value();
sysLog.setOperation(value);//保存获取的操作
System.out.println("操作方法名:"+value);
}
//获取请求的类名
String className = joinPoint.getTarget().getClass().getName();
//获取请求的方法名
String methodName = method.getName();
sysLog.setMethod(className + "." + methodName);
System.out.println("获取请求的方法名有:"+methodName);
//请求的参数
Object[] args = joinPoint.getArgs();
//过滤HttpServletRequest和HttpServletResponse类型的参数
List<Object> logArgs = streamOf(args)
.filter(arg -> (!(arg instanceof HttpServletRequest) && !(arg instanceof HttpServletResponse)))
.collect(Collectors.toList());
String argStr = JSON.toJSONString(logArgs);
//将参数所在的数组转换成json
//String params = JSON.toJSONString(args);
//sysLog.setParams(params);
sysLog.setParams(argStr);
//获取操作用户
HttpServletRequest request = requestAttributes.getRequest();
/*HttpSession session = request.getSession();
User user=(User) session.getAttribute("user");
sysLog.setUserName(user.getUserName());*/
/*HttpSession session = request.getSession();
String userName= (String) session.getAttribute("userName");
sysLog.setUserName(userName);*/
String userName = request.getHeader("userName");
if(userName == null || "".equals(userName)){
sysLog.setUserName("dsgc");
}else{
sysLog.setUserName(userName);
}
//sysLog.setUserName("admin");
User user = (User)request.getSession().getAttribute("user");
System.out.println("***操作用戶為***:"+user);
if(user!=null){
sysLog.setUserName(user.getUserName());
}
//获取日期
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String creationDate=df.format(new Date());
sysLog.setOpertionDate(creationDate);
//获取ip
String ip=IpUtil.getIpAddr(request);
sysLog.setIp(ip);
//插入本地捕获用户操作日志
sysLogService.insertSysLog(sysLog);
}
/* public static void main(String[] args){
SysLogAspect sysLogAspect = new SysLogAspect();
//sysLogAspect.saveSysLog();
}*/
public static <T> Stream<T> streamOf(T[] array) {
return ArrayUtils.isEmpty(array) ? Stream.empty() : Arrays.asList(array).stream();
}
}
4.效果图
祝你幸福
送你一首歌:《还有我》 任贤齐
附图:解放J7