01 | package com.jt.common.aspect; |
03 | import java.lang.reflect.Method; |
06 | import org.apache.shiro.SecurityUtils; |
07 | import org.aspectj.lang.ProceedingJoinPoint; |
08 | import org.aspectj.lang.annotation.Around; |
09 | import org.aspectj.lang.annotation.Aspect; |
10 | import org.aspectj.lang.annotation.Pointcut; |
11 | import org.aspectj.lang.reflect.MethodSignature; |
12 | import org.springframework.beans.factory.annotation.Autowired; |
13 | import org.springframework.stereotype.Component; |
15 | import com.fasterxml.jackson.databind.ObjectMapper; |
16 | import com.jt.common.annotation.RequestLog; |
17 | import com.jt.common.exception.ServiceException; |
18 | import com.jt.common.util.IPUtils; |
19 | import com.jt.sys.dao.SysLogDao; |
20 | import com.jt.sys.entity.SysLog; |
24 | public class SysLogAspect { |
26 | private SysLogDao sysLogDao; |
27 | @Pointcut ( "@annotation(com.jt.common.annotation.RequestLog)" ) |
28 | public void logPointCut(){} |
30 | @Around ( "logPointCut()" ) |
31 | public Object around(ProceedingJoinPoint point) throws Throwable { |
32 | System.out.println( "===SyslogAspect===" ); |
33 | long beginTime = System.currentTimeMillis(); |
35 | Object result = point.proceed(); |
37 | long time = System.currentTimeMillis() - beginTime; |
39 | saveSysLog(point, time); |
42 | private void saveSysLog(ProceedingJoinPoint joinPoint, long time) { |
43 | MethodSignature signature = (MethodSignature) joinPoint.getSignature(); |
44 | Method method = signature.getMethod(); |
45 | Class<?> classTarget=joinPoint.getTarget().getClass(); |
46 | Class<?>[] par=((MethodSignature) joinPoint.getSignature()).getParameterTypes(); |
47 | Method targetMethod= null ; |
49 | targetMethod=classTarget.getMethod(method.getName(), par); |
50 | } catch (Exception e1) { |
52 | throw new ServiceException(e1.getMessage()); |
54 | SysLog sysLog = new SysLog(); |
55 | RequestLog requestLog = targetMethod.getAnnotation(RequestLog. class ); |
56 | System.out.println( "requestLog=" +requestLog); |
57 | if (requestLog != null ){ |
59 | System.out.println( "oper==" +requestLog.value()); |
60 | sysLog.setOperation(requestLog.value()); |
63 | String className = joinPoint.getTarget().getClass().getName(); |
64 | String methodName = signature.getName(); |
65 | sysLog.setMethod(className + "." + methodName + "()" ); |
67 | Object[] args = joinPoint.getArgs(); |
70 | String params= new ObjectMapper().writeValueAsString(args[ 0 ]); |
71 | sysLog.setParams(params); |
76 | sysLog.setIp(IPUtils.getIpAddr()); |
78 | String username =(String)SecurityUtils.getSubject().getPrincipal(); |
79 | sysLog.setUsername(username); |
81 | sysLog.setCreateDate( new Date()); |
83 | sysLogDao.insertObject(sysLog); |