@Retention(RetentionPolicy.RUNTIME)
public @interface ControllerLoggerIgnore {
public String title() default "排除";
}
@Around("@within(org.springframework.web.bind.annotation.RestController)")
public Object aroundMethod(ProceedingJoinPoint joinPoint) throws Throwable {
// 1.获取切入点所在目标对象
Object targetObj = joinPoint.getTarget();
// System.out.println(targetObj.getClass().getName());
// 2.获取切入点方法的名字
String methodName = joinPoint.getSignature().getName();
// System.out.println("切入方法名字:" + methodName);
// 3.获取方法上的注解
Signature signature = joinPoint.getSignature();
MethodSignature methodSignature = (MethodSignature) signature;
Method method = methodSignature.getMethod();
if (method != null) {
ControllerLoggerIgnore apiLog = method.getAnnotation(ControllerLoggerIgnore.class);
if (null != apiLog) {
System.out.println("切入方法注解的title:" + apiLog.title());
}
}
// 4.获取方法的参数
Object[] _args = joinPoint.getArgs();
for (Object o : _args) {
// System.out.println("切入方法的参数:" + o);
}
log.info("\n开始捕获用户操作日志 aroundMethod :" +
"\n1.获取切入点所在目标对象 - {} " +
"\n2.获取切入点方法的名字 - {} " +
"\n3.获取方法上的注解 - {} " +
"\n4.获取方法的参数 - {}" +
"\n",
targetObj.getClass().getName(), methodName, "", _args
);
SysUserOperationLogParam userOperation = new SysUserOperationLogParam();
long startTime = 0;
try {
// 获取Request对象
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
LoginSysUser loginSysUser = JwtUtil.getLoginSysUser(request);
if (null != loginSysUser) {
// 操作人
userOperation.setUserName(loginSysUser.getSysUserName());
// 操作人账号
userOperation.setNickName(loginSysUser.getSysUserName());
} else {
// 操作人
userOperation.setUserName("公司打杂");
// 操作人账号
userOperation.setNickName("公司打杂");
}
// 操作名称
userOperation.setMethodName(joinPoint.getSignature().getName());
// 操作时间
Date date = new Date();
startTime = date.getTime();
userOperation.setOperationTime(date);
// 请求开始时间
userOperation.setRequestStartTime(date);
// 请求IP
// userOperation.setRequestIp(IPUtil.getRemoteIpAddr(request));
// URL
userOperation.setRequestUrl(request.getRequestURI());
// 模块
userOperation.setModel(model);
Object[] args = new Object[0];
try {
// 请求参数 TODO 目录 系统 不统一 有的可能会报错 不用理
args = joinPoint.getArgs();
Integer isLogRequestParam = 0;
// if(args instanceof java.util.logging.ErrorManager){
// isLogRequestParam = 1;
// } else
if (null != args) {
if (java.io.Serializable.class.isInstance(args)) {
isLogRequestParam = 1;
}
}
// 过滤不能序列化的
if (isLogRequestParam.equals(1)) {
String param = JsonUtils.toJson(args);
userOperation.setRequestParam(param);
}
} catch (RuntimeException re) {
} catch (Exception e) {
log.error("截取参数异常", e);
}
} catch (Exception e) {
e.printStackTrace();
}
Object proceed = null;
try {
// TODO 目前这里的问题 excel 下载有问题
proceed = joinPoint.proceed();
userOperation.setResultStatus("success");
return proceed;
} catch (Exception e) {
// 堆栈错误信息
String stackTrace = Arrays.toString(e.getStackTrace());
// 错误信息
String message = e.getMessage();
ConcurrentHashMap<String, String> errorMsg = new ConcurrentHashMap<>();
errorMsg.put("stackTrace", stackTrace);
errorMsg.put("message", e.getClass().getName() + ": " + message);
userOperation.setErrorInfo(JsonUtils.toJson(errorMsg));
userOperation.setResultStatus("error");
throw e;
} finally {
try {
if (null != proceed) {
// 返回结果
userOperation.setResponseResult(JsonUtils.toJson(proceed));
}
// 请求结束时间
Date endDate = new Date();
userOperation.setRequestEndTime(endDate);
userOperation.setResponseDate(endDate.getTime() - startTime);
} catch (Exception e) {
log.error("获取返回结果异常{}", e);
}
// 异步将操作日志保存
CompletableFuture.runAsync(() -> {
try {
// 保存日志,具体方法自定义
sysUserOperationLogDubboConsumerService.addSysUserOperationLog(userOperation);
} catch (Exception e2) {
log.error("用户操作日记记录异常:{}", e2);
}
});
}
}
java.lang.reflect.Method.getAnnotation(Class <T> annotationClass)
方法如果存在这样的注释,则返回指定类型的元素的注释,否则为null
。
以下是java.lang.reflect.Method.getAnnotation(Class <T> annotationClass)
方法的声明。
public <T extends Annotation> T getAnnotation(Class<T> annotationClass)
参数
Class
对象对相应的注释类型。返回值
null
。异常
以下示例显示java.lang.reflect.Method.getAnnotation(Class<T> annotationClass)
方法的用法。
import java.lang.annotation.Annotation;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.Method;
public class MethodDemo {
public static void main(String[] args) {
Method[] methods = SampleClass.class.getMethods();
Annotation annotation = methods[0].getAnnotation(CustomAnnotation.class);
if(annotation instanceof CustomAnnotation){
CustomAnnotation customAnnotation = (CustomAnnotation) annotation;
System.out.println("name: " + customAnnotation.name());
System.out.println("value: " + customAnnotation.value());
}
}
}
@CustomAnnotation(name="SampleClass", value = "Sample Class Annotation")
class SampleClass {
private String sampleField;
@CustomAnnotation(name="getSampleMethod", value = "Sample Method Annotation")
public String getSampleField() {
return sampleField;
}
public void setSampleField(String sampleField) {
this.sampleField = sampleField;
}
}
@Retention(RetentionPolicy.RUNTIME)
@interface CustomAnnotation {
public String name();
public String value();
}
让我们编译并运行上面的程序,这将产生以下结果 -
name: getSampleMethod
value: Sample Method Annotation
原网址: 访问
创建于: 2021-03-10 15:33:23
目录: default
标签: 无
未标明原创文章均为采集,版权归作者所有,转载无需和我联系,请注明原出处,南摩阿彌陀佛,知识,不只知道,要得到
最新评论