操作日志spring aop

注解接口

package com.bw.admin.common.annotation;

import com.bw.admin.common.enums.LogsEnums;

import java.lang.annotation.*;

@Target({ElementType.PARAMETER, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface SysLog {

LogsEnums type() default LogsEnums.QUERY;

boolean paramHide() default false;

String description() default "";

}

aop

package com.bw.admin.common.aop;

import com.bw.admin.common.annotation.SysLog;
import com.bw.admin.common.mongo.Dao.SysOperationLogDao;
import com.bw.admin.common.mongo.Entity.SysOperationLog;
import com.bw.admin.common.utils.ServletUtils;
import com.bw.admin.system.entity.Admin;
import com.coin.common.utils.HttpUtil;
import com.coin.common.utils.JsonHelper;
import com.jiu.common.mongo.dao.Page;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.scheduling.annotation.Async;
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.servlet.http.HttpServletRequest;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.Map;
@Aspect
@Component
@Order
@Slf4j
public class SysLogAspect {

    @Autowired
    private SysOperationLogDao sysOperationLogDao;

    // defined aop pointcut
    @Pointcut("@annotation(com.bw.admin.common.annotation.SysLog)")
    public void controllerLog() {
    }

    // result of return
    @AfterReturning(pointcut = "controllerLog()", returning = "result")
    @Async
    public void after(JoinPoint joinPoint, Object result)  {
        try{
            HttpServletRequest request = ServletUtils.getRequest();
            String requestIp = HttpUtil.clientIP(request);

            //获取用户
            Authentication auth = SecurityContextHolder.getContext().getAuthentication();
            Admin admin = (Admin)auth.getPrincipal();

            Class targetClass = joinPoint.getTarget().getClass();
            String methodName = joinPoint.getSignature().getName();
            Map<String, String[]> map = request.getParameterMap();//请求参数
            Class[] parameterTypes = ((MethodSignature)joinPoint.getSignature()).getMethod().getParameterTypes();
            Method method = targetClass.getMethod(methodName, parameterTypes);
            Annotation annotation = method.getAnnotation(SysLog.class);
            SysLog anLog = (SysLog)annotation;
            String requestCode = anLog.type().getCode();
            String requestName = anLog.type().getName();
            //请求参数是否隐藏,隐藏请求参数,则不保留请求参数 比如修改密码
            boolean paramHide = anLog.paramHide();
            String res = result == null?"":JsonHelper.obj2JsonStr(result);
           /* log.info("操作人:{},请求ip:{},请求地址:{},请求方法:{},请求参数:{},返回结果:{}",
                    admin.getEmail(),requestIp,request.getRequestURI(),requestName,map.toString(),res);*/
            SysOperationLog sysOperationLog = null ;
            if(paramHide){
                sysOperationLog = new SysOperationLog(admin.getEmail(),requestIp,requestName,request.getRequestURI(),res,requestCode);
            }else{
                sysOperationLog = new SysOperationLog(admin.getEmail(),requestIp,requestName,request.getRequestURI(),map==null?"":map.toString(),res,requestCode);
            }
            sysOperationLogDao.save(sysOperationLog);
        }catch (Exception e){
            log.info("保存操作日志失败");
            e.printStackTrace();
        }

    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值