java aop拦截增删改成_spring-boot aop 增删改操作日志 实现
发布日期:2021-06-25 03:25:48 浏览次数:6 分类:技术文章

本文共 4637 字,大约阅读时间需要 15 分钟。

1.注解接口:

import com.github.wxiaoqi.security.common.constant.Constants;

import java.lang.annotation.*;

/**

* 日志注解

*/

@Target(ElementType.METHOD)

@Retention(RetentionPolicy.RUNTIME)

@Documented

public @interface SysLogOpt {

String value();

String module() default ""; //模块名称 系统管理-用户管理-列表页面

String description() default ""; //描述

Constants.LogOptEnum operationType() default Constants.LogOptEnum.UNKNOW;//操作类型

}

2.切面类

import cn.hutool.http.HttpUtil;

import com.alibaba.fastjson.JSON;

import com.duiyue.common.mvc.core.APIResponse;

import com.duiyue.common.util.LogUtils;

import com.github.wxiaoqi.security.admin.annotation.SysLogOpt;

import com.github.wxiaoqi.security.admin.biz.SysLogService;

import com.github.wxiaoqi.security.api.entity.SysLog;

import com.github.wxiaoqi.security.common.constant.Constants;

import com.github.wxiaoqi.security.common.context.BaseContextHandler;

import com.github.wxiaoqi.security.common.util.RegexUtil;

import lombok.extern.slf4j.Slf4j;

import org.apache.commons.lang3.StringUtils;

import org.aspectj.lang.ProceedingJoinPoint;

import org.aspectj.lang.annotation.Around;

import org.aspectj.lang.annotation.Aspect;

import org.aspectj.lang.annotation.Pointcut;

import org.springframework.stereotype.Component;

import org.springframework.web.context.request.RequestContextHolder;

import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;

import javax.servlet.http.HttpServletRequest;

import java.lang.reflect.Method;

import java.util.Date;

@Slf4j

@Aspect

@Component

public class LogAspect {

/**

* 开始时间

*/

private long startTime = 0L;

@Resource

private SysLogService logService;

@Pointcut("execution(* *..rest..*.*(..)) && @annotation(com.github.wxiaoqi.security.admin.annotation.SysLogOpt)")

public void logPointCut() {

}

@Around("logPointCut()")

public Object doAround(ProceedingJoinPoint pjp) throws Throwable {

startTime = System.currentTimeMillis();

Object result = null;

SysLog sysLogModel = logPre(pjp);

try {

result = pjp.proceed();

} finally {

//查询类型不添加日志

if (Constants.LogOptEnum.QUERY.value() != sysLogModel.getOperationType()) {

if (logAfter(result, sysLogModel).getUserName() != null) {

sysLogModel.setCreateTime(new Date());

sysLogModel.setUpdateTime(new Date());

logService.add(sysLogModel);

}

}

}

return result;

}

private SysLog logPre(ProceedingJoinPoint pjp) throws Exception {

SysLog sysLogModel = new SysLog();

for (Method method : Class.forName(pjp.getTarget().getClass().getName()).getMethods()) {

if (method.getName().equals(pjp.getSignature().getName())) {

Class[] clazzs = method.getParameterTypes();

if (clazzs.length == pjp.getArgs().length) {

//方法名称

sysLogModel.setOperation(method.getAnnotation(SysLogOpt.class).value());

//操作类型

sysLogModel.setOperationType(method.getAnnotation(SysLogOpt.class).operationType().value());

break;

}

}

}

//获取请求对象

HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();

startTime = System.currentTimeMillis();

String ip = HttpUtil.getClientIP(request);

//方法名含包名(om.github.wxiaoqi.security.admin.rest.SysLogController.queryListPage)

String classMethod = pjp.getSignature().getDeclaringTypeName() + "." + pjp.getSignature().getName();

//请求参数

String args = JSON.toJSONString(pjp.getArgs()).replaceAll(RegexUtil.getJSonValueRegex("password"), "****").replaceAll(RegexUtil.getJSonValueRegex("oldPassword"), "****");

sysLogModel.setIp(ip);

sysLogModel.setMethod(classMethod);

sysLogModel.setParams(args);

sysLogModel.setCreateTime(new Date());

sysLogModel.setCreateUser(0L);

sysLogModel.setUpdateUser(0L);

String currentUserName = "";

try {

currentUserName = BaseContextHandler.getUsername();

} catch (Exception e) {

LogUtils.error(log, "", e);

}

if (StringUtils.isNotEmpty(currentUserName)) {

sysLogModel.setUserName(currentUserName);

}

return sysLogModel;

}

private SysLog logAfter(Object result, SysLog sysLogModel) {

APIResponse response = null;

if (result != null) {

response = (APIResponse) result;

}

String currentUserName = "";

try {

currentUserName = BaseContextHandler.getUsername();

} catch (Exception e) {

LogUtils.error(log, "", e);

}

if (StringUtils.isNotEmpty(currentUserName)) {

sysLogModel.setUserName(currentUserName);

}

//返回结果

if (response != null && response.getStatus() == Constants.ResultCodeEnum.SUCCESS.value()) {

sysLogModel.setResult(1);

} else {

sysLogModel.setResult(0);

}

//执行时长(毫秒)

Long spendTime = System.currentTimeMillis() - startTime;

sysLogModel.setProcessTime(spendTime);

return sysLogModel;

}

}

3.控制层使用(方法上使用)。

@SysLogOpt(module = "活动管理", value = "添加活动", operationType = Constants.LogOptEnum.ADD)

(注:枚举类型未给出,自行修改)

转载地址:https://blog.csdn.net/weixin_35901163/article/details/114584908 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:java编译字节码转化程序下载_编译与翻译,“编译”Java到字节码?
下一篇:mac 应用程序已被java安全阻止_关于 Java for Mac OS X 10.5 系统更新 2 的安全性内容...

发表评论

最新留言

留言是一种美德,欢迎回访!
[***.207.175.100]2024年04月14日 06时15分58秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章