发展到现在,基本上的javaweb应用都是前后端分离,在提交表单的时候,容易遇到网络延时或者是闪屏快速点击多次的情况,然后造成多个纪录的插入,这并不是我们想要的结果,鄙人有以下方法可以不同程度的防止重复提交:
1、前段控制,前端控制可以说是很多人的选择,因为简单,不需要考虑太多的其他因素,保障不会重复表单提交就好:比如:提交后把提交按钮设置为不可点击(disabled),或者设置子一个boolearn类型的值,依据值判断能否再次提交,再者可以使用promise
实现同步操作
2、后台校验:每一次操作者前,判断是否存在该数据(唯一值判断),如果存在则删除,反之新增
3、spring AOP 面向切面编程,过程:编写一个注解类(把注解卸载你调用的接口类方法上),和一个切面(切面切得就是这个注解类),切面的操作就是,设置指令(token+method:这个值必须保证唯一性)
上代码:
/**
* @Author hhl
* ToDo 防止重复提交注解类
* @Date 2020/11/10 0010 18:39
* @Version 1.0
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface NoRepeatSubmit {
/**
* 设置请求锁定时间
*
* @return
*/
int lockTime() default 10;
}
/**
* @Author hhl
* @Date 2020/11/10 0010 18:43
* @Version 1.0
*/
@Service
public class RedisLock {
@Autowired
private RedisUtil redisUtil;
/**
* 该加锁方法仅针对单实例 Redis 可实现分布式加锁
* 对于 Redis 集群则无法使用
*
* 支持重复,线程安全
*
* @param lockKey 加锁键
* @param clientId 加锁客户端唯一标识(采用UUID)
* @param seconds 锁过期时间
* @return
*/
public boolean tryLock(String lockKey, String clientId, long seconds) {
Object isExits = redisUtil.get(lockKey);
if (Objects.isNull(isExits)) {
redisUtil.set(lockKey, clientId, seconds);
return true;
} else {
return false;
}
}
/**
* 与 tryLock 相对应,用作释放锁
*
* @param lockKey
* @param clientId
* @return
*/
public boolean releaseLock(String lockKey, String clientId) {
if (StringUtils.isNotBlank(lockKey) && StringUtils.isNotBlank(clientId) ) {
redisUtil.remove(lockKey);
return true;
} else {
return false;
}
}
}
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.*;
import org.springframework.data.redis.support.atomic.RedisAtomicLong;
import org.springframework.stereotype.Component;
import java.io.Serializable;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
@Component
public class RedisUtil {
@Autowired
private RedisTemplate redisTemplate;
/**
* 写入缓存
* @param key
* @param value
* @return
*/
public boolean set(final String key, Object value) {
boolean result = false;
try {
ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
operations.set(key, value);
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* 写入缓存设置时效时间
* @param key
* @param value
* @return
*/
public boolean set(final String key, Object value, Long expireTime) {
boolean result = false;
try {
ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
operations.set(key, value);
redisTemplate.expire(key, expireTime, TimeUnit.SECONDS);
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* 批量删除对应的value
* @param keys
*/
public void remove(final String... keys) {
for (String key : keys) {
remove(key);
}
}
/**
* 批量删除key
* @param pattern
*/
public void removePattern(final String pattern) {
Set<Serializable> keys = redisTemplate.keys(pattern);
if (keys.size() > 0)
redisTemplate.delete(keys);
}
/**
* 删除对应的value
* @param key
*/
public void remove(final String key) {
if (exists(key)) {
redisTemplate.delete(key);
}
}
/**
* 判断缓存中是否有对应的value
* @param key
* @return
*/
public boolean exists(final String key) {
return redisTemplate.hasKey(key);
}
/**
* 读取缓存
* @param key
* @return
*/
public Object get(final String key) {
Object result = null;
ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
result = operations.get(key);
return result;
}
/**
* 获取缓存的过期时间
*/
public long getExpire(final String key){
return redisTemplate.getExpire(key);
}
/**
* 哈希 添加
* @param key
* @param hashKey
* @param value
*/
public void hmSet(String key, Object hashKey, Object value){
HashOperations<String, Object, Object> hash = redisTemplate.opsForHash();
hash.put(key,hashKey,value);
}
/**
* 哈希 添加 设置时效时间
* @param key
* @param hashKey
* @param value
* @param expireTime
*/
public void hmSet(String key, Object hashKey, Object value, Long expireTime){
HashOperations<String, Object, Object> hash = redisTemplate.opsForHash();
hash.put(key,hashKey,value) ;
redisTemplate.expire(key, expireTime, TimeUnit.SECONDS);
}
/**
* 哈希获取数据
* @param key
* @param hashKey
* @return
*/
public Object hmGet(String key, Object hashKey){
HashOperations<String, Object, Object> hash = redisTemplate.opsForHash();
return hash.get(key,hashKey);
}
/**
* 列表添加
* @param k
* @param v
*/
public void lPush(String k,Object v){
ListOperations<String, Object> list = redisTemplate.opsForList();
list.rightPush(k,v);
}
/**
* 列表获取
* @param k
* @param l
* @param l1
* @return
*/
public List<Object> lRange(String k, long l, long l1){
ListOperations<String, Object> list = redisTemplate.opsForList();
return list.range(k,l,l1);
}
/**
* 集合添加
* @param key
* @param value
*/
public void add(String key,Object value){
SetOperations<String, Object> set = redisTemplate.opsForSet();
set.add(key,value);
}
/**
* 集合获取
* @param key
* @return
*/
public Set<Object> setMembers(String key){
SetOperations<String, Object> set = redisTemplate.opsForSet();
return set.members(key);
}
/**
* 有序集合添加
* @param key
* @param value
* @param scoure
*/
public void zAdd(String key,Object value,double scoure){
ZSetOperations<String, Object> zset = redisTemplate.opsForZSet();
zset.add(key,value,scoure);
}
/**
* 有序集合获取
* @param key
* @param scoure
* @param scoure1
* @return
*/
public Set<Object> rangeByScore(String key,double scoure,double scoure1){
ZSetOperations<String, Object> zset = redisTemplate.opsForZSet();
return zset.rangeByScore(key, scoure, scoure1);
}
/**
* @Description: 获取自增长值
* @param key key
* @return
*/
public Long getIncr(String key) {
RedisAtomicLong entityIdCounter = new RedisAtomicLong(key, redisTemplate.getConnectionFactory());
Long increment = entityIdCounter.getAndIncrement();
entityIdCounter.expire(60*30, TimeUnit.SECONDS);
return increment;
}
public Long getIncr(String key, long timeOut) {
RedisAtomicLong entityIdCounter = new RedisAtomicLong(key, redisTemplate.getConnectionFactory());
Long increment = entityIdCounter.getAndIncrement();
entityIdCounter.expire(timeOut, TimeUnit.SECONDS);
return increment;
}
public Long getIncr(String key, long timeOut, TimeUnit timeUnit) {
RedisAtomicLong entityIdCounter = new RedisAtomicLong(key, redisTemplate.getConnectionFactory());
Long increment = entityIdCounter.getAndIncrement();
entityIdCounter.expire(timeOut, timeUnit);
return increment;
}
/**
* @Description: 初始化自增长值
* @param key key
* @param value 当前值
*/
public void setIncr(String key, int value) {
RedisAtomicLong counter = new RedisAtomicLong(key, redisTemplate.getConnectionFactory());
counter.set(value);
counter.expire(60*30, TimeUnit.SECONDS);
}
public void setIncr(String key, int value, long timeOut) {
RedisAtomicLong counter = new RedisAtomicLong(key, redisTemplate.getConnectionFactory());
counter.set(value);
counter.expire(timeOut, TimeUnit.SECONDS);
}
}
package com.zhirui.lmwy.project.norepeat;
import com.zhirui.core.model.ResultModel;
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.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;
import javax.servlet.http.HttpServletRequest;
import java.util.UUID;
/**
* @Author hhl
* @Date 2020/11/10 0010 18:41
* @Version 1.0
*/
@Aspect
@Component
public class AspectSubmit {
private static final Logger LOGGER = LoggerFactory.getLogger(AspectSubmit.class);
@Autowired
private RedisLock redisLock;
@Pointcut("@annotation(noRepeatSubmit)")
public void pointCut(NoRepeatSubmit noRepeatSubmit) {
}
@Around("pointCut(noRepeatSubmit)")
public Object around(ProceedingJoinPoint pjp, NoRepeatSubmit noRepeatSubmit) throws Throwable {
int lockSeconds = noRepeatSubmit.lockTime();
HttpServletRequest request = RequestUtils.getRequest();
Assert.notNull(request, "request can not null");
// 此处可以用token或者JSessionId
String token = request.getHeader("Authorization");
String path = request.getServletPath();
String key = getKey(token, path);
String clientId = getClientId();
boolean isSuccess = redisLock.tryLock(key, clientId, lockSeconds);
LOGGER.info("tryLock key = [{}], clientId = [{}]", key, clientId);
if (isSuccess) {
LOGGER.info("tryLock success, key = [{}], clientId = [{}]", key, clientId);
// 获取锁成功
Object result;
try {
// 执行进程
result = pjp.proceed();
} finally {
// 解锁
redisLock.releaseLock(key, clientId);
LOGGER.info("releaseLock success, key = [{}], clientId = [{}]", key, clientId);
}
return result;
} else {
// 获取锁失败,认为是重复提交的请求
LOGGER.info("tryLock fail, key = [{}]", key + "重复请求,请稍后再试");
return new ResultModel(true, "1", "重复请求,请稍后再试", null);
}
}
private String getKey(String token, String path) {
return token + path;
}
private String getClientId() {
return UUID.randomUUID().toString();
}
}
@RestController
@RequestMapping("/test")
@Slf4j
@Api(tags = "重复提交测试-api")
public class TestController {
@Autowired
private ProjectBuildService projectBuildService;
@GetMapping("/norepeat")
@NoRepeatSubmit(lockTime = 20000)
public ResultModel norepeat() {
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return new ResultModel(true, ResultModel.CODE_SUCCESS, "查询成功");
}
}
总结:三者可取其一,也可以1和3结合(推荐)