审查视图

src/main/java/com/tianbo/warehouse/annotation/LogAnnotationAOP.java 4.5 KB
朱兆平 authored
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
package com.tianbo.warehouse.annotation;

import com.alibaba.fastjson.JSON;
import com.tianbo.warehouse.model.LOGWithBLOBs;
import com.tianbo.warehouse.service.LogService;
import com.tianbo.warehouse.util.IO.StreamUtil;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Component;
import org.springframework.validation.BindingResult;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;


@Aspect
@Component
public class LogAnnotationAOP {

    @Autowired
    private LogService logService;

    private LOGWithBLOBs log;

    @Pointcut("@annotation(com.tianbo.warehouse.annotation.LogAnnotation)")
    public void annotationPointCut(){

    }

    @Before("annotationPointCut()")
    public void before(JoinPoint point){
        //获取当前登录用户信息
        try{
            UserDetails userDetails =(UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
            HttpServletRequest request = attributes.getRequest();
            String requestRemoteAddr = request.getRemoteAddr();

            log.setIp(requestRemoteAddr);
            log.setUsername(userDetails.getUsername());
        }catch (Exception e){
            e.printStackTrace();
        }

        try{
            String targetName = point.getTarget().getClass().getName();
            Class targetClass = Class.forName(targetName);

            MethodSignature signature = (MethodSignature) point.getSignature();
            Method method= signature.getMethod();
            LogAnnotation annotation = method.getAnnotation(LogAnnotation.class);
            System.out.print("打印:"+annotation.moduleName()+" 开始前");
            String methodName = point.getSignature().getName();
//            Method[] methods = targetClass.getMethods();


            if(annotation != null){
                log.setModelnamecn(annotation.moduleName());
                log.setOperatenamecn(annotation.operate());
            }
            log.setClassname(targetName);
            log.setMethodname(methodName);

            // 获得参数列表
            Object[] params = point.getArgs();
            if(params.length>0){
                //序列化时过滤掉request和response,参考资料为:https://blog.csdn.net/hbn1326317071/article/details/83818059
                List<Object> logArgs = StreamUtil.streamOf(params)
                        .filter(arg -> (!(arg instanceof HttpServletRequest) && !(arg instanceof HttpServletResponse) && !(arg instanceof BindingResult)))
                        .collect(Collectors.toList());

                //将参数列表转成JSON存储
                log.setParameters(JSON.toJSONString(logArgs));
            }

        }catch (ClassNotFoundException e){
            e.printStackTrace();
朱兆平 authored
89 90
            log.setResult(JSON.toJSONString(e));
            logService.insertSelective(log);
朱兆平 authored
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
        }




    }

    @Around("annotationPointCut()")
    public Object advice(ProceedingJoinPoint joinPoint){
        System.out.println("通知之开始");
        log = new LOGWithBLOBs();

        Object retmsg=null;
        try {
            retmsg=joinPoint.proceed();
            System.err.println("++++++++"+retmsg);
        } catch (Throwable e) {
            e.printStackTrace();
        }
        System.out.println("通知之结束");
        return retmsg;
    }

    @After("annotationPointCut()")
    public void after(){
        System.out.println("after方法执行后");
    }

    /**
     * 方法正常完成后执行方法
     * @param retmsg 这里的retmsg= advice中的Object retmsg
     */
    @AfterReturning(returning = "retmsg",pointcut="annotationPointCut()")
    public void afterReturn(Object retmsg) throws Throwable{

        // 处理完请求,返回内容
        log.setResult(JSON.toJSONString(retmsg));

        logService.insertSelective(log);
    }
}