From 5e16cb904f09ad448a8c6631e051a1daceec4e39 Mon Sep 17 00:00:00 2001 From: chuhang <891580081@qq.com> Date: Thu, 1 Jul 2021 14:18:57 +0800 Subject: [PATCH] =?UTF-8?q?fix(uboot-core):=20=E5=B9=82=E7=AD=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修复delKey = true。即业务执行完,删除key --- .../idempotent/aspect/IdempotentAspect.java | 70 +++++++------------ 1 file changed, 27 insertions(+), 43 deletions(-) diff --git a/uboot-common/src/main/java/com/chinaunicom/mall/ebtp/common/idempotent/aspect/IdempotentAspect.java b/uboot-common/src/main/java/com/chinaunicom/mall/ebtp/common/idempotent/aspect/IdempotentAspect.java index 0f1083e..de67f69 100644 --- a/uboot-common/src/main/java/com/chinaunicom/mall/ebtp/common/idempotent/aspect/IdempotentAspect.java +++ b/uboot-common/src/main/java/com/chinaunicom/mall/ebtp/common/idempotent/aspect/IdempotentAspect.java @@ -1,7 +1,9 @@ package com.chinaunicom.mall.ebtp.common.idempotent.aspect; +import com.chinaunicom.mall.ebtp.cloud.security.starter.common.Constants; import com.chinaunicom.mall.ebtp.common.exception.common.CommonExceptionEnum; import com.chinaunicom.mall.ebtp.common.idempotent.annotation.Idempotent; +import com.chinaunicom.mall.ebtp.common.util.HttpContextUtils; import com.chinaunicom.mall.ebtp.common.util.JsonUtils; import lombok.extern.slf4j.Slf4j; import org.aspectj.lang.JoinPoint; @@ -13,16 +15,11 @@ import org.aspectj.lang.reflect.MethodSignature; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Component; -import org.springframework.util.CollectionUtils; -import org.springframework.web.context.request.RequestContextHolder; -import org.springframework.web.context.request.ServletRequestAttributes; import javax.servlet.http.HttpServletRequest; import java.lang.reflect.Method; import java.time.LocalDateTime; import java.util.Arrays; -import java.util.HashMap; -import java.util.Map; import java.util.concurrent.TimeUnit; /** @@ -35,14 +32,6 @@ import java.util.concurrent.TimeUnit; @Aspect public class IdempotentAspect { - private static final ThreadLocal> THREAD_CACHE = ThreadLocal.withInitial(HashMap::new); - - private static final String RMAPCACHE_KEY = "idempotent"; - - private static final String KEY = "key"; - - private static final String DELKEY = "delKey"; - @Autowired private RedisTemplate redisTemplate; @@ -52,10 +41,8 @@ public class IdempotentAspect { @Before("pointCut()") public void beforePointCut(JoinPoint joinPoint) { - ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder - .getRequestAttributes(); - assert requestAttributes != null; - HttpServletRequest request = requestAttributes.getRequest(); + + HttpServletRequest request = HttpContextUtils.getHttpServletRequest(); MethodSignature signature = (MethodSignature) joinPoint.getSignature(); Method method = signature.getMethod(); @@ -64,12 +51,7 @@ public class IdempotentAspect { } Idempotent idempotent = method.getAnnotation(Idempotent.class); - /* key : url + 参数列表作为区分 */ - String url = request.getRequestURL().toString(); - String argString = JsonUtils.objectToJson(Arrays.asList(joinPoint.getArgs())); - assert argString != null; - String redisKey = String.valueOf(argString.hashCode()); - String key = url.concat(":").concat(redisKey); + String key = getKey(joinPoint, request); long expireTime = idempotent.expireTime(); String info = idempotent.info(); @@ -77,47 +59,49 @@ public class IdempotentAspect { boolean delKey = idempotent.delKey(); // do not need check null - Map rMapCache = redisTemplate.opsForHash().entries(RMAPCACHE_KEY); - String value = LocalDateTime.now().toString().replace("T", " "); - if (null != rMapCache.get(key)) { + Object o = redisTemplate.opsForValue().get(key); + if (null != o) { // had stored CommonExceptionEnum.FRAME_EXCEPTION_COMMON_DATA_OTHER_ERROR.customValidName(info, true); } synchronized (this) { - boolean submitAble = redisTemplate.opsForValue().setIfAbsent(key, value, expireTime, timeUnit); + boolean submitAble = redisTemplate.opsForValue().setIfAbsent(key, delKey, expireTime, timeUnit); if (!submitAble) { CommonExceptionEnum.FRAME_EXCEPTION_COMMON_DATA_OTHER_ERROR.customValidName(info, true); } else { - log.info("[idempotent]:has stored key={},value={},expireTime={}{},now={}", key, value, expireTime, - timeUnit, LocalDateTime.now().toString()); + log.info("[idempotent]:has stored key={},value={},expireTime={}{},now={}", key, delKey, expireTime, + timeUnit, LocalDateTime.now()); } } - Map map = THREAD_CACHE.get(); - map.put(KEY, key); - map.put(DELKEY, delKey); + } + + private String getKey(JoinPoint joinPoint, HttpServletRequest request) { + String token = request.getHeader(Constants.AUTHORIZATION_HEADER); + /* key : token + url + 参数列表作为区分 */ + String url = request.getRequestURI(); + String argString = JsonUtils.objectToJson(Arrays.asList(joinPoint.getArgs())); + assert argString != null; + String redisKey = String.valueOf(argString.hashCode()); + return token.concat(":").concat(url).concat(":").concat(redisKey); } @After("pointCut()") public void afterPointCut(JoinPoint joinPoint) { - Map map = THREAD_CACHE.get(); - if (CollectionUtils.isEmpty(map)) { + HttpServletRequest request = HttpContextUtils.getHttpServletRequest(); + String key = getKey(joinPoint, request); + + Object o = redisTemplate.opsForValue().get(key); + if (o == null) { return; } - Map mapCache = redisTemplate.opsForHash().entries(RMAPCACHE_KEY); - if (mapCache.size() == 0) { - return; - } - - String key = map.get(KEY).toString(); - boolean delKey = (boolean) map.get(DELKEY); + boolean delKey = (boolean) o; if (delKey) { - mapCache.remove(key); + redisTemplate.delete(key); log.info("[idempotent]:has removed key={}", key); } - THREAD_CACHE.remove(); } }