前言:
本文介绍基于redis和toke机制,自定义@RepeatSubmit注解来实现防止接口重复调用情况。
实现教程:
- 自定义注解是基于spring aop实现的,所以需要先查看是否引入了aop的依赖,如果没有的话在pom文件中添加以下依赖,还有redis的依赖。
<!-- 引入aop相关 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<!-- redis-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
- redis配置为:
spring:
redis:
database: 0
host: 127.0.0.1
port: 6379
timeout: 5000
password:
- 接下来我们来自定义@RepeatSubmit注解,这里的time我们用来控制防止重复提交的时间,默认为一秒。
使用@RepeatSubmit(time = xxx)来指定时间。
package com.blog.utils.aop;
import java.lang.annotation.*;
/**
* @Description: 自定义注解防止表单重复提交
* @Author: LiWT
* @Date: 2021/6/24
*/
@Inherited
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface RepeatSubmit {
int time() default 1;
}
- 开头我们就提到了,是使用spring aop实现的,那么接下来我们来定义切面、切入点
package com.blog.utils.aop;
import com.blog.utils.Md5Utils;
import com.blog.utils.RedisCache;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
/**
* @Description: 防止重复提交
* @Author: LiWT
* @Date: 2021/6/24
*/
@Component
@Slf4j
@Aspect
public class NoRepeatSubmitAspect {
private static final String JWT_TOKEN_KEY = "Authorization";
@Autowired
private RedisCache redisCache;
@Pointcut("@annotation(com.blog.utils.aop.RepeatSubmit)")
public void serviceNoRepeat() {
}
@Around("serviceNoRepeat()")
public Object around(ProceedingJoinPoint pjp) throws Throwable {
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
String jwtToken = request.getHeader(JWT_TOKEN_KEY);
if (!StringUtils.hasText(jwtToken)) {
jwtToken = UUID.randomUUID().toString();
}
String key = Md5Utils.hash(jwtToken + "-" + request.getRequestURL().toString());
if (redisCache.getCacheObject(key) == null) {
try {
Object o = pjp.proceed();
MethodSignature signature = (MethodSignature) pjp.getSignature();
RepeatSubmit repeatSubmit = signature.getMethod().getAnnotation(RepeatSubmit.class);
// 默认1秒内统一用户同一个地址同一个参数,视为重复提交
redisCache.setCacheObject(key, signature.getMethod().toString(), repeatSubmit.time(), TimeUnit.SECONDS);
return o;
} catch (Exception e) {
throw new RuntimeException(e.getMessage());
}
} else {
throw new RuntimeException("请勿重复请求");
}
}
}
- 其中@Aspect定义了这个类是一个切面,@Pointcut为切入点,它指向了我们刚定义的注解,所以我们可以使用@RepeatSubmit
这个注解来实现aop,@Around为环绕增强。 - 其大概实现思路是我们将用户的token信息及用户的请求路径拼接作为key,将参数信息作为value,同时将我们注解中定义的time作为过期时间存入redis缓存中,在下次请求到来前先检测该key是否存在于缓存中,如果存在则认为它是重复提交,抛出相应异常。
- 刚我们也有提到是基于redis和token机制实现的防重复提交,所以会依赖于redis,这里不再过多讲述redis了,redis工具类如下,直接粘贴到项目中即可。
package com.blog.utils;
import com.alibaba.fastjson.support.spring.FastJsonRedisSerializer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Component;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
/**
* redis 工具类
* @author LiWT
* @Date 2021-06-22
**/
@SuppressWarnings(value = {"unchecked", "rawtypes"})
@Component
public class RedisCache {
public RedisTemplate redisTemplate;
@Autowired
public RedisCache(RedisTemplate redisTemplate){
this.redisTemplate=redisTemplate;
this.redisTemplate.setKeySerializer(new StringRedisSerializer());
this.redisTemplate.setValueSerializer(new FastJsonRedisSerializer(Object.class));
}
/**
* 缓存基本的对象,Integer、String、实体类等
*
* @param key 缓存的键值
* @param value 缓存的值
*/
public <T> void setCacheObject(final String key, final T value) {
redisTemplate.opsForValue().set(key, value);
}
/**
* 缓存基本的对象,Integer、String、实体类等
*
* @param key 缓存的键值
* @param value 缓存的值
* @param timeout 时间
* @param timeUnit 时间颗粒度
*/
public <T> void setCacheObject(final String key, final T value, final Integer timeout, final TimeUnit timeUnit) {
redisTemplate.opsForValue().set(key, value, timeout, timeUnit);
}
/**
* 设置有效时间
*
* @param key Redis键
* @param timeout 超时时间
* @return true=设置成功;false=设置失败
*/
public boolean expire(final String key, final long timeout) {
return expire(key, timeout, TimeUnit.SECONDS);
}
/**
* 设置有效时间
*
* @param key Redis键
* @param timeout 超时时间
* @param unit 时间单位
* @return true=设置成功;false=设置失败
*/
public boolean expire(final String key, final long timeout, final TimeUnit unit) {
return redisTemplate.expire(key, timeout, unit);
}
/**
* 获得缓存的基本对象。
*
* @param key 缓存键值
* @return 缓存键值对应的数据
*/
public <T> T getCacheObject(final String key) {
ValueOperations<String, T> operation = redisTemplate.opsForValue();
return operation.get(key);
}
/**
* 删除单个对象
*
* @param key
*/
public boolean deleteObject(final String key) {
return redisTemplate.delete(key);
}
/**
* 删除集合对象
*
* @param collection 多个对象
* @return
*/
public long deleteObject(final Collection collection) {
return redisTemplate.delete(collection);
}
/**
* 缓存List数据
*
* @param key 缓存的键值
* @param %values 待缓存的List数据
* @return 缓存的对象
*/
public <T> long setCacheList(final String key, final List<T> dataList) {
Long count = redisTemplate.opsForList().rightPushAll(key, dataList);
return count == null ? 0 : count;
}
/**
* 获得缓存的list对象
*
* @param key 缓存的键值
* @return 缓存键值对应的数据
*/
public <T> List<T> getCacheList(final String key) {
return redisTemplate.opsForList().range(key, 0, -1);
}
/**
* 缓存Set
*
* @param key 缓存键值
* @param dataSet 缓存的数据
* @return 缓存数据的对象
*/
public <T> long setCacheSet(final String key, final Set<T> dataSet) {
Long count = redisTemplate.opsForSet().add(key, dataSet);
return count == null ? 0 : count;
}
/**
* 获得缓存的set
*
* @param key
* @return
*/
public <T> Set<T> getCacheSet(final String key) {
return redisTemplate.opsForSet().members(key);
}
/**
* 缓存Map
*
* @param key
* @param dataMap
*/
public <T> void setCacheMap(final String key, final Map<String, T> dataMap) {
if (dataMap != null) {
redisTemplate.opsForHash().putAll(key, dataMap);
}
}
/**
* 获得缓存的Map
*
* @param key
* @return
*/
public <T> Map<String, T> getCacheMap(final String key) {
return redisTemplate.opsForHash().entries(key);
}
/**
* 往Hash中存入数据
*
* @param key Redis键
* @param hKey Hash键
* @param value 值
*/
public <T> void setCacheMapValue(final String key, final String hKey, final T value) {
redisTemplate.opsForHash().put(key, hKey, value);
}
/**
* 获取Hash中的数据
*
* @param key Redis键
* @param hKey Hash键
* @return Hash中的对象
*/
public <T> T getCacheMapValue(final String key, final String hKey) {
HashOperations<String, String, T> opsForHash = redisTemplate.opsForHash();
return opsForHash.get(key, hKey);
}
/**
* 获取多个Hash中的数据
*
* @param key Redis键
* @param hKeys Hash键集合
* @return Hash对象集合
*/
public <T> List<T> getMultiCacheMapValue(final String key, final Collection<Object> hKeys) {
return redisTemplate.opsForHash().multiGet(key, hKeys);
}
/**
* 获得缓存的基本对象列表
*
* @param pattern 字符串前缀
* @return 对象列表
*/
public Collection<String> keys(final String pattern) {
return redisTemplate.keys(pattern);
}
/**
* @param key 增长的key
* @return
*/
public Long increment(final String key) {
return redisTemplate.opsForValue().increment(key);
}
}
- 还需要一个MD5加密的方法,也是直接粘贴至项目中即可,当然也可以使用其他的加密算法。
package com.blog.utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.security.MessageDigest;
/**
* Md5加密方法
*/
public class Md5Utils {
private static final Logger log = LoggerFactory.getLogger(Md5Utils.class);
private static byte[] md5(String s) {
MessageDigest algorithm;
try {
algorithm = MessageDigest.getInstance("MD5");
algorithm.reset();
algorithm.update(s.getBytes("UTF-8"));
byte[] messageDigest = algorithm.digest();
return messageDigest;
} catch (Exception e) {
log.error("MD5 Error...", e);
}
return null;
}
private static String toHex(byte hash[]) {
if (hash == null) {
return null;
}
StringBuffer buf = new StringBuffer(hash.length * 2);
int i;
for (i = 0; i < hash.length; i++) {
if ((hash[i] & 0xff) < 0x10) {
buf.append("0");
}
buf.append(Long.toString(hash[i] & 0xff, 16));
}
return buf.toString();
}
public static String hash(String s) {
try {
return new String(toHex(md5(s)).getBytes("UTF-8"), "UTF-8");
} catch (Exception e) {
log.error("not supported charset...", e);
return s;
}
}
}
- ok,我们的配置到这里就结束了,接下来让我们来测试一下,测试接口如下:
package com.blog.controller;
import com.blog.utils.aop.RepeatSubmit;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @Author: LiWT
* @Date: 2021/6/22
*/
@RestController
@RequestMapping("/test")
public class TestController {
/**
* 测试重复提交
* 这里我们将time设置为3秒,也就是3秒内我们同一个请求,同一个token,同一个参数不能重复请求。
* @param param 参数
*/
@GetMapping("/testRepeatSubmit")
@RepeatSubmit(time = 3)
public String testRepeatSubmit(String param) {
System.out.println("param:" + param);
return "请求成功,参数为:" + param;
}
}
-
这里我使用的是postman测试工具,首先在请求头中设置Authorization为我们的token,token我就随意填写了,然后填入我们的测试接口开始测试。
-
可以看出我们第一次请求是成功响应,紧接着我们再进行第二次请求,也就是我们模拟的重复提交表单
-
第二次提示请勿重复请求,且响应状态码为500,ok,到这里我们的防止重复提交的注解就完成了,防止重复提交在实际项目中还是比较实用的,小伙伴们快去自己项目中体验吧!