diff --git a/src/main/java/com/kx/Const.java b/src/main/java/com/kx/Const.java index 18ac917369dcc8d83b74fca94bc814a45a5b2588..8d71968c05cd7b86146aa3c5f026d5d29106c242 100644 --- a/src/main/java/com/kx/Const.java +++ b/src/main/java/com/kx/Const.java @@ -10,14 +10,4 @@ public class Const { */ public static final String BASE_PACKAGE = "com.kx.utils"; - /** - * 【幂等】默认请求体实现下,采用该方式作为携带的 token和value 参数列表 - *
-     *      Const.REQUEST_TOKEN_VALUE_ARGS[0] = "token"
-     *      Const.REQUEST_TOKEN_VALUE_ARGS[1] = "userid"
-     *
-     *      // 调用含有注解@Idempotent的类或方法,自动去传入到幂等 token校验中。
-     *  
- */ - public static final String[] REQUEST_TOKEN_VALUE_ARGS = new String[2]; } diff --git a/src/main/java/com/kx/utils/idempotent/bean/TokenHolder.java b/src/main/java/com/kx/utils/idempotent/bean/TokenHolder.java new file mode 100644 index 0000000000000000000000000000000000000000..87add7cf550f3a2dcfdbbd139d408aea7ba39d21 --- /dev/null +++ b/src/main/java/com/kx/utils/idempotent/bean/TokenHolder.java @@ -0,0 +1,51 @@ +package com.kx.utils.idempotent.bean; + +import lombok.Getter; + +/** + * token 请求数据附加token结构 + * + * @author kongxiang + */ +@Getter +public class TokenHolder { + /** + * 线程唯一ThreadLocal持有 + * 【幂等】默认请求体实现下,采用ThreadLocal方案来传递 线程内部 请求参数token的value的传递 + *
+     *      // 在线程执行之初时,赋值
+     *      TokenHolder.setTokenHolder("1","2");
+     *      // 调用含有注解@Idempotent的类或方法,自动去传入到幂等 token校验中。
+     *      // 在幂等校验时获取
+     *      *      TokenHolder th = TokenHolder.getTokenHolder();
+     *        ... boolean valid = idempotent...valid(th);
+     *  
+ */ + private static final ThreadLocal TOKEN_HOLDER_THREAD_LOCAL = new ThreadLocal<>(); + + public static TokenHolder setTokenHolder(String token, String value) { + synchronized (TOKEN_HOLDER_THREAD_LOCAL) { + TokenHolder tokenHolder = new TokenHolder(); + tokenHolder.token = token; + tokenHolder.validValue = value; + TOKEN_HOLDER_THREAD_LOCAL.remove(); + TOKEN_HOLDER_THREAD_LOCAL.set(tokenHolder); + return tokenHolder; + } + } + + public static TokenHolder getTokenHolder() { + synchronized (TOKEN_HOLDER_THREAD_LOCAL) { + return TOKEN_HOLDER_THREAD_LOCAL.get(); + } + } + + /** + * token + */ + private String token; + /** + * 辅助认证信息 + */ + private String validValue; +} diff --git a/src/main/java/com/kx/utils/idempotent/impl/DefaultTokenValueRequestHolder.java b/src/main/java/com/kx/utils/idempotent/impl/DefaultTokenValueRequestHolder.java index 95ed89486d1e25edbc93c72c4a9c1012405ddd40..e0323106020031fdca9b73148520c8b9cac3d83e 100644 --- a/src/main/java/com/kx/utils/idempotent/impl/DefaultTokenValueRequestHolder.java +++ b/src/main/java/com/kx/utils/idempotent/impl/DefaultTokenValueRequestHolder.java @@ -1,25 +1,31 @@ package com.kx.utils.idempotent.impl; -import com.kx.Const; import com.kx.utils.idempotent.TokenValueRequestHolder; -import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; -import org.springframework.stereotype.Component; +import com.kx.utils.idempotent.bean.TokenHolder; /** * 该框架默认支持的一种解决方案 + * 采用ThreadLocal注册 + * * @author kongxiang */ public class DefaultTokenValueRequestHolder implements TokenValueRequestHolder { - private String[] args = Const.REQUEST_TOKEN_VALUE_ARGS; - @Override public String getToken() { - return args[0]; + TokenHolder token = TokenHolder.getTokenHolder(); + if (token == null) { + return null; + } + return token.getToken(); } @Override public String getValidValue() { - return args[1]; + TokenHolder token = TokenHolder.getTokenHolder(); + if (token == null) { + return null; + } + return token.getValidValue(); } } diff --git a/src/test/java/com/kx/test/IdTest.java b/src/test/java/com/kx/test/IdTest.java index 9aa1daa880cd811c78de659bfcebd1fee10a8570..4ffd3d0b3a01d0187ac7ac544eaccd19d28711ab 100644 --- a/src/test/java/com/kx/test/IdTest.java +++ b/src/test/java/com/kx/test/IdTest.java @@ -1,16 +1,37 @@ package com.kx.test; +import com.kx.config.ConfigPropertiesAutoConfiguration; +import com.kx.config.IdAutoConfiguration; +import com.kx.utils.id.IdService; +import lombok.extern.slf4j.Slf4j; import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.context.annotation.Import; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; -@SpringBootTest -public class IdTest { - +import java.util.Set; +@SpringBootTest(classes = IdAutoConfiguration.class) +@Import(ConfigPropertiesAutoConfiguration.class) +@RunWith(SpringJUnit4ClassRunner.class) +@Slf4j +public class IdTest { + @Autowired + private IdService idService; @Test - public void IdGen(){ - + public void IdGen() { + String generate = idService.generate(); + assert generate != null; + log.info("generate : {}", generate); + String uuid = idService.generate("uuid"); + assert uuid != null; + log.info("uuid : {}", uuid); + Set supports = idService.supports(); + assert supports.size() > 0; + supports.forEach(System.out::println); } } diff --git a/src/test/java/com/kx/test/IdempotentTest.java b/src/test/java/com/kx/test/IdempotentTest.java new file mode 100644 index 0000000000000000000000000000000000000000..4c55fd265c292e78d78d02930bace8f3fa6b4dff --- /dev/null +++ b/src/test/java/com/kx/test/IdempotentTest.java @@ -0,0 +1,14 @@ +package com.kx.test; + + +import org.junit.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +public class IdempotentTest { + + @Test + public void test() { + + } +}