package com.netauth.api.aoplog;
import java.lang.annotation.*;
@Target(ElementType.METHOD) //注解放置的目标位置,METHOD是可注解在方法级别上
@Retention(RetentionPolicy.RUNTIME) //注解在哪个阶段执行
@Documented
public @interface OperLog {
String operModul() default ""; // 操作模块
String operType() default ""; // 操作类型
String operDesc() default ""; // 操作说明
}
package com.netauth.api.aoplog;
import java.lang.reflect.Method;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import javax.servlet.http.HttpServletRequest;
import com.alibaba.fastjson.JSON;
import com.netauth.entity.user.UserLoginBean;
import com.netauth.utils.currentuser.LoginUserUtil;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
@Aspect
@Component
public class OperLogAspect {
@Pointcut("@annotation(com.netauth.api.aoplog.OperLog)")
public void operLogPoinCut() {
}
@Pointcut("execution(* com.infosec.user..*.*(..))")
public void operExceptionLogPoinCut() {
}
@AfterReturning(value = "operLogPoinCut()", returning = "keys")
public void saveOperLog(JoinPoint joinPoint, Object keys) {
// 获取RequestAttributes
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
// 从获取RequestAttributes中获取HttpServletRequest的信息
HttpServletRequest request = (HttpServletRequest) requestAttributes
.resolveReference(RequestAttributes.REFERENCE_REQUEST);
Map
UserLoginBean localUser = LoginUserUtil.getLoginUserBean(request);
try {
// map.put("uuid", UUID.randomUUID().toString().replace("-",""));
// 从切面织入点处通过反射机制获取织入点处的方法
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
// 获取切入点所在的方法
Method method = signature.getMethod();
// 获取操作
OperLog opLog = method.getAnnotation(OperLog.class);
if (opLog != null) {
String operModul = opLog.operModul();
String operType = opLog.operType();
String operDesc = opLog.operDesc();
map.put("operModul",operModul);// 操作模块
map.put("operType",operType);// 操作类型
map.put("operDesc",operDesc);// 操作描述
}
// 获取请求的类名
String className = joinPoint.getTarget().getClass().getName();
// 获取请求的方法名
String methodName = method.getName();
methodName = className + "." + methodName;
String username = localUser.getLoginName();
map.put("loginInfo", username);
map.put("operMethod",methodName);// 请求方法
// 请求的参数
// Map
// 将参数所在的数组转换成json
// String params = JSON.toJSONString(rtnMap);
// map.put("params",params);// 请求参数
map.put("result",JSON.toJSONString(keys));// 返回结果
map.put("requestURI",request.getRequestURI());// 请求URI
//打印结果
System.out.println("打印操作日志:"+map.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
@AfterThrowing(pointcut = "operExceptionLogPoinCut()", throwing = "e")
public void saveExceptionLog(JoinPoint joinPoint, Throwable e) {
// 获取RequestAttributes
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
// 从获取RequestAttributes中获取HttpServletRequest的信息
HttpServletRequest request = (HttpServletRequest) requestAttributes
.resolveReference(RequestAttributes.REFERENCE_REQUEST);
Map
try {
// 从切面织入点处通过反射机制获取织入点处的方法
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
// 获取切入点所在的方法
Method method = signature.getMethod();
// map.put("uuid", UUID.randomUUID().toString().replace("-",""));
// 获取请求的类名
String className = joinPoint.getTarget().getClass().getName();
// 获取请求的方法名
String methodName = method.getName();
methodName = className + "." + methodName;
// 请求的参数
Map
// 将参数所在的数组转换成json
String params = JSON.toJSONString(rtnMap);
// map.put("params",params);// 请求参数
map.put("methodName",methodName);// 请求方法名
map.put("excName",e.getClass().getName());
// map.put("excMessage",stackTraceToString(e.getClass().getName(), e.getMessage(), e.getStackTrace()));// 异常信息
map.put("requestURI",request.getRequestURI());// 操作URI
map.put("excCreateTime",new Date());// 发生异常时间
System.out.println("打印异常日志:"+map.toString());
} catch (Exception e2) {
e2.printStackTrace();
}
}
public Map
Map
for (String key : paramMap.keySet()) {
rtnMap.put(key, paramMap.get(key)[0]);
}
return rtnMap;
}
public String stackTraceToString(String exceptionName, String exceptionMessage, StackTraceElement[] elements) {
StringBuffer strbuff = new StringBuffer();
for (StackTraceElement stet : elements) {
strbuff.append(stet + "n");
}
String message = exceptionName + ":" + exceptionMessage + "nt" + strbuff.toString();
return message;
}
}



