JAVA 添加用户操作日志Aop+Annotation
一般是用于管理系统,记录管理人员对数据的一些操作信息。
一,新增注解
/** * @author xxx * @description 操作日志 * @date 2020/10/19 */ @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) public @interface ZyLog { String value() default ""; }
二,新增AOP
import lombok.extern.slf4j.Slf4j;
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.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.servlet.http.HttpServletRequest;
/** * @author xxx * @description 操作日志 * @date 2020/10/19 */ @Slf4j @Aspect @Component public class LogAspect { @Autowired private ZyLogService zyLogService; @Pointcut("@annotation(com.zy.wechat.annotation.ZyLog)") public void pointcut() {} @Around("pointcut()") public Object around(ProceedingJoinPoint point) throws Throwable { Object result = null; long beginTime = System.currentTimeMillis(); // 执行方法 result = point.proceed(); HttpServletRequest request = HttpContextUtil.getHttpServletRequest(); // 设置 IP地址 String ip = IPUtil.getIpAddr(request); // 执行时长(毫秒) long time = System.currentTimeMillis() - beginTime; // 保存日志;(这里是自定义的获取登录用户信息的对象) LoginUserV3 user = LoginUserUtilV3.getLoginUser(); ZyLog log = new ZyLog(); if (user != null){ log.setUserName(user.getUsername()); } log.setIp(ip); log.setTime(time); zyLogService.saveLog(point, log); return result; } }
三,获取IP定位所需插件pom
<!-- IP定位插件 --> <dependency> <groupId>org.lionsoul</groupId> <artifactId>ip2region</artifactId> <version>1.7</version> </dependency>
四,Service
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.zy.core.base.ZyBaseService;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.LocalVariableTableParameterNameDiscoverer;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.Serializable;
import java.lang.reflect.Method;
import java.util.*;
/** * @author xxx * @description 操作日志 * @date 2020/10/19 */ @Service public class ZyLogService extends ZyBaseService<ZyLog, ZyLogMapper> { @Autowired private ZyLogMapper zyLogMapper; @Autowired private ObjectMapper objectMapper; /** * @description: 保存操作日志 * @param point * @param log (数据库表zy_log表) * @return: void * @author: xxx * @Date: 2020/10/19 15:58 */ public void saveLog(ProceedingJoinPoint point, ZyLog log) throws JsonProcessingException { MethodSignature signature = (MethodSignature) point.getSignature(); Method method = signature.getMethod(); com.zy.wechat.annotation.ZyLog logAnnotation = method.getAnnotation(com.zy.wechat.annotation.ZyLog.class); if (logAnnotation != null) { // 注解上的描述 log.setOperation(logAnnotation.value()); } // 请求的类名 String className = point.getTarget().getClass().getName(); // 请求的方法名 String methodName = signature.getName(); log.setMethod(className + "." + methodName + "()"); // 请求的方法参数值 Object[] args = point.getArgs(); // 请求的方法参数名称 LocalVariableTableParameterNameDiscoverer u = new LocalVariableTableParameterNameDiscoverer(); String[] paramNames = u.getParameterNames(method); if (args != null && paramNames != null) { StringBuilder params = new StringBuilder(); params = handleParams(params, args, Arrays.asList(paramNames)); log.setParams(params.toString()); } log.setCreateTime(new Date()); log.setLocation(AddressUtil.getCityInfo(log.getIp())); // 保存系统日志 zyLogMapper.insert(log); } /** * @description:设置参数 * @param params * @param args * @param paramNames * @return: java.lang.StringBuilder * @author: xxx * @Date: 2020/10/19 15:59 */ private StringBuilder handleParams(StringBuilder params, Object[] args, List paramNames) throws JsonProcessingException { for (int i = 0; i < args.length; i++) { if (args[i] instanceof Map) { Set set = ((Map) args[i]).keySet(); List<Object> list = new ArrayList<>(); List<Object> paramList = new ArrayList<>(); for (Object key : set) { list.add(((Map) args[i]).get(key)); paramList.add(key); } return handleParams(params, list.toArray(), paramList); } else { if (args[i] instanceof Serializable) { Class<?> aClass = args[i].getClass(); try { aClass.getDeclaredMethod("toString", new Class[]{null}); // 如果不抛出 NoSuchMethodException 异常则存在 toString 方法 ,安全的 writeValueAsString ,否则 走 Object的 toString方法 params.append(" ").append(paramNames.get(i)).append(": ").append(objectMapper.writeValueAsString(args[i])); } catch (NoSuchMethodException e) { params.append(" ").append(paramNames.get(i)).append(": ").append(objectMapper.writeValueAsString(args[i].toString())); } } else if (args[i] instanceof MultipartFile) { MultipartFile file = (MultipartFile) args[i]; params.append(" ").append(paramNames.get(i)).append(": ").append(file.getName()); } else { params.append(" ").append(paramNames.get(i)).append(": ").append(args[i]); } } } return params; }
五,工具类
/** * @author zxq * @description * @date 2020/10/19 */ public class HttpContextUtil { private HttpContextUtil(){ } public static HttpServletRequest getHttpServletRequest() { return ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest(); } } /** * @author MrBird */ public class IPUtil { private static final String UNKNOWN = "unknown"; protected IPUtil(){ } /** * 获取 IP地址 * 使用 Nginx等反向代理软件, 则不能通过 request.getRemoteAddr()获取 IP地址 * 如果使用了多级反向代理的话,X-Forwarded-For的值并不止一个,而是一串IP地址, * X-Forwarded-For中第一个非 unknown的有效IP字符串,则为真实IP地址 */ public static String getIpAddr(HttpServletRequest request) { String ip = request.getHeader("x-forwarded-for"); if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) { ip = request.getHeader("Proxy-Client-IP"); } if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) { ip = request.getHeader("WL-Proxy-Client-IP"); } if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) { ip = request.getRemoteAddr(); } return "0:0:0:0:0:0:0:1".equals(ip) ? "127.0.0.1" : ip; } } /** * @author xxx * @description * @date 2020/10/19 */ @Slf4j public class AddressUtil { public static String getCityInfo(String ip) { DbSearcher searcher = null; try { String dbPath = AddressUtil.class.getResource("/ip2region/ip2region.db").getPath(); File file = new File(dbPath); if (!file.exists()) { String tmpDir = System.getProperties().getProperty("java.io.tmpdir"); dbPath = tmpDir + "ip.db"; file = new File(dbPath); FileUtils.copyInputStreamToFile(Objects.requireNonNull(AddressUtil.class.getClassLoader().getResourceAsStream("classpath:ip2region/ip2region.db")), file); } DbConfig config = new DbConfig(); searcher = new DbSearcher(config, file.getPath()); Method method = searcher.getClass().getMethod("btreeSearch", String.class); if (!Util.isIpAddress(ip)) { log.error("Error: Invalid ip address"); } DataBlock dataBlock = (DataBlock) method.invoke(searcher, ip); return dataBlock.getRegion(); } catch (Exception e) { log.error("获取地址信息异常", e); }finally { if (searcher !=null) { try { searcher.close(); } catch (IOException e) { log.error("获取地址信息异常:",e); } } } return ""; } }
六,如何使用,在需要记录的接口上添加注解。
七,表结构
DROP TABLE IF EXISTS `zy_log`; CREATE TABLE `zy_log` ( `id` BIGINT(20) NOT NULL AUTO_INCREMENT COMMENT '日志ID', `user_name` VARCHAR(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '操作用户', `operation` TEXT CHARACTER SET utf8 COLLATE utf8_general_ci NULL COMMENT '操作内容', `time` DECIMAL(11, 0) NULL DEFAULT NULL COMMENT '耗时', `method` TEXT CHARACTER SET utf8 COLLATE utf8_general_ci NULL COMMENT '操作方法', `params` TEXT CHARACTER SET utf8 COLLATE utf8_general_ci NULL COMMENT '方法参数', `ip` VARCHAR(64) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '操作者IP', `create_time` DATETIME(0) NULL DEFAULT NULL COMMENT '创建时间', `location` VARCHAR(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '操作地点', PRIMARY KEY (`id`) USING BTREE ) ENGINE = INNODB AUTO_INCREMENT = 1 CHARACTER SET = utf8 COLLATE = utf8_general_ci COMMENT = '操作日志表' ROW_FORMAT = COMPACT; SET FOREIGN_KEY_CHECKS = 1;