SpringAop拦截controller进行日志管理

2017-10-29 19:08:06来源:CSDN作者:my_power520人点击

分享
一.使用自定义注解的方式进行操作日志管理
import java.lang.annotation.*;/** * Created by Administrator on 2017/8/30. * 自定义拦截service */@Target({ElementType.PARAMETER, ElementType.METHOD})@Retention(RetentionPolicy.RUNTIME)@Documentedpublic @interface SystemLog {
    int operationType()  default 0;    String opObject() default "";}
二.操作日志拦截类
import com.findingroof.appserver.annotation.SystemLog;import com.findingroof.appserver.contant.CacheConstant;import com.findingroof.appserver.interceptor.LoginInterceptor;import com.findingroof.appserver.operationlogservice.model.Operationlog;import com.findingroof.appserver.operationlogservice.provider.OperationlogProvider;import com.findingroof.appserver.userinfoservice.model.Userinfo;import com.findingroof.appserver.util.DateUtil;import com.findingroof.appserver.util.RedisUtils;import org.aspectj.lang.JoinPoint;import org.aspectj.lang.Signature;import org.aspectj.lang.annotation.After;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.slf4j.Logger;import org.slf4j.LoggerFactory;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;/** */@Component@Aspectpublic class SystemLogAction {    @Autowired    private static final Logger logger = LoggerFactory.getLogger(SystemLogAction.class);    @Autowired    private RedisUtils redisUtils;    @Autowired    private OperationlogProvider operationlogProvider;    //Controller层切点    @Pointcut("@annotation(com.findingroof.appserver.annotation.SystemLog)")    public void controllerAspect() {    }    /**     * 前置通知 用于拦截Controller层记录用户的操作     *     * @param joinPoint 切点     */    @After(value = "controllerAspect() && @annotation(systemLog) && args(log,..)")    public void after(JoinPoint joinPoint, SystemLog systemLog, Operationlog log) throws Throwable {        long start = System.currentTimeMillis();        logger.debug(log.toString());        //常见日志实体对象        log.setOptime(DateUtil.format(new Date(), DateUtil.FORMAT_DATETIME));        log.setOpobject(systemLog.opObject());        log.setOperationtype(systemLog.operationType());        //获取登录用户账户        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();//        String name = (String) request.getAttribute("username");        String token = request.getParameter("token");        Userinfo userinfo = (Userinfo) redisUtils.get(CacheConstant.USERINFO + token);//        String aopUsername = userinfo.getUsername();       // log.setOpmen(userinfo.getUsername());                //获取系统ip,这里用的是我自己的工具类,可自行网上查询获取ip方法        String ip = request.getRemoteAddr();        logger.debug(ip);        // 拦截的实体类,就是当前正在执行的controller        Object target = joinPoint.getTarget();        logger.debug(target.toString());        // 拦截的方法名称。当前正在执行的方法        String methodName = joinPoint.getSignature().getName();        logger.debug(methodName.toString());        // 拦截的方法参数        Object[] args = joinPoint.getArgs();        Signature signature = joinPoint.getSignature();        MethodSignature methodSignature = (MethodSignature) signature;        String[] strings = methodSignature.getParameterNames();        System.out.println(Arrays.toString(strings));        for (int i = 0; i < args.length; i++) {            logger.debug(String.valueOf(strings[i] + ":" + args[i].toString()));        }        //拦截的放参数类型        Signature sig = joinPoint.getSignature();        logger.debug(sig.toString());        logger.debug("耗时:{}", System.currentTimeMillis() - start);        operationlogProvider.insertOperationlog(log);    }}
三.使用注解测试

微信扫一扫

第七城市微信公众平台