自定义注解实现接口操作记录日志

接口拦截器–附带了登录拦截

package com.futuredata.cloud.cap.aop;

import com.futuredata.cloud.cap.config.feign.UaaFeignService;
import com.futuredata.cloud.cap.config.feign.vo.UserVo;
import com.futuredata.cloud.cap.mapper.UserLogMapper;
import com.futuredata.cloud.cap.model.UserLog;
import com.futuredata.cloud.cap.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
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;
import java.util.Date;

@Aspect
@Component
@Slf4j
public class LogAspect {

    @Autowired
    private UserLogMapper mapper;
    @Autowired
    private UaaFeignService uaaFeignService;

    @Pointcut("@annotation(com.futuredata.cloud.cap.aop.Fdlog)")
    public void webLog() {
    }

    @Around("webLog()")
    public Object saveLog(ProceedingJoinPoint pjp) {
        // 此处joinPoint的实现类是MethodInvocationProceedingJoinPoint
        Signature signature = pjp.getSignature();
        // 获取参数名
        MethodSignature methodSignature = (MethodSignature) signature;
        Fdlog fdlog = methodSignature.getMethod().getAnnotation(Fdlog.class);
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        UserVo vo = uaaFeignService.selectUser().getData();
        String ipAddress = request.getHeader("X-Real-IP");
        String userName = vo.getUserName();
        String userId = vo.getId();
        String description = fdlog.description();
        String type = fdlog.type();
        UserLog userLog = new UserLog();
        if (StringUtils.isEmpty(ipAddress)) {
            userLog.setIpAddress("192.168.1.117");
        } else {
            userLog.setIpAddress(ipAddress);
        }
        userLog.setUserId(userId);
        userLog.setUserName(userName);
        userLog.setEnable(1);
        userLog.setDescription(description);
        userLog.setDepartId(vo.getDepartId());
        userLog.setDepartName(vo.getDepartName());
        userLog.setType(type);
        userLog.setCreateTime(new Date());
        mapper.insertSelective(userLog);
        try {
            // 直接pjp.proceed(),没有return ,前台页面获取不到ajax数据
            return pjp.proceed();
        } catch (Throwable e) {
            doAfterThrowing(e);
            return null;
        }
    }

    @Pointcut("execution(public * com.futuredata.cloud.cap.controller.*.*(..))")
    public void log() {
    }

    @Before("log()")
    public void doBefore(JoinPoint joinPoint) throws Throwable {
        // 接收到请求,记录请求内容
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();

        // 记录下请求内容
        log.debug("### 请求地址 : " + request.getRequestURL().toString());
        log.debug("### 请求类型 : " + request.getMethod());
        log.debug("### 请求IP : " + request.getRemoteAddr());
        log.debug("### 请求方法 : " + joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName());
        log.debug("### 请求参数 : " + Arrays.toString(joinPoint.getArgs()));

    }

    @AfterReturning(returning = "ret", pointcut = "log()")
    public void doAfterReturning(Object ret) throws Throwable {
        // 处理完请求,返回内容
        log.info("### 请求返回结果 : " + ret);
    }

    @AfterThrowing(pointcut = "log()", throwing = "e")
    public void doAfterThrowing (Throwable e) {
        log.error("### 返回异常信息:", e);
    }

}

自定义注解

package com.futuredata.cloud.cap.aop;

import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

@Documented
@Retention(RUNTIME)
@Target(METHOD)
public @interface Fdlog {

    String description() default "";

    String type() default "查看";

}

注解使用方式及效果图
在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值