springboot—spring aop 实现系统操作日志记录存储到数据库
原文:
https://www.jianshu.com/p/d0bbdf1974bd
采用方案: 使用spring 的 aop 技术切到自定义注解上,针对不同注解标志进行参数解析,记录日志
缺点是要针对每个不同的注解标志进行分别取注解标志,获取参数进行日志记录输出
1. 需要引用的依赖
<!--spring切面aop依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
在application.properties文件里加这样一条配置
spring.aop.auto=true //这个配置我的例子中没有加 也正常运行
2. 创建实体类
public class SysLog implements Serializable {
private Long id;
private String username; //用户名
private String operation; //操作
private String method; //方法名
private String params; //参数
private String ip; //ip地址
private Date createDate; //操作时间
//创建getter和setter方法
}
3. 使用spring 的 aop 技术切到自定义注解上,所以先创建一个自定义注解类
import java.lang.annotation.*;
/**
* 自定义注解类
*/
@Target(ElementType.METHOD) //注解放置的目标位置,METHOD是可注解在方法级别上
@Retention(RetentionPolicy.RUNTIME) //注解在哪个阶段执行
@Documented //生成文档
public @interface MyLog {
String value() default "";
}
4. 创建aop切面实现类
import com.alibaba.fastjson.JSON;
import com.qfedu.rongzaiboot.annotation.MyLog;
import com.qfedu.rongzaiboot.entity.SysLog;
import com.qfedu.rongzaiboot.service.SysLogService;
import com.qfedu.rongzaiboot.utils.HttpContextUtils;
import com.qfedu.rongzaiboot.utils.IPUtils;
import com.qfedu.rongzaiboot.utils.ShiroUtils;
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 javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.Date;
/**
* 系统日志:切面处理类
*/
@Aspect
@Component
public class SysLogAspect {
@Autowired
private SysLogService sysLogService;
//定义切点 @Pointcut
//在注解的位置切入代码
@Pointcut("@annotation( com.qfedu.rongzaiboot.annotation.MyLog)")
public void logPoinCut() {
}
//切面 配置通知
@AfterReturning("logPoinCut()")
public void saveSysLog(JoinPoint joinPoint) {
System.out.println("切面。。。。。");
//保存日志
SysLog sysLog = new SysLog();
//从切面织入点处通过反射机制获取织入点处的方法
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
//获取切入点所在的方法
Method method = signature.getMethod();
//获取操作
MyLog myLog = method.getAnnotation(MyLog.class);
if (myLog != null) {
String value = myLog.value();
sysLog.setOperation(value);//保存获取的操作
}
//获取请求的类名
String className = joinPoint.getTarget().getClass().getName();
//获取请求的方法名
String methodName = method.getName();
sysLog.setMethod(className + "." + methodName);
//请求的参数
Object[] args = joinPoint.getArgs();
Object[] arguments = new Object[args.length];
for (int i = 0; i < args.length; i++) {
if (args[i] instanceof ServletRequest || args[i] instanceof ServletResponse || args[i] instanceof MultipartFile) {
//ServletRequest不能序列化,从入参里排除,否则报异常:java.lang.IllegalStateException: It is illegal to call this method if the current request is not in asynchronous mode (i.e. isAsyncStarted() returns false)
//ServletResponse不能序列化 从入参里排除,否则报异常:java.lang.IllegalStateException: getOutputStream() has already been called for this response
continue;
}
arguments[i] = args[i];
}
String paramter = "";
if (arguments != null) {
try {
paramter = JSONObject.toJSONString(arguments);
} catch (Exception e) {
paramter = arguments.toString();
}
}
sysLog.setParams(paramter);
sysLog.setCreateDate(new Date());
//获取用户名
sysLog.setUsername(ShiroUtils.getUserEntity().getUsername());
//获取用户ip地址
HttpServletRequest request = HttpContextUtils.getHttpServletRequest();
sysLog.setIp(IPUtils.getIpAddr(request));
//调用service保存SysLog实体类到数据库
sysLogService.save(sysLog);
}
}
5. 接下来就可以在需要监控的方法上添加 aop的自定义注解
**
格式为 @+自定义注解的类名 @MyLog
**
在这里插入代码片//例如在contoller类的方法上加注解
@RestController
@RequestMapping("/sys/menu")
public class SysMenuController extends AbstractController {
@Autowired
private SysMenuService sysMenuService;
@MyLog(value = "删除菜单记录") //这里添加了AOP的自定义注解
@PostMapping("/del")
public R deleteBatch(@RequestBody Long[] menuIds) {
for (Long menuId : menuIds) {
if (menuId <= 31) {
return R.error("系统菜单,不能删除");
}
}
sysMenuService.deleteBatch(menuIds);
return R.ok("删除成功");
}
}
6. 执行上面的方法操作后,会将记录保存到数据库
涉及到的util类:
HttpContextUtils
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
public class HttpContextUtils {
public static HttpServletRequest getHttpServletRequest() {
return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
}
}
IPUtils
import javax.servlet.http.HttpServletRequest;
public class IPUtils {
public static String getIpAddr(HttpServletRequest request) {
String ip = request.getHeader("x-real-ip");
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("x-forwarded-for");
if (ip != null) {
ip = ip.split(",")[0].trim();
}
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("WL-Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
}
return ip;
}
}