package com.box.annotation;
  
import org.apache.shiro.SecurityUtils;
import org.aspectj.lang.JoinPoint;    
import org.aspectj.lang.annotation.*;    
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 com.box.entity.DbAdmin;
import com.box.entity.DbLog;
import com.box.service.LogService;
import com.box.utils.MyUtil;
import com.box.utils.WebUtils;

import javax.annotation.Resource;    
import javax.servlet.http.HttpServletRequest;    
import javax.servlet.http.HttpSession;    

import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Date;
    
/**
 * 切点类   
 * @author Mr Du
 */
@Aspect    
@Component    
public  class SystemLogAspect {    
    //注入Service用于把日志保存数据库    
    @Resource    
    private LogService logServiceImp;    
    //本地异常日志记录对象    
    private  static  final Logger logger = LoggerFactory.getLogger(SystemLogAspect. class);    
    
    //Controller层切点    
    @Pointcut("@annotation(com.box.annotation.SysLog)")    
     public  void controllerAspect() {    
    }    
    
    /**  
     * 前置通知 用于拦截Controller层记录用户的操作  
     *  
     * @param joinPoint 切点  
     */    
    @Before("controllerAspect()")    
     public  void doBefore(JoinPoint joinPoint) {    
    
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();    
        HttpSession session = request.getSession();    
        //读取session中的用户    
        DbAdmin user = (DbAdmin)SecurityUtils.getSubject().getPrincipal();
        System.out.println(user.toString()+"...................");
        //请求的IP    
        //String ip = request.getRemoteAddr();
        
        String requestURI=request.getRequestURI();
        
        String ip=WebUtils.getRemoteAddr(request);
        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++) {    
           	 params+=joinPoint.getArgs()[i]+";";   
           }    
       }    
         try {    
            //*========控制台输出=========*//    
            //System.out.println("=====前置通知开始=====");    
            String operation=getControllerMethodDescription(joinPoint);    
            String username=user.getUsername();
            //System.out.println("请求参数:" + params);    
            DbLog log=new DbLog();
            String date = MyUtil.getNowDateStr2();
            SimpleDateFormat fomat=new SimpleDateFormat(date);
            Date parse = fomat.parse(date);
            log.setCreateTime(parse);
            log.setIp(ip);
            log.setOperation(operation);
            log.setParams(params);
            log.setUsername(username);
            log.setMethod(requestURI);
			//*========保存数据库日志=========*// 
            //System.out.println(log);
            logServiceImp.insertLof(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;    
    }    
}    
最近下载更多
TY0165  LV20 2024年6月24日
pureshyness  LV6 2022年5月22日
wanglinddad  LV55 2022年4月9日
goohodo  LV8 2022年3月2日
xiayan  LV6 2021年12月7日
ljm2s  LV3 2021年9月23日
芜湖1234  LV2 2021年6月14日
xiaoxiexie  LV13 2021年5月16日
最代码-宋家辉  LV61 2021年5月1日
手落弦崩  LV13 2021年4月19日
最近浏览更多
哪里的完整版  LV8 4月1日
xiaoaitx  LV8 1月1日
xianyu091012  LV5 2024年11月18日
yellow_flowers  LV8 2024年7月8日
TY0165  LV20 2024年6月24日
花朝廿五  LV1 2024年6月10日
goccgoccgocc  LV4 2024年5月9日
李俊雄  LV3 2024年5月8日
sz1111111  LV2 2024年3月26日
WBelong  LV8 2023年12月19日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友