文章目录
- 一、常用的场景
- 1. 请求拦截
- 2. 异步保存轨迹
- 二、案例实战
- 2.1. pom
- 2.2. 自定义注解
- 2.3. aop拦截
- 2.4. 测试类
- 2.5. 保存日志
一、常用的场景
1. 请求拦截
通过aop 请求拦截,举个例子,第三方厂商请求平台接口,先去数据库查询该接口,此ip是否有访问权限,有如果就通过,继续下面的逻辑,否则,权限访问拦截,请求到此结束!
2. 异步保存轨迹
见下面案例:说一下思路
也是同理,同样通过拦截器来实现的,利用下的注解即可,案例中柚子
@Aspect
@Pointcut(“execution( * com.gblfy.logboot...*(…))”)//两个…代表所有子目录,最后括号里的两个…代表所有参数
@After
@Around
二、案例实战
2.1. pom
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
</dependency>
2.2. 自定义注解
package com.gblfy.logboot.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
//注解作用的位置,ElementType.METHOD表示该注解仅能作用于方法上
@Target(ElementType.METHOD)
public @interface Log {
String value() default "";
}
2.3. aop拦截
package com.gblfy.logboot;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.Arrays;
/**
* 日志文件记录
*/
@Aspect
@Component
public class WebLogAspect {
private static final Logger logger = LoggerFactory.getLogger(WebLogAspect.class);
@Autowired
HttpServletRequest request;
@Pointcut("execution( * com.gblfy.logboot.*.*.*(..))")//两个..代表所有子目录,最后括号里的两个..代表所有参数
public void logPointCut() {
}
@Before("logPointCut()")
public void doBefore(JoinPoint joinPoint) throws Throwable {
// 接收到请求,记录请求内容
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
// 记录下请求内容
logger.info("请求地址 : " + request.getRequestURL().toString());
logger.info("HTTP METHOD : " + request.getMethod());
// 获取真实的ip地址
//logger.info("IP : " + WebUtils.getIpAddress(request));
logger.info("CLASS_METHOD : " + joinPoint.getSignature().getDeclaringTypeName() + "."
+ joinPoint.getSignature().getName());
logger.info("参数 : " + Arrays.toString(joinPoint.getArgs()));
// loggger.info("参数 : " + joinPoint.getArgs());
}
@After("logPointCut()")
public void doAfter(JoinPoint joinPoint) throws Throwable {
System.out.println("request---" + request.getAttribute("aa"));
}
@Around("logPointCut()")
public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
long startTime = System.currentTimeMillis();
Object ob = pjp.proceed();// ob 为方法的返回值
logger.info("耗时 : " + (System.currentTimeMillis() - startTime));
return ob;
}
}
2.4. 测试类
package com.gblfy.logboot.controller;
import com.gblfy.logboot.annotation.Log;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@RequestMapping("/log")
@RestController("日志控制器")
public class LogController {
@Log("测试收集日志")
@RequestMapping("/save")
public String saveLog(@RequestParam(name = "token") String token,
@RequestParam(name = "name") String name,
HttpServletRequest request, HttpServletResponse response) {
System.out.println("开始收集log"+token+name);
request.setAttribute("aa","assddddd");
return "收集日志succes333s";
}
// http://localhost:8080/log/save?token=123
}
2.5. 保存日志