From df1921835e2307256b310e58ccaf98d74b216df8 Mon Sep 17 00:00:00 2001
From: kongxiang <316378085@qq.com>
Date: Sun, 27 Aug 2023 12:22:09 +0800
Subject: [PATCH] =?UTF-8?q?doc=20:=20=E3=80=90=E5=B9=82=E7=AD=89=E3=80=91?=
=?UTF-8?q?=EF=BC=9A=20=E9=80=9A=E8=BF=87ThreadLocal=E4=BC=A0=E9=80=92Toke?=
=?UTF-8?q?nHolder=E5=92=8C=E8=BE=85=E5=8A=A9=E9=AA=8C=E8=AF=81value?=
=?UTF-8?q?=E4=BF=A1=E6=81=AF?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
src/main/java/com/kx/Const.java | 10 ----
.../kx/utils/idempotent/bean/TokenHolder.java | 51 +++++++++++++++++++
.../impl/DefaultTokenValueRequestHolder.java | 20 +++++---
src/test/java/com/kx/test/IdTest.java | 31 +++++++++--
src/test/java/com/kx/test/IdempotentTest.java | 14 +++++
5 files changed, 104 insertions(+), 22 deletions(-)
create mode 100644 src/main/java/com/kx/utils/idempotent/bean/TokenHolder.java
create mode 100644 src/test/java/com/kx/test/IdempotentTest.java
diff --git a/src/main/java/com/kx/Const.java b/src/main/java/com/kx/Const.java
index 18ac917..8d71968 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 0000000..87add7c
--- /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 95ed894..e032310 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 9aa1daa..4ffd3d0 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 0000000..4c55fd2
--- /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() {
+
+ }
+}
--
GitLab