一、前言
最近测试提某些接口重复提交的问题,想了下应该不止是前端点击之后按钮不可点击的问题,后端应该根据登录token、操作方法、参数做多层的判断。
二、示例
切片代码
@Slf4j
@Aspect
@Component
public class ApiIdempotentAspect {
@Resource
RedisUtil redisUtil;
@Resource
UserUtils userUtils;
@Pointcut("@annotation(com.xx.anno.ApiIdempotent)")
private void pointCut() {
}
@Before("pointCut()")
public void doPoint(JoinPoint joinPoint) {
String action = joinPoint.getSignature().getDeclaringTypeName()
.substring(joinPoint.getSignature().getDeclaringTypeName().lastIndexOf(".")+1)
+ "::" + joinPoint.getSignature().getName();
String args = JSON.toJSONString(joinPoint.getArgs());
String token = userUtils.getAuthToke().replace("-","")
.replace("Bearer ","");
String idempotentKey = "api::idempotent::"+token+"::"+action;
//短时间内没进行相似操作
if(redisUtil.hasKey(idempotentKey)){
//接口参数是否一致
String idempotentValue = redisUtil.getCacheObject(idempotentKey);
log.info("idempotentValue : {}",idempotentValue);
if(args.equals(idempotentValue)){
throw new BusinessException("请勿重复操作");
}
} else{
//30s内禁止重复操作
redisUtil.setCacheObject(idempotentKey,args,30, TimeUnit.SECONDS);
}
}
}
用到一个redisutil
@Component
public class RedisUtil {
@Resource
public RedisTemplate redisTemplate;
public <T> void setCacheObject(final String key, final T value)
{
redisTemplate.opsForValue().set(key, value);
}
public <T> void setCacheObject(final String key, final T value, final Integer timeout, final TimeUnit timeUnit)
{
redisTemplate.opsForValue().set(key, value, timeout, timeUnit);
}
public boolean expire(final String key, final long timeout)
{
return expire(key, timeout, TimeUnit.SECONDS);
}
public boolean expire(final String key, final long timeout, final TimeUnit unit)
{
return redisTemplate.expire(key, timeout, unit);
}
public <T> T getCacheObject(final String key)
{
ValueOperations<String, T> operation = redisTemplate.opsForValue();
return operation.get(key);
}
public boolean deleteObject(final String key)
{
return redisTemplate.delete(key);
}
public long deleteObject(final Collection collection)
{
return redisTemplate.delete(collection);
}
public <T> long setCacheList(final String key, final List<T> dataList)
{
Long count = redisTemplate.opsForList().rightPushAll(key, dataList);
return count == null ? 0 : count;
}
public <T> List<T> getCacheList(final String key)
{
return redisTemplate.opsForList().range(key, 0, -1);
}
public <T> BoundSetOperations<String, T> setCacheSet(final String key, final Set<T> dataSet)
{
BoundSetOperations<String, T> setOperation = redisTemplate.boundSetOps(key);
Iterator<T> it = dataSet.iterator();
while (it.hasNext())
{
setOperation.add(it.next());
}
return setOperation;
}
public <T> Set<T> getCacheSet(final String key)
{
return redisTemplate.opsForSet().members(key);
}
public <T> void setCacheMap(final String key, final Map<String, T> dataMap)
{
if (dataMap != null) {
redisTemplate.opsForHash().putAll(key, dataMap);
}
}
public <T> Map<String, T> getCacheMap(final String key)
{
return redisTemplate.opsForHash().entries(key);
}
public <T> void setCacheMapValue(final String key, final String hKey, final T value)
{
redisTemplate.opsForHash().put(key, hKey, value);
}
public <T> T getCacheMapValue(final String key, final String hKey)
{
HashOperations<String, String, T> opsForHash = redisTemplate.opsForHash();
return opsForHash.get(key, hKey);
}
public <T> List<T> getMultiCacheMapValue(final String key, final Collection<Object> hKeys)
{
return redisTemplate.opsForHash().multiGet(key, hKeys);
}
public Collection<String> keys(final String pattern)
{
return redisTemplate.keys(pattern);
}
public boolean hasKey(final String key){
return redisTemplate.hasKey(key);
}
}
到此这篇关于springboot幂等切片的实现的文章就介绍到这了,更多相关springboot幂等切片内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!