七仔的博客

七仔的博客GithubPages分博

0%

SpringBoot使用切面实现日志记录

自定义注解,使用Spring AOP实现日志记录

SpringBoot使用切面实现日志记录

首先是自定义日志记录注解

1
2
3
4
5
6
7
8
import java.lang.annotation.*;

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface MyLog {
String value() default "";
}

然后是实现类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import com.alibaba.fastjson.JSON;
import com.example.entity.SysLog;
import com.example.service.SysLogService;
import com.example.utils.HttpContextUtils;
import com.example.utils.IPUtils;
import com.example.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.example.aspect.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();
//将参数所在的数组转换成json
String params = JSON.toJSONString(args);
sysLog.setParams(params);

sysLog.setCreateDate(new Date());
//获取用户名
sysLog.setUsername(ShiroUtils.getUserEntity().getUsername());
//获取用户ip地址
HttpServletRequest request = HttpContextUtils.getHttpServletRequest();
sysLog.setIp(IPUtils.getIpAddr(request));

//调用service保存SysLog实体类到数据库
sysLogService.save(sysLog);
}
}

最后直接在例如Controller中使用即可

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import com.example.aspect.MyLog;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/sys")
public class SysMenuController {

@MyLog(value = "测试")
@PostMapping("/test")
public String test(@RequestBody String test) {
System.out.println(test);
return "index";
}
}

下面是几个Utils

HttpContextUtils:

1
2
3
4
5
6
7
8
9
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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;
}
}

ShiroUtils:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import com.example.entity.User;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

public class ShiroUtils {

public static HttpSession getSession(){
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
HttpSession httpSession = request.getSession();
return httpSession;
}

public static User getUserEntity(){
return (User)getSession().getAttribute("user");
}

}

此为博主副博客,留言请去主博客,转载请注明出处:https://www.baby7blog.com/myBlog/87.html

欢迎关注我的其它发布渠道