diff --git a/src/main/java/com/easysoftware/common/aop/RequestLimitRedisAspect.java b/src/main/java/com/easysoftware/common/aop/RequestLimitRedisAspect.java index 715a8624c16bb0dfcdad4d7f37b11265e19a9403..d0a5fe25d69ce5feba68ff2e75d45da8e41fe09d 100644 --- a/src/main/java/com/easysoftware/common/aop/RequestLimitRedisAspect.java +++ b/src/main/java/com/easysoftware/common/aop/RequestLimitRedisAspect.java @@ -27,6 +27,7 @@ import org.springframework.web.context.request.ServletRequestAttributes; import com.easysoftware.common.entity.MessageCode; import com.easysoftware.common.utils.ClientUtil; +import com.easysoftware.common.utils.LogUtil; import com.easysoftware.common.utils.ResultUtil; import jakarta.servlet.http.HttpServletRequest; @@ -64,7 +65,8 @@ public class RequestLimitRedisAspect { private long rejectCount; /** - * Pointcut method to define where the aspect applies based on the RequestLimitRedis annotation. + * Pointcut method to define where the aspect applies based on the + * RequestLimitRedis annotation. * * @param requestLimit The RequestLimitRedis annotation. */ @@ -73,10 +75,13 @@ public class RequestLimitRedisAspect { } /** - * Advice method that intercepts the method calls annotated with RequestLimitRedis and enforces request limiting. + * Advice method that intercepts the method calls annotated with + * RequestLimitRedis and enforces request limiting. * - * @param joinPoint The ProceedingJoinPoint representing the intercepted method. - * @param requestLimit The RequestLimitRedis annotation containing request limiting criteria. + * @param joinPoint The ProceedingJoinPoint representing the intercepted + * method. + * @param requestLimit The RequestLimitRedis annotation containing request + * limiting criteria. * @return The result of the intercepted method execution. * @throws Throwable if an error occurs during method execution. */ @@ -98,13 +103,11 @@ public class RequestLimitRedisAspect { String uri = request.getRequestURI(); String key = "req_limit:".concat(uri).concat(ip); - ZSetOperations zSetOperations = redisTemplate.opsForZSet(); long currentMs = System.currentTimeMillis(); zSetOperations.add(key, currentMs, currentMs); - redisTemplate.expire(key, period, TimeUnit.SECONDS); // remove the value that out of current window @@ -116,13 +119,11 @@ public class RequestLimitRedisAspect { if (count != null && count > limitCount) { // 审计日志 LOGGER.error("the current uri is{},the request frequency of uri exceeds the limited frequency: " - + "{} times/{}s ,IP:{}", uri, limitCount, period, ip); + + "{} times/{}s ,IP:{},type: GET", LogUtil.formatCodeString(uri), limitCount, period, ip); return ResultUtil.fail(HttpStatus.TOO_MANY_REQUESTS, MessageCode.EC00010); } - return joinPoint.proceed(); } } - diff --git a/src/main/java/com/easysoftware/common/utils/LogUtil.java b/src/main/java/com/easysoftware/common/utils/LogUtil.java index 4fe9ba932a83d879f350252acbea5b57366f879b..672a3a11b6318c51f892d440ff99ea63be98aaa4 100644 --- a/src/main/java/com/easysoftware/common/utils/LogUtil.java +++ b/src/main/java/com/easysoftware/common/utils/LogUtil.java @@ -30,7 +30,6 @@ import java.util.Locale; public final class LogUtil { - // Private constructor to prevent instantiation of the utility class private LogUtil() { // private constructor to hide the implicit public one @@ -56,9 +55,9 @@ public final class LogUtil { */ @SneakyThrows public static void managementOperate(final JoinPoint joinPoint, - final HttpServletRequest request, - final HttpServletResponse response, - final Object returnObject) { + final HttpServletRequest request, + final HttpServletResponse response, + final Object returnObject) { ManagementLog log = new ManagementLog(); log.setType("OmOperate"); @@ -88,4 +87,23 @@ public final class LogUtil { LOGGER.info("operationLog:{}", jsonLog); } + /** + * format logging parameter. + * + * @param input The input pramater + * @param formatedOutput The safe output logging parmeter + */ + + public static String formatCodeString(String input) { + if (input == null) { + return input; + } + + String formatedOutput = input.replace("\r", "\\r").replace("\n", "\\n").replace("\u0008", "\\u0008") + .replace("\u000B", "\\u000B") + .replace("\u000C", "\\u000C") + .replace("\u007F", "\\u007F"); + + return formatedOutput; + } }