首页>代码>ssm+shiro+layui+quartz实现细粒度用户权限控制>/app_demo/src/main/java/com/bird/business/annotation/SystemLogAspect.java
package com.bird.business.annotation;

import com.bird.business.domain.TbLog;
import com.bird.business.service.ITbLogService;
import com.bird.business.utils.JsonUtils;
import com.bird.business.utils.WebUtils;
import org.apache.shiro.SecurityUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.lang.reflect.Method;
import java.util.Date;

/**
 * 切点类   
 * @author Cui Hui
 */
@Aspect
@Component
public  class SystemLogAspect {

    //本地异常日志记录对象
    private  static  final Logger logger = LoggerFactory.getLogger(SystemLogAspect.class);

    //注入Service用于把日志保存数据库    
    @Resource    
    private ITbLogService tbLogService;

    
    //Controller层切点    
    @Pointcut("@annotation(com.bird.business.annotation.SysLog)")
     public  void controllerAspect() {    
    }    
    
    /**  
     * 前置通知 用于拦截Controller层记录用户的操作  
     *  
     * @param joinPoint 切点  
     */    
    @Before("controllerAspect()")
     public  void doBefore(JoinPoint joinPoint) {
    
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        //读取session中的用户
        String username = (String) SecurityUtils.getSubject().getPrincipal();
        //请求的IP    
        String ip= WebUtils.getRemoteAddr(request);
        
        String requestURI=request.getRequestURI();
        

        String method = joinPoint.getSignature().getDeclaringTypeName() + 
                "." + joinPoint.getSignature().getName();
        String params = ""; 
        if (joinPoint.getArgs() !=  null && joinPoint.getArgs().length > 0) {    
            for ( int i = 0; i < joinPoint.getArgs().length; i++) {
                Object obj = joinPoint.getArgs()[i];
                String objStr = JsonUtils.objectToJson(obj);
                if (objStr != null) {
                    params += objStr + ";";
                }
           }
       }    
         try {    
            //*========控制台输出=========*//    
            String operation=getControllerMethodDescription(joinPoint);
            TbLog log=new TbLog();
            log.setCreateTime(new Date());
            log.setIp(ip);
            log.setOperation(operation);
            log.setParams(params);
            log.setUsername(username);
            log.setMethod(requestURI);
			//*========保存数据库日志=========*// 
            //System.out.println(log);
            tbLogService.insert(log);
            //保存数据库    
        }  catch (Exception e) {    
            //记录本地异常日志    
            logger.error("==前置通知异常==");    
            logger.error("异常信息:{}", e.getMessage());    
        }    
    }    
    
    /**  
     * 获取注解中对方法的描述信息 用于Controller层注解  
     *  
     * @param joinPoint 切点  
     * @return 方法描述  
     * @throws Exception  
     */    
     public  static String getControllerMethodDescription(JoinPoint joinPoint)  throws Exception {
        String targetName = joinPoint.getTarget().getClass().getName();    
        String methodName = joinPoint.getSignature().getName();    
        Object[] arguments = joinPoint.getArgs();    
        Class targetClass = Class.forName(targetName);    
        Method[] methods = targetClass.getMethods();    
        String description = "";    
         for (Method method : methods) {    
             if (method.getName().equals(methodName)) {    
                Class[] clazzs = method.getParameterTypes();    
                 if (clazzs.length == arguments.length) {
                     //获取注解传入的参数值,可设置多个
                     description = method.getAnnotation(SysLog.class).value();
                     break;    
                }    
            }    
        }    
         return description;    
    }    
}    
最近下载更多
Brilion  LV6 2023年3月22日
admin115  LV6 2022年11月30日
543666826  LV33 2021年11月20日
zhoulang123  LV8 2021年5月6日
Xavier1123  LV8 2021年4月22日
breakCY  LV15 2021年4月9日
xhmpmail  LV17 2021年2月23日
123456nty  LV37 2020年12月7日
hello-world  LV10 2020年9月24日
qwicheng  LV1 2020年9月22日
最近浏览更多
xuweiwowzy  LV5 1月14日
ljt289917726  LV3 2023年12月8日
Hachi6  LV13 2023年10月11日
monster536 2023年8月28日
暂无贡献等级
skook7  LV2 2023年6月1日
女王不该在山炮村养花  LV8 2023年4月17日
wiston 2023年3月30日
暂无贡献等级
 LV7 2023年3月29日
暂无贡献等级
Brilion  LV6 2023年3月22日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友