Commit c5b15e62 authored by AfirSraftGarrier's avatar AfirSraftGarrier

.

parent 122deff1
...@@ -32,6 +32,7 @@ public class LogAspect { ...@@ -32,6 +32,7 @@ public class LogAspect {
@Pointcut("@annotation(iot.sixiang.license.log.MyLog)") @Pointcut("@annotation(iot.sixiang.license.log.MyLog)")
public void logPointCut() { public void logPointCut() {
log.info("------>配置织入点");
} }
/** /**
...@@ -39,9 +40,9 @@ public class LogAspect { ...@@ -39,9 +40,9 @@ public class LogAspect {
* *
* @param joinPoint 切点 * @param joinPoint 切点
*/ */
@AfterReturning(pointcut = "logPointCut()") @AfterReturning(pointcut = "logPointCut()", returning = "jsonResult")
public void doAfterReturning(JoinPoint joinPoint) { public void doAfterReturning(JoinPoint joinPoint, Object jsonResult) {
handleLog(joinPoint, null); handleLog(joinPoint, null, jsonResult);
} }
/** /**
...@@ -52,43 +53,53 @@ public class LogAspect { ...@@ -52,43 +53,53 @@ public class LogAspect {
*/ */
@AfterThrowing(value = "logPointCut()", throwing = "e") @AfterThrowing(value = "logPointCut()", throwing = "e")
public void doAfterThrowing(JoinPoint joinPoint, Exception e) { public void doAfterThrowing(JoinPoint joinPoint, Exception e) {
handleLog(joinPoint, e); handleLog(joinPoint, e, null);
} }
private void handleLog(final JoinPoint joinPoint, final Exception e) { protected void handleLog(final JoinPoint joinPoint, final Exception e, Object jsonResult) {
// 获得MyLog注解 // 获得注解
MyLog controllerLog = getAnnotationLog(joinPoint); MyLog controllerLog = getAnnotationLog(joinPoint);
if (controllerLog == null) { if (controllerLog == null) {
return; return;
} }
SysOperLog operLog = new SysOperLog(); SysOperLog operLog = new SysOperLog();
// 操作状态(0正常 1异常)
operLog.setStatus(0); operLog.setStatus(0);
// 操作时间
operLog.setOperTime(new Date()); operLog.setOperTime(new Date());
if (e != null) { if (e != null) {
operLog.setStatus(1); operLog.setStatus(1);
// IotLicenseException为本系统自定义的异常类,读者若要获取异常信息,请根据自身情况变通
operLog.setErrorMsg(StringUtils.substring(((IotLicenseException) e).getMsg(), 0, 2000)); operLog.setErrorMsg(StringUtils.substring(((IotLicenseException) e).getMsg(), 0, 2000));
} }
// UserUtils.getUri();获取方法上的路径 如:/login,本文实现方法如下:
// 1、在拦截器中 String uri = request.getRequestURI();
// 2、用ThreadLocal存放uri,UserUtils.setUri(uri);
// 3、UserUtils.getUri();
String uri = UserUtils.getUri(); String uri = UserUtils.getUri();
operLog.setUri(uri); operLog.setUri(uri);
// 处理注解上的参数 // operLog.setMethod(className + "." + methodName + "()");
// 处理设置注解上的参数
getControllerMethodDescription(joinPoint, controllerLog, operLog); getControllerMethodDescription(joinPoint, controllerLog, operLog);
// 保存数据库 // 保存数据库
sysOperLogService.addOperlog(operLog.getTitle(), operLog.getBusinessType(), operLog.getUri(), operLog.getStatus(), operLog.getOptParam(), operLog.getErrorMsg(), operLog.getOperTime()); sysOperLogService.addOperlog(operLog.getTitle(), operLog.getBusinessType(), operLog.getUri(), operLog.getStatus(), operLog.getOptParam(), operLog.getErrorMsg(), operLog.getOperTime());
} }
/** /**
* 是否存在注解,如果存在就获取,不存在则返回null * 获取注解中对方法的描述信息 用于Controller层注解
* @param joinPoint *
* @return * @param myLog 日志
* @param operLog 操作日志
* @throws Exception
*/
public void getControllerMethodDescription(JoinPoint joinPoint, MyLog myLog, SysOperLog operLog) {
// 设置action动作
operLog.setBusinessType(myLog.businessType().ordinal());
// 设置标题
operLog.setTitle(myLog.title());
String optParam = getAnnotationValue(joinPoint, myLog.optParam());
operLog.setOptParam(optParam);
}
/**
* 是否存在注解,如果存在就获取
*/ */
private MyLog getAnnotationLog(JoinPoint joinPoint) { private MyLog getAnnotationLog(JoinPoint joinPoint) {
Signature signature = joinPoint.getSignature(); Signature signature = joinPoint.getSignature();
...@@ -100,36 +111,13 @@ public class LogAspect { ...@@ -100,36 +111,13 @@ public class LogAspect {
return null; return null;
} }
/** public String getAnnotationValue(JoinPoint joinPoint, String name) {
* 获取Controller层上MyLog注解中对方法的描述信息
* @param joinPoint 切点
* @param myLog 自定义的注解
* @param operLog 操作日志实体类
*/
private void getControllerMethodDescription(JoinPoint joinPoint, MyLog myLog, SysOperLog operLog) {
// 设置业务类型(0其它 1新增 2修改 3删除)
operLog.setBusinessType(myLog.businessType().ordinal());
// 设置模块标题,eg:登录
operLog.setTitle(myLog.title());
// 对方法上的参数进行处理,处理完:userName=xxx,password=xxx
String optParam = getAnnotationValue(joinPoint, myLog.optParam());
operLog.setOptParam(optParam);
}
/**
* 对方法上的参数进行处理
* @param joinPoint
* @param name
* @return
*/
private String getAnnotationValue(JoinPoint joinPoint, String name) {
String paramName = name; String paramName = name;
// 获取方法中所有的参数 // 获取方法中所有的参数
Map<String, Object> params = getParams(joinPoint); Map<String, Object> params = getParams(joinPoint);
// 参数是否是动态的:#{paramName} // 参数是否是动态的:#{paramName}
if (paramName.matches("^#\\{\\D*\\}")) { if (paramName.matches("^#\\{\\D*\\}")) {
// 获取参数名,去掉#{ } // 获取参数名
paramName = paramName.replace("#{", "").replace("}", ""); paramName = paramName.replace("#{", "").replace("}", "");
// 是否是复杂的参数类型:对象.参数名 // 是否是复杂的参数类型:对象.参数名
if (paramName.contains(".")) { if (paramName.contains(".")) {
...@@ -140,39 +128,35 @@ public class LogAspect { ...@@ -140,39 +128,35 @@ public class LogAspect {
JSONObject jsonObject = (JSONObject) JSONObject.toJSON(object); JSONObject jsonObject = (JSONObject) JSONObject.toJSON(object);
// 获取值 // 获取值
Object o = jsonObject.get(split[1]); Object o = jsonObject.get(split[1]);
return String.valueOf(o); return String.valueOf(o);
} else {// 简单的动态参数直接返回 } else {// 简单的动态参数直接返回
StringBuilder str = new StringBuilder(); StringBuilder str = new StringBuilder();
String[] paraNames = paramName.split(","); String[] paraNames = paramName.split(",");
for (String paraName : paraNames) { for (int i = 0; i < paraNames.length; i++) {
String paraName = paraNames[i];
String val = String.valueOf(getValue(params, paraName)); String val = String.valueOf(getValue(params, paraName));
// 组装成 userName=xxx,password=xxx,
str.append(paraName).append("=").append(val).append(","); str.append(paraName + "=" + val + ",");
} }
// 去掉末尾的,
if (str.toString().endsWith(",")) { if (str.toString().endsWith(",")) {
String substring = str.substring(0, str.length() - 1); String substring = str.substring(0, str.length() - 1);
return substring; return substring;
} else { } else {
return str.toString(); return str.toString();
} }
} }
} }
// 非动态参数直接返回 // 非动态参数直接返回
return name; return name;
} }
/**
* 获取方法上的所有参数,返回Map类型, eg: 键:"userName",值:xxx 键:"password",值:xxx
* @param joinPoint
* @return
*/
public Map<String, Object> getParams(JoinPoint joinPoint) { public Map<String, Object> getParams(JoinPoint joinPoint) {
Map<String, Object> params = new HashMap<>(8); Map<String, Object> params = new HashMap<>(8);
// 通过切点获取方法所有参数值["zhangsan", "123456"]
Object[] args = joinPoint.getArgs(); Object[] args = joinPoint.getArgs();
// 通过切点获取方法所有参数名 eg:["userName", "password"]
MethodSignature signature = (MethodSignature) joinPoint.getSignature(); MethodSignature signature = (MethodSignature) joinPoint.getSignature();
String[] names = signature.getParameterNames(); String[] names = signature.getParameterNames();
for (int i = 0; i < args.length; i++) { for (int i = 0; i < args.length; i++) {
...@@ -181,13 +165,7 @@ public class LogAspect { ...@@ -181,13 +165,7 @@ public class LogAspect {
return params; return params;
} }
/** public Object getValue(Map<String, Object> map, String paramName) {
* 从map中获取键为paramName的值,不存在放回null
* @param map
* @param paramName
* @return
*/
private Object getValue(Map<String, Object> map, String paramName) {
for (Map.Entry<String, Object> entry : map.entrySet()) { for (Map.Entry<String, Object> entry : map.entrySet()) {
if (entry.getKey().equals(paramName)) { if (entry.getKey().equals(paramName)) {
return entry.getValue(); return entry.getValue();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment