1+ package cn .tellsea .aspect ;
2+
3+ import cn .tellsea .annotation .SystemControllerLog ;
4+ import cn .tellsea .annotation .SystemServiceLog ;
5+ import cn .tellsea .utils .IPUtils ;
6+ import com .alibaba .fastjson .JSONObject ;
7+ import lombok .extern .slf4j .Slf4j ;
8+ import org .aspectj .lang .JoinPoint ;
9+ import org .aspectj .lang .annotation .AfterThrowing ;
10+ import org .aspectj .lang .annotation .Aspect ;
11+ import org .aspectj .lang .annotation .Before ;
12+ import org .aspectj .lang .annotation .Pointcut ;
13+ import org .springframework .stereotype .Component ;
14+ import org .springframework .web .context .request .RequestContextHolder ;
15+ import org .springframework .web .context .request .ServletRequestAttributes ;
16+
17+ import javax .servlet .http .HttpServletRequest ;
18+ import javax .servlet .http .HttpSession ;
19+ import java .lang .reflect .Method ;
20+
21+ @ Slf4j
22+ @ Aspect
23+ @ Component
24+ @ SuppressWarnings ("all" )
25+ public class SystemLogAspect {
26+
27+ //注入Service用于把日志保存数据库,实际项目入库采用队列做异步
28+ // @Resource
29+ // private ActionService actionService;
30+
31+ //Controller层切点
32+ @ Pointcut ("@annotation(cn.tellsea.annotation.SystemControllerLog)" )
33+ public void controllerAspect () {
34+ }
35+
36+ //Service层切点
37+ @ Pointcut ("@annotation(cn.tellsea.annotation.SystemServiceLog)" )
38+ public void serviceAspect () {
39+ }
40+
41+ /**
42+ * 前置通知 用于拦截Controller层记录用户的操作
43+ *
44+ * @param joinPoint
45+ */
46+ @ Before ("controllerAspect()" )
47+ public void doBefore (JoinPoint joinPoint ) {
48+ HttpServletRequest request = ((ServletRequestAttributes ) RequestContextHolder .getRequestAttributes ()).getRequest ();
49+ HttpSession session = request .getSession ();
50+ //读取session中的用户
51+ // User user = (User) session.getAttribute("user");
52+ String ip = IPUtils .getIpAddr (request );
53+ try {
54+ //*========控制台输出=========*//
55+ System .out .println ("==============前置通知开始==============" );
56+ System .out .println ("请求接口" + (joinPoint .getTarget ().getClass ().getName () + "." + joinPoint .getSignature ().getName ()));
57+ System .out .println ("方法描述:" + getControllerMethodDescription (joinPoint ));
58+ System .out .println ("请求人:" + "test" );
59+ System .out .println ("请求ip:" + ip );
60+
61+ //*========数据库日志=========*//
62+ // Action action = new Action();
63+ // action.setActionDes(getControllerMethodDescription(joinPoint));
64+ // action.setActionType("0");
65+ // action.setActionIp(ip);
66+ // action.setUserId(user.getId());
67+ // action.setActionTime(new Date());
68+ // //保存数据库
69+ // actionService.add(action);
70+
71+ } catch (Exception e ) {
72+ //记录本地异常日志
73+ log .error ("==前置通知异常==" );
74+ log .error ("异常信息:{}" , e .getMessage ());
75+ }
76+ }
77+
78+ /**
79+ * 异常通知 用于拦截service层记录异常日志
80+ *
81+ * @param joinPoint
82+ * @param e
83+ */
84+ @ AfterThrowing (pointcut = "serviceAspect()" , throwing = "e" )
85+ public void doAfterThrowing (JoinPoint joinPoint , Throwable e ) {
86+ HttpServletRequest request = ((ServletRequestAttributes ) RequestContextHolder .getRequestAttributes ()).getRequest ();
87+ HttpSession session = request .getSession ();
88+ //读取session中的用户
89+ // User user = (User) session.getAttribute("user");
90+ //获取请求ip
91+ String ip = IPUtils .getIpAddr (request );
92+ //获取用户请求方法的参数并序列化为JSON格式字符串
93+ String params = "" ;
94+ if (joinPoint .getArgs () != null && joinPoint .getArgs ().length > 0 ) {
95+ for (int i = 0 ; i < joinPoint .getArgs ().length ; i ++) {
96+ params += JSONObject .toJSON (joinPoint .getArgs ()[i ]) + ";" ;
97+ }
98+ }
99+ try {
100+ /*========控制台输出=========*/
101+ System .out .println ("=====异常通知开始=====" );
102+ System .out .println ("异常代码:" + e .getClass ().getName ());
103+ System .out .println ("异常信息:" + e .getMessage ());
104+ System .out .println ("异常方法:" + (joinPoint .getTarget ().getClass ().getName () + "." + joinPoint .getSignature ().getName () + "()" ));
105+ System .out .println ("方法描述:" + getServiceMethodDescription (joinPoint ));
106+ System .out .println ("请求人:" + null );
107+ System .out .println ("请求IP:" + ip );
108+ System .out .println ("请求参数:" + params );
109+ /*==========数据库日志=========*/
110+ // Action action = new Action();
111+ // action.setActionDes(getServiceMethodDescription(joinPoint));
112+ // action.setActionType("1");
113+ // action.setUserId(user.getId());
114+ // action.setActionIp(ip);
115+ // action.setActionTime(new Date());
116+ // //保存到数据库
117+ // actionService.add(action);
118+ } catch (Exception ex ) {
119+ //记录本地异常日志
120+ log .error ("==异常通知异常==" );
121+ log .error ("异常信息:{}" , ex .getMessage ());
122+ }
123+ }
124+
125+
126+ /**
127+ * 获取注解中对方法的描述信息 用于service层注解
128+ *
129+ * @param joinPoint
130+ * @return
131+ * @throws Exception
132+ */
133+ public static String getServiceMethodDescription (JoinPoint joinPoint ) throws Exception {
134+ String targetName = joinPoint .getTarget ().getClass ().getName ();
135+ String methodName = joinPoint .getSignature ().getName ();
136+ Object [] arguments = joinPoint .getArgs ();
137+ Class targetClass = Class .forName (targetName );
138+ Method [] methods = targetClass .getMethods ();
139+ String description = "" ;
140+ for (Method method : methods ) {
141+ if (method .getName ().equals (methodName )) {
142+ Class [] clazzs = method .getParameterTypes ();
143+ if (clazzs .length == arguments .length ) {
144+ description = method .getAnnotation (SystemServiceLog .class ).description ();
145+ break ;
146+ }
147+ }
148+ }
149+ return description ;
150+ }
151+
152+ /**
153+ * 获取注解中对方法的描述信息 用于Controller层注解
154+ *
155+ * @param joinPoint
156+ * @return
157+ * @throws Exception
158+ */
159+ public static String getControllerMethodDescription (JoinPoint joinPoint ) throws Exception {
160+ String targetName = joinPoint .getTarget ().getClass ().getName ();
161+ String methodName = joinPoint .getSignature ().getName ();//目标方法名
162+ Object [] arguments = joinPoint .getArgs ();
163+ Class targetClass = Class .forName (targetName );
164+ Method [] methods = targetClass .getMethods ();
165+ String description = "" ;
166+ for (Method method : methods ) {
167+ if (method .getName ().equals (methodName )) {
168+ Class [] clazzs = method .getParameterTypes ();
169+ if (clazzs .length == arguments .length ) {
170+ description = method .getAnnotation (SystemControllerLog .class ).description ();
171+ break ;
172+ }
173+ }
174+ }
175+ return description ;
176+ }
177+ }
0 commit comments