提交 df192183 编写于 作者: 无难事者若执's avatar 无难事者若执

doc : 【幂等】: 通过ThreadLocal传递TokenHolder和辅助验证value信息

上级 4cbc1462
...@@ -10,14 +10,4 @@ public class Const { ...@@ -10,14 +10,4 @@ public class Const {
*/ */
public static final String BASE_PACKAGE = "com.kx.utils"; public static final String BASE_PACKAGE = "com.kx.utils";
/**
* 【幂等】默认请求体实现下,采用该方式作为携带的 token和value 参数列表
* <pre>
* Const.REQUEST_TOKEN_VALUE_ARGS[0] = "token"
* Const.REQUEST_TOKEN_VALUE_ARGS[1] = "userid"
*
* // 调用含有注解@Idempotent的类或方法,自动去传入到幂等 token校验中。
* </pre>
*/
public static final String[] REQUEST_TOKEN_VALUE_ARGS = new String[2];
} }
package com.kx.utils.idempotent.bean;
import lombok.Getter;
/**
* token 请求数据附加token结构
*
* @author kongxiang
*/
@Getter
public class TokenHolder {
/**
* 线程唯一ThreadLocal持有
* 【幂等】默认请求体实现下,采用ThreadLocal方案来传递 线程内部 请求参数token的value的传递
* <pre>
* // 在线程执行之初时,赋值
* TokenHolder.setTokenHolder("1","2");
* // 调用含有注解@Idempotent的类或方法,自动去传入到幂等 token校验中。
* // 在幂等校验时获取
* * TokenHolder th = TokenHolder.getTokenHolder();
* ... boolean valid = idempotent...valid(th);
* </pre>
*/
private static final ThreadLocal<TokenHolder> 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;
}
package com.kx.utils.idempotent.impl; package com.kx.utils.idempotent.impl;
import com.kx.Const;
import com.kx.utils.idempotent.TokenValueRequestHolder; import com.kx.utils.idempotent.TokenValueRequestHolder;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import com.kx.utils.idempotent.bean.TokenHolder;
import org.springframework.stereotype.Component;
/** /**
* 该框架默认支持的一种解决方案 * 该框架默认支持的一种解决方案
* 采用ThreadLocal注册
*
* @author kongxiang * @author kongxiang
*/ */
public class DefaultTokenValueRequestHolder implements TokenValueRequestHolder { public class DefaultTokenValueRequestHolder implements TokenValueRequestHolder {
private String[] args = Const.REQUEST_TOKEN_VALUE_ARGS;
@Override @Override
public String getToken() { public String getToken() {
return args[0]; TokenHolder token = TokenHolder.getTokenHolder();
if (token == null) {
return null;
}
return token.getToken();
} }
@Override @Override
public String getValidValue() { public String getValidValue() {
return args[1]; TokenHolder token = TokenHolder.getTokenHolder();
if (token == null) {
return null;
}
return token.getValidValue();
} }
} }
package com.kx.test; 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.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Import;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@SpringBootTest import java.util.Set;
public class IdTest {
@SpringBootTest(classes = IdAutoConfiguration.class)
@Import(ConfigPropertiesAutoConfiguration.class)
@RunWith(SpringJUnit4ClassRunner.class)
@Slf4j
public class IdTest {
@Autowired
private IdService idService;
@Test @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<String> supports = idService.supports();
assert supports.size() > 0;
supports.forEach(System.out::println);
} }
} }
package com.kx.test;
import org.junit.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
public class IdempotentTest {
@Test
public void test() {
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册