先介绍几个注解
@Aspect //代表这个类是一个切面
@Component //代表这个类被Spring所管理
@Before //这个代表前置通知运行方法前拦截运行有这个注解的方法
@After //这个代表后置通知运行方法后返回拦截运行有这个注解的方法
下面开始讲解使用方法
首先添加Spring-boot的AOP jar包
<!-- aop依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
接下来开始使用
先创建一个类名字随意
@Aspect //这个是个切面
@Component //被Spring-boot所管理
@Slf4j
public class SystemLogAspect {
}
创建一个方法
@Aspect
@Component
@Slf4j
public class SystemLogAspect {
// 后置通知:在目标方法运行之后运行
// value的值代表切入点
@After(value = "execution(* com.bjhz.api.controller..*.*(..))")
public void doBefore(JoinPoint joinpoint) throws Throwable {
}
首先自定义一个注解
//注解的名字就是方法名比如下面的注解就叫@Menu
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Menu {
String value() default "";
}
//这个方法是获取注解的值 返回值和注解的方法名相同
public static Menu getMenu(Method method) {
if (method != null) {
return method.getAnnotation(Menu.class);
}
return null;
}
//这个方法用于从拦截的接口中获取注解
public static Method getMethod(JoinPoint joinPoint) {
Signature signature = joinPoint.getSignature();
MethodSignature methodSignature = (MethodSignature) signature;
Method method = methodSignature.getMethod();
if (method != null) {
return method;
}
return null;
}
接下来看我们切入点所拦截的方法
//注解的值代表这个方法的操作用于登记数据库
@Menu("系统管理-审计日志")
@GetMapping("/selectlist")
@ResponseBody
public JSONObject selectoperLog(Map<String, Object> param) {
return operLogService.selectoperLog(param);
}
接下来编写我们的切入点方法
@Aspect
@Component
@Slf4j
public class SystemLogAspect {
//用于存储数据库的接口
@Autowired
private TOperLogMapper operLogMapper;
// 后置通知:在目标方法运行之后运行
@After(value = "execution(* com.bjhz.api.controller..*.*(..))")
public void doBefore(JoinPoint joinpoint) throws Throwable {
// 获得注解
//此方法的作用看上面的介绍
Method method = WebLogUtil.getMethod(joinpoint);
//这个方法是取注解
Menu Menu = WebLogUtil.getMenu(method);
//判断注解是否存在
if (Menu!=null) {
System.out.println("拦截到了请求");
//拦截到请求
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
//获取请求
HttpServletRequest request = attributes.getRequest();
//获取响应
HttpServletResponse response = attributes.getResponse();
//获取是否执行成功状态吗
int status = response.getStatus();
//从请求中获取Header中存储的Token令牌
String header = request.getHeader("X-Token");
//解析Token令牌
Map<String, Object> valid = JwtTokenUtils.valid(header);
String data = String.valueOf(valid.get("data"));
JSONObject jsonObject = JSONObject.parseObject(data);
//获取用户名
String username = jsonObject.getString("username");
//获取用户名Id
String userId = jsonObject.getString("userid");
//记录请求内容
log.info("userId:{}", userId);
log.info("username:{}", username);
log.info("URL : " + request.getRequestURL().toString());
log.info("HTTP_METHOD : " + request.getMethod());
log.info("IP : " + request.getRemoteAddr());
String Menus = Menu.value();
if (StringUtil.isNotNull(MenuTypes) && StringUtil.isNotNull(Menus)) {
TOperLog operLog = new TOperLog();
operLog.setId(UUID.randomUUID().toString().replace("-", ""));
operLog.setOperUserId(userId);
operLog.setOperUserName(username);
operLog.setOperDt(new Date());
operLog.setOperUrl(request.getRequestURL().toString());
operLog.setOperUserIp(request.getRemoteAddr());
operLog.setOperMethod(request.getMethod());
operLog.setOperMenuName(split[0]);
operLog.setOperOperationType(MenuTypes);
operLog.setOperOperationContent(MenuType.value() + Menu.value());
if (status!=200){
operLog.setOperOperationData("失败");
}else {
operLog.setOperOperationData("成功");
}
//存储操作
operLogMapper.insertSelective(operLog);
}
}
}
}