springboot使用AOP基于注解记录操作日志(含源码)

spring框架让java开发者觉得非常简便,其中spring的两个特性IOC和AOP更是让我们的java代码变得十分得简洁,下面分享一个使用注解基于AOP记录操作日志

目的

自定义注解,只要添加了这个注解的请求在被请求时,都会有操作记录入库

思路

1.首先定义一个操作类型的枚举,操作包括增,删,改,查

public enum OperationType {

    SELECT("查询"),
    INSERT("插入"),
    UPDATE("更新"),
    DELETE("删除");

    private final String value;


    public String getValue() {
        return value;
    }

    OperationType(String value) {
        this.value = value;
    }
}

2.自定义一个注解,有两个属性分别为title(标题)和type(操作类型)两个属性,作用域在方法上

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Operation {

    String title() default "";

    OperationType value() default OperationType.SELECT;
}

3.定义一个操作类型实体,记录操作日志记录,字段包括标题,操作类型,方法名,ip,入参

public class OperationRecord implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 标题
     */
    private String title;

    /**
     * 操作类型
     */
    private String type;

    /**
     * 方法名
     */
    private String method;

    /**
     * 真实ip
     */
    private String ip;

    /**
     * 入参类型和入参值
     */
    private Map<String, Object> requestAndValue;


    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getMethod() {
        return method;
    }

    public void setMethod(String method) {
        this.method = method;
    }


    public String getIp() {
        return ip;
    }

    public void setIp(String ip) {
        this.ip = ip;
    }

    public Map<String, Object> getRequestAndValue() {
        return requestAndValue;
    }

    public void setRequestAndValue(Map<String, Object> requestAndValue) {
        this.requestAndValue = requestAndValue;
    }

    @Override
    public String toString() {
        return "OperationRecord{" +
                "title='" + title + '\'' +
                ", type='" + type + '\'' +
                ", method='" + method + '\'' +
                ", ip='" + ip + '\'' +
                ", requestAndValue=" + requestAndValue +
                '}';
    }
}

4.定义切面,入库操作

@Slf4j
@Aspect
@Component
public class OperationAspect {

    private static final String UNKNOWN = "unknown";


    @Resource
    private OperationRecordService operationRecordService;

    /**
     * 定义拦截点
     */
    @Pointcut("@annotation(com.zyy.log.aop.annocation.Operation)")
    public void pointcut() {

    }

    @Around("pointcut()")
    public Object recordOperation(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        Object obj = proceedingJoinPoint.proceed();

        try {
            // 模拟入库
            OperationRecord operationRecord = new OperationRecord();
            operationRecord.setTitle(getTitle(proceedingJoinPoint));
            operationRecord.setMethod(getMethodName(proceedingJoinPoint));
            operationRecord.setType(getOperationType(proceedingJoinPoint).getValue());
            operationRecord.setIp(getIp());
            operationRecord.setRequestAndValue(getNameAndValue(proceedingJoinPoint));
            operationRecordService.insert(operationRecord);
            System.out.println("operationRecord = " + JSONUtil.toJsonStr(operationRecord));

        } catch (Exception e) {
            throw new RuntimeException("入库失败");
        }
        return obj;
    }


    /**
     * 获取注解
     *
     * @param proceedingJoinPoint
     * @return
     */
    private Operation getAnnotation(ProceedingJoinPoint proceedingJoinPoint) {
        return ((MethodSignature) proceedingJoinPoint.getSignature()).getMethod().getAnnotation(Operation.class);
    }

    /**
     * 获取操作类型
     *
     * @param proceedingJoinPoint
     * @return
     */
    private OperationType getOperationType(ProceedingJoinPoint proceedingJoinPoint) {
        return getAnnotation(proceedingJoinPoint).value();
    }

    /**
     * 获取标题
     *
     * @param proceedingJoinPoint
     * @return
     */
    private String getTitle(ProceedingJoinPoint proceedingJoinPoint) {
        return getAnnotation(proceedingJoinPoint).title();
    }

    /**
     * 获取方法名称
     *
     * @param proceedingJoinPoint
     * @return
     */
    private String getMethodName(ProceedingJoinPoint proceedingJoinPoint) {
        MethodSignature methodSignature = (MethodSignature) proceedingJoinPoint.getSignature();
        return methodSignature.getMethod().getDeclaringClass().getName() + "." + methodSignature.getMethod().getName();
    }

    /**
     * 获取真实ip
     */
    private String getIp() {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = Objects.requireNonNull(attributes).getRequest();

        String ip = request.getHeader("x-forwarded-for");
        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();
        }
        String comma = ",";
        String localhost = "127.0.0.1";
        if (ip.contains(comma)) {
            ip = ip.split(",")[0];
        }
        if (localhost.equals(ip)) {
            // 获取本机真正的ip地址
            try {
                ip = InetAddress.getLocalHost().getHostAddress();
            } catch (UnknownHostException e) {
                log.error(e.getMessage(), e);
            }
        }
        return ip;
    }

    /**
     * 获取方法参数名和参数值
     *
     * @param joinPoint
     * @return
     */
    private Map<String, Object> getNameAndValue(ProceedingJoinPoint joinPoint) {

        final Signature signature = joinPoint.getSignature();
        MethodSignature methodSignature = (MethodSignature) signature;
        final String[] names = methodSignature.getParameterNames();
        final Object[] args = joinPoint.getArgs();

        if (ArrayUtil.isEmpty(names) || ArrayUtil.isEmpty(args)) {
            return Collections.emptyMap();
        }
        if (names.length != args.length) {
            log.warn("{}方法参数名和参数值数量不一致", methodSignature.getName());
            return Collections.emptyMap();
        }
        Map<String, Object> map = new HashMap<>();
        for (int i = 0; i < names.length; i++) {
            map.put(names[i], args[i]);
        }
        return map;
    }

}

5.定义测试请求,请求中使用Operation注解

@Slf4j
@RestController
public class TestController {

    /**
     * 测试方法
     *
     * @param who 测试参数
     */
    @GetMapping("/test")
    @Operation(title = "查询列表", value = OperationType.SELECT)
    public String test(String who) {
        return who;
    }

}

6.启动项目,输入http://localhost:8080/demo/test?who=jack查看控制台输出

源码

https://gitee.com/onepiecezyy/demo-log-aop

总结

AOP非常强大,应用在很多场景。希望能帮助到大家

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值