Springboot自定义注解实现操作日志管理

本文涉及的产品
云数据库 Redis 版,社区版 2GB
推荐场景:
搭建游戏排行榜
简介: Springboot自定义注解实现操作日志管理

操作日志的记录

为什么要有日志?

因为我们不光要记录代码的运行,如(logback log4j),而且还应该记录用户的行为,这叫做业务运行日志

例如:记录 zhangsan 在项目中 调用了哪个方法, 什么时间调用的 。访问的ip地址, 访问了哪些数据,做了什么操作,以此当程序出现问题的时候更利于我们进行错误的排查!

业务运行日志的作用

  1. 记录用户的行为 用于后续的分析
  2. 记录用户的所有的操作

业务运行日志最常用的使用场景:记录管理员所有的行为操作, 可以用于业务分析,事故恢复

日志实现的思路

1.我们需要记录哪些数据 存入数据库

这里列出一个我所用的表结构,如下所示:

字段 含义
log_id 主键
log_date 时间
log_content 操作内容 例如:查询全部菜单信息 添加用户数据
log_name_id 用户的id
log_ip 用户的ip地址
log_type 操作类型

2.在项目中什么位置记录

日志记录是一个数据库的添加操作 是一段代码

通常,我们在Controller方法进行后置增强

如下图所示,我们在需要记录操作的controller上使用aop配置一个切入点,以此来记录用户所进行的操作

3.如何实现记录功能

实现方式:AOP

4.Aop日志记录 具体代码实现

aop的使用流程,这里使用注解式aop来实现

具体步骤:

设置切入点

  1. 可以切在方法上
  2. 可以切在注解上
@Transactional 事务注解 注解加在类上 aop 切在注解上

写增强 日志记录增强

  1. 获取日志的相关信息
    用户的id ip地址, 时间, 操作的描述, 类型等信息
  2. 将日志对象 添加到数据库

增强方法中获取session

因为我们是通过aop来获取用户的请求的,所以就需要通过当前的请求拿到session,进而去获取用户的信息。

但是,操作的描述如何获取呢?

比如 执行的方法不同  描述是不一样的
login             管理员登录
selectAllMenu  查询了所有的菜单

解决方案:使用自定义注解:

  1. 在 目标 方法上添加自定义注解 (@Log) 如下
  2. 在增强中获取注解(@Log)的value 和 type

代码实现

自定义日志注解

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
 * 元注解:加在自定义注解上的注解
 * @Target 定义注解可以添加的位置 METHOD 方法上 type 类上
 * @Retention RUNTIME 运行时  不管编译 还是 运行 这个注解都可以用
 *
 */
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface LogAnnotation {
    /**
     * 写法类似于接口的方法 后面可以通过default 关键字给默认值
     * 用法类似于属性
     * @return
     */
    String value() default "";
    String type() default "";
}

这里要注意什么是元注解,和 注解属性的定义方式

2. 在目标方法上使用注解

3. 在增强方法中获取注解的value 和 type

        /**
         * 操作的描述
         *
         * 执行的方法不同  描述是不一样的
         * login         管理员登录
         * selectAllGuru 查询了所有的上师
         *
         * 获取注解的值
         */
//        1.通过连接点获取方法签名 被切入方法的所有信息
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
//        2.获取被切入方法对象
        Method method = signature.getMethod();
//        3.获取方法上的注解
        LogAnnotation annotation = method.getAnnotation(LogAnnotation.class);
//        4.获取注解的值
        String value = annotation.value();

完整的aop的代码实现

package com.tourism.hu.config;
/**
 * @author 马超伟
 * @PROJECT_NAME: fzll
 * @Description:
 * @date 15:29
 * @Copyright: All rights Reserved, Designed By Huerdai  
 * Copyright:    Copyright(C) 2019-2020
 * Company       Huerdai Henan LTD.
 */
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tourism.hu.entity.CustomerInfo;
import com.tourism.hu.entity.CustomerLoginLog;
import com.tourism.hu.service.ICustomerInfoService;
import com.tourism.hu.service.ICustomerLoginLogService;
import com.tourism.hu.util.IpAddressUtil;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.lang.reflect.Method;
import java.time.LocalDateTime;
/*** @Aspect 标记当前类为功能增强类 切面类 *
 *  @Configuration 标记当前类为配置类 这个注解包含了@Component的功能
 */
@Aspect
@Configuration
public class LogAop {
    private  Logger logger = LoggerFactory.getLogger(getClass());
    @Resource
    private ICustomerInfoService iCustomerInfoService;
    @Resource
    private RedisTemplate redisTemplate;
    @Resource
    private ICustomerLoginLogService iCustomerLoginLogService;
    /**
     * JoinPoint 连接点 就是切入点 通过这个对象可以获取切入点的相关所有信息 例如:被切入的方法和注解
     *
     * @param joinPoint ** 切入点的设置 切注解 @annotation *
     */
    @After("@annotation(com.tourism.hu.config.Log)")
    public void logAfter(JoinPoint joinPoint) {
      //new 一个日志的实体,用来保存日志信息
        CustomerLoginLog loginLog = new CustomerLoginLog();
        // 1.获取日志相关的信息  用户的id session  ip  时间  操作的描述  类型  ctrl+H
        /**
         * 获取用户id
         * 为什么不能装配session?因为服务器有多个session
         * 通过 ServletRequestAttributes 可以获取当前请求
         * 当前请求可以获取当前会话的session
         */
         //获取用户的请求
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        //得到session
        HttpSession session = request.getSession();
        String sessionid = session.getId();
        //通过sessionid去获取用户信息
        Object obj = redisTemplate.opsForValue().get(sessionid);
        String customerId = "";
        if(obj!=null) {
            customerId=obj.toString();
        }
        //拿到用户对象
        CustomerInfo customerInfo = iCustomerInfoService.getOne(new QueryWrapper<CustomerInfo>().eq("id", customerId));
        if (customerInfo!=null){
          //将用户的id 存入到日志实体中
            loginLog.setCustomerId(customerInfo.getCustomerId());
        }
         loginLog.setLoginTime(LocalDateTime.now());
        /**
         * 获取用户的ip
         * 通过工具类 ip
         */
        loginLog.setLoginIp(IpAddressUtil.getIp());
        /**
         * 操作的描述
         * 执行的方法不同  描述是不一样的
         * login         管理员登录
         * 获取注解的值
         */
//        1.通过连接点获取方法签名 被切入方法的所有信息
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
//        2.获取被切入方法对象
        Method method = signature.getMethod();
//        3.获取方法上的注解
        Log annotation = method.getAnnotation(Log.class);
//        4.获取注解的值
        String value = annotation.value();
        loginLog.setLogContent(value);
        // 获取注解的类型
        String type = annotation.type();
        if (type!=null){
            loginLog.setLoginType(type);
        }
//        2.将日志对象 添加到数据库
        System.out.println(loginLog);
        logger.debug("loginLog===="+loginLog);
        boolean save = iCustomerLoginLogService.save(loginLog);
        logger.debug("保存日志------"+save);
    }
}

所用到的工具类

获取ip地址的工具类IpAddressUtil

  public static String getIp() {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String ip = "null";
        try {
            ip = request.getHeader("x-forwarded-for");
            if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("Proxy-Client-IP");
            }
            if (StringUtils.isEmpty(ip) || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("WL-Proxy-Client-IP");
            }
            if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("HTTP_CLIENT_IP");
            }
            if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("HTTP_X_FORWARDED_FOR");
            }
            if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getRemoteAddr();
            }
        } catch (Exception e) {
            logger.error("IPUtils ERROR ", e);
        }
        //使用代理,则获取第一个IP地址
        if(StringUtils.isNotEmpty(ip) && ip.length() > 15) {
          if(ip.indexOf(",") > 0) {
              ip = ip.substring(0, ip.indexOf(","));
          }
      }
        return ip;
    }
相关实践学习
日志服务之使用Nginx模式采集日志
本文介绍如何通过日志服务控制台创建Nginx模式的Logtail配置快速采集Nginx日志并进行多维度分析。
目录
相关文章
|
1月前
|
XML Java 数据库连接
spring boot 参数的过滤注解与实战
在Spring Boot应用中,对于入参的过滤,通常会涉及到对Web层的数据验证和处理。Spring Boot借助Spring框架提供了强大的验证框架支持,主要基于JSR-303/JSR-380(Bean Validation API)规范,以及Spring自身的@Valid或@Validated注解来实现请求参数的验证。以下是一些常见的使用案例来展示如何对参数进行过滤和验证。
29 1
|
3天前
|
缓存 Java Sentinel
Springboot 中使用 Redisson+AOP+自定义注解 实现访问限流与黑名单拦截
Springboot 中使用 Redisson+AOP+自定义注解 实现访问限流与黑名单拦截
|
2天前
|
Java 数据安全/隐私保护 开发者
【SpringBoot】讲清楚日志文件&&lombok
【SpringBoot】讲清楚日志文件&&lombok
13 5
|
3天前
|
Java Spring 容器
SpringBoot自动装配原理之@Import注解解析
SpringBoot自动装配原理之@Import注解解析
|
3天前
|
存储 缓存 NoSQL
Springboot中使用redisson + 自定义注解优雅的实现消息的发布订阅
Springboot中使用redisson + 自定义注解优雅的实现消息的发布订阅
|
16天前
|
XML Java API
springboot 常用的注解标签的概念及用法RequiredArgsConstructor 、RestController、RequestMapping
【4月更文挑战第12天】在 Spring Boot 中,@RequiredArgsConstructor, @RestController, 和 @RequestMapping 是常用的注解,每个都有其特定的功能和用法,它们合起来极大地简化了 Spring 应用程序的开发过程。
20 2
|
19天前
|
存储 关系型数据库 MySQL
【mybatis-plus】Springboot+AOP+自定义注解实现多数据源操作(数据源信息存在数据库)
【mybatis-plus】Springboot+AOP+自定义注解实现多数据源操作(数据源信息存在数据库)
|
19天前
|
Java 容器
SpringBoot使用配置注解开启自动配置功能&整合spring-boot-configuration-processor
SpringBoot使用配置注解开启自动配置功能&整合spring-boot-configuration-processor
15 0
|
1月前
|
NoSQL Java Redis
springboot实现复杂业务下的更新操作
springboot实现复杂业务下的更新操作
14 0
|
前端开发 Java 索引
40 个 SpringBoot 常用注解,你知道几个?
40 个 SpringBoot 常用注解,你知道几个?
133 0
40 个 SpringBoot 常用注解,你知道几个?