AOP 操作日志注解记录

@Retention(RetentionPolicy.RUNTIME)
@Target( { ElementType.METHOD })
public @interface OperationLogAnnotation {
    //操作描述
    String desc() default "";
    //操作表
    String table() default "";
}

1.这里有获取注解参数的方法。 因为用了security,这是这样取了userId。获取用户id的方法有很多。
2.有获取 注解值的方法。
3.因为json格式的post获取参数,这里从request获取不了,基本因为@PostMapping 因为读了一次流了,调试起来麻烦(没有意义),这里是获取方法的 接参对象用作参数。

import com.annotation.OperationLogAnnotation;
import com.common.Result;
import com.model.OperationLog;
import com.model.SecurityUser;
import com.service.OperationLogService;
import com.service.UserService;
import com.util.SnowflakeIdWorker;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

/**
 * @program: he
 * @description: 操作日志记录注解实现, 注:参数实体放在首位。
 * @author: he
 * @create: 2020-05-20 10:19
 **/
@Slf4j
@Aspect
@Component
public class OperationLogAspect {
    @Resource
    OperationLogService operationLogService;
    @Resource
    UserService userService;

    @Pointcut("@annotation(com.annotation.OperationLogAnnotation)")
    public void doLog(){}

    @Around(value = "doLog() && @annotation(operationLogAnnotation)")
    public Object saveLog(ProceedingJoinPoint joinPoint, OperationLogAnnotation operationLogAnnotation) throws Throwable {
        HttpServletRequest request = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
        String url = request.getRequestURI();
        Object[] args = joinPoint.getArgs();
        if (args[0]==null){
            return Result.error("参数格式不对");
        }
        String param=args[0].toString();
        String desc = operationLogAnnotation.desc();
        String table = operationLogAnnotation.table();
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if(authentication==null) {
           return Result.error("无法获取用户id");
        }
        SecurityUser user = (SecurityUser) authentication.getPrincipal();
        OperationLog operationLog = new OperationLog().setId(SnowflakeIdWorker.id()).setOperationUrl(url)
                .setOperationTable(table).setOperationDesc(desc).setOperationUserId(user.getUserId())
                .setOperationUserName(userService.getById(user.getUserId()).getUserName())
                .setParam(param);
        operationLogService.insert(operationLog);
        return joinPoint.proceed();
    }


}
	@PostMapping("/add")
    @CheckParam
    @OperationLogAnnotation(desc = "新增银联错误日志描述记录",table = "epay_error_info")
    public Result add(@RequestBody @Valid EpayErrorInfo epayErrorInfo, BindingResult bindingResult){
        epayErrorInfoService.insert(epayErrorInfo.setId(SnowflakeIdWorker.id()));
        return Result.ok("操作成功");
    }

你可能感兴趣的:(JAVA基础)