JAVA 添加用戶操作日志Aop+Annotation


  一般是用於管理系統,記錄管理人員對數據的一些操作信息。

 

一,新增注解  

/**
 * @author xxx
 * @description 操作日志
 * @date 2020/10/19
 */
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface ZyLog {
    String value() default "";
}

 

 

 

二,新增AOP

 

/**
 * @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

 

/**
 * @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
     * @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;

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM