提交 | 用户 | 时间
|
1ac2bc
|
1 |
/** |
懒 |
2 |
* Copyright 2018-2020 stylefeng & fengshuonan (https://gitee.com/stylefeng) |
|
3 |
* <p> |
|
4 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
5 |
* you may not use this file except in compliance with the License. |
|
6 |
* You may obtain a copy of the License at |
|
7 |
* <p> |
|
8 |
* http://www.apache.org/licenses/LICENSE-2.0 |
|
9 |
* <p> |
|
10 |
* Unless required by applicable law or agreed to in writing, software |
|
11 |
* distributed under the License is distributed on an "AS IS" BASIS, |
|
12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
13 |
* See the License for the specific language governing permissions and |
|
14 |
* limitations under the License. |
|
15 |
*/ |
|
16 |
package cn.stylefeng.guns.sys.core.log.aop; |
|
17 |
|
|
18 |
import cn.stylefeng.guns.base.auth.context.LoginContextHolder; |
|
19 |
import cn.stylefeng.guns.base.auth.model.LoginUser; |
|
20 |
import cn.stylefeng.guns.base.dict.AbstractDictMap; |
|
21 |
import cn.stylefeng.guns.base.log.BussinessLog; |
|
22 |
import cn.stylefeng.guns.sys.core.log.LogManager; |
|
23 |
import cn.stylefeng.guns.sys.core.log.LogObjectHolder; |
|
24 |
import cn.stylefeng.guns.sys.core.log.factory.LogTaskFactory; |
|
25 |
import cn.stylefeng.guns.sys.core.util.Contrast; |
|
26 |
import cn.stylefeng.roses.core.util.HttpContext; |
|
27 |
import org.aspectj.lang.ProceedingJoinPoint; |
|
28 |
import org.aspectj.lang.Signature; |
|
29 |
import org.aspectj.lang.annotation.Around; |
|
30 |
import org.aspectj.lang.annotation.Aspect; |
|
31 |
import org.aspectj.lang.annotation.Pointcut; |
|
32 |
import org.aspectj.lang.reflect.MethodSignature; |
|
33 |
import org.slf4j.Logger; |
|
34 |
import org.slf4j.LoggerFactory; |
|
35 |
import org.springframework.stereotype.Component; |
|
36 |
|
|
37 |
import java.lang.reflect.Method; |
|
38 |
import java.util.Map; |
|
39 |
|
|
40 |
/** |
|
41 |
* 日志记录 |
|
42 |
* |
|
43 |
* @author fengshuonan |
|
44 |
* @date 2016年12月6日 下午8:48:30 |
|
45 |
*/ |
|
46 |
@Aspect |
|
47 |
@Component |
|
48 |
public class LogAop { |
|
49 |
|
|
50 |
private Logger log = LoggerFactory.getLogger(this.getClass()); |
|
51 |
|
|
52 |
@Pointcut(value = "@annotation(cn.stylefeng.guns.base.log.BussinessLog)") |
|
53 |
public void cutService() { |
|
54 |
} |
|
55 |
|
|
56 |
@Around("cutService()") |
|
57 |
public Object recordSysLog(ProceedingJoinPoint point) throws Throwable { |
|
58 |
|
|
59 |
//先执行业务 |
|
60 |
Object result = point.proceed(); |
|
61 |
|
|
62 |
try { |
|
63 |
handle(point); |
|
64 |
} catch (Exception e) { |
|
65 |
log.error("日志记录出错!", e); |
|
66 |
} |
|
67 |
|
|
68 |
return result; |
|
69 |
} |
|
70 |
|
|
71 |
private void handle(ProceedingJoinPoint point) throws Exception { |
|
72 |
|
|
73 |
//获取拦截的方法名 |
|
74 |
Signature sig = point.getSignature(); |
|
75 |
MethodSignature msig = null; |
|
76 |
if (!(sig instanceof MethodSignature)) { |
|
77 |
throw new IllegalArgumentException("该注解只能用于方法"); |
|
78 |
} |
|
79 |
msig = (MethodSignature) sig; |
|
80 |
Object target = point.getTarget(); |
|
81 |
Method currentMethod = target.getClass().getMethod(msig.getName(), msig.getParameterTypes()); |
|
82 |
String methodName = currentMethod.getName(); |
|
83 |
|
|
84 |
//如果当前用户未登录,不做日志 |
|
85 |
LoginUser user = LoginContextHolder.getContext().getUser(); |
|
86 |
if (null == user) { |
|
87 |
return; |
|
88 |
} |
|
89 |
|
|
90 |
//获取拦截方法的参数 |
|
91 |
String className = point.getTarget().getClass().getName(); |
|
92 |
Object[] params = point.getArgs(); |
|
93 |
|
|
94 |
//获取操作名称 |
|
95 |
BussinessLog annotation = currentMethod.getAnnotation(BussinessLog.class); |
|
96 |
String bussinessName = annotation.value(); |
|
97 |
String key = annotation.key(); |
|
98 |
Class dictClass = annotation.dict(); |
|
99 |
|
|
100 |
StringBuilder sb = new StringBuilder(); |
|
101 |
for (Object param : params) { |
|
102 |
sb.append(param); |
|
103 |
sb.append(" & "); |
|
104 |
} |
|
105 |
|
|
106 |
//如果涉及到修改,比对变化 |
|
107 |
String msg; |
|
108 |
if (bussinessName.contains("修改") || bussinessName.contains("编辑")) { |
|
109 |
Object obj1 = LogObjectHolder.me().get(); |
|
110 |
Map<String, String> obj2 = HttpContext.getRequestParameters(); |
|
111 |
msg = Contrast.contrastObj(dictClass, key, obj1, obj2); |
|
112 |
} else { |
|
113 |
Map<String, String> parameters = HttpContext.getRequestParameters(); |
|
114 |
AbstractDictMap dictMap = (AbstractDictMap) dictClass.newInstance(); |
|
115 |
msg = Contrast.parseMutiKey(dictMap, key, parameters); |
|
116 |
} |
|
117 |
|
|
118 |
LogManager.me().executeLog(LogTaskFactory.bussinessLog(user.getId(), bussinessName, className, methodName, msg)); |
|
119 |
} |
|
120 |
} |