在客户端网络慢或者服务器响应慢时,用户有时是会频繁刷新页面或重复提交表单的,这样是会给服务器造成不小的负担的,同时在添加数据时有可能造成不必要的麻烦。所以我们在后端也有必要进行防抖操作。
1.自定义注解
@Target(ElementType.METHOD) // 作用到方法上
@Retention(RetentionPolicy.RUNTIME) // 运行时有效
public @interface NoRepeatSubmit {
//名称,如果不给就是要默认的
String name() default "name";
}
2.使用AOP实现该注解
@Aspect
@Component
@Slf4j
public class NoRepeatSubmitAop {
@Autowired
private RedisService redisService;
@Pointcut("@annotation(com.qwt.part_time_admin_api.common.validation.NoRepeatSubmit)")
public void pt() {
}
@Around("pt()")
public Object arround(ProceedingJoinPoint joinPoint) throws Throwable {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
assert attributes != null;
HttpServletRequest request = attributes.getRequest();
//这里是唯一标识 根据情况而定
String key = "1" + "-" + request.getServletPath();
// 如果缓存中有这个url视为重复提交
if (!redisService.haskey(key)) {
//通过,执行下一步
Object o = joinPoint.proceed();
//然后存入redis 并且设置15s倒计时
redisService.setCacheObject(key, 0, 15, TimeUnit.SECONDS);
//返回结果
return o;
} else {
return Result.fail(400, "请勿重复提交或者操作过于频繁!");
}
}
}
3.serice,也可以放在工具包里面,这里我们使用到了Redis来对key和标识码进行存储和倒计时,所以在使用时还需要连接一下Redis
package com.qwt.part_time_admin_api.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.*;
import org.springframework.stereotype.Component;
import java.util.*;
import java.util.concurrent.TimeUnit;
@Component
public class RedisService {
@Autowired
public RedisTemplate redisTemplate;
public <T> ValueOperations<String, T> setCacheObject(String key, T value) {
ValueOperations<String, T> operation = redisTemplate.opsForValue();
operation.set(key, value);
return operation;
}
public <T> ValueOperations<String, T> setCacheObject(String key, T value, Integer timeout, TimeUnit timeUnit) {
ValueOperations<String, T> operation = redisTemplate.opsForValue();
operation.set(key, value, timeout, timeUnit);
return operation;
}
public <T> T getCacheObject(String key) {
ValueOperations<String, T> operation = redisTemplate.opsForValue();
return operation.get(key);
}
public void deleteObject(String key) {
redisTemplate.delete(key);
}
public void deleteObject(Collection collection) {
redisTemplate.delete(collection);
}
public <T> ListOperations<String, T> setCacheList(String key, List<T> dataList) {
ListOperations listOperation = redisTemplate.opsForList();
if (null != dataList) {
int size = dataList.size();
for (int i = 0; i < size; i++) {
listOperation.leftPush(key, dataList.get(i));
}
}
return listOperation;
}
public <T> List<T> getCacheList(String key) {
List<T> dataList = new ArrayList<>();
ListOperations<String, T> listOperation = redisTemplate.opsForList();
Long size = listOperation.size(key);
for (int i = 0; i < size; i++) {
dataList.add(listOperation.index(key, i));
}
return dataList;
}
public <T> BoundSetOperations<String, T> setCacheSet(String key, 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(String key) {
Set<T> dataSet = new HashSet<>();
BoundSetOperations<String, T> operation = redisTemplate.boundSetOps(key);
dataSet = operation.members();
return dataSet;
}
public <T> HashOperations<String, String, T> setCacheMap(String key, Map<String, T> dataMap) {
HashOperations hashOperations = redisTemplate.opsForHash();
if (null != dataMap) {
for (Map.Entry<String, T> entry : dataMap.entrySet()) {
hashOperations.put(key, entry.getKey(), entry.getValue());
}
}
return hashOperations;
}
public <T> Map<String, T> getCacheMap(String key) {
Map<String, T> map = redisTemplate.opsForHash().entries(key);
return map;
}
public Collection<String> keys(String pattern) {
return redisTemplate.keys(pattern);
}
public boolean haskey(String key) {
return redisTemplate.hasKey(key);
}
public Long getExpire(String key) {
return redisTemplate.getExpire(key);
}
public <T> ValueOperations<String, T> setBillObject(String key, List<Map<String, Object>> value) {
ValueOperations<String, T> operation = redisTemplate.opsForValue();
operation.set(key, (T) value);
return operation;
}
public <T> ValueOperations<String, T> setBillObject(String key, List<Map<String, Object>> value, Integer timeout, TimeUnit timeUnit) {
ValueOperations<String, T> operation = redisTemplate.opsForValue();
operation.set(key, (T) value, timeout, timeUnit);
return operation;
}
public <T> HashOperations<String, String, T> setCKdBillMap(String key, Map<String, T> dataMap) {
HashOperations hashOperations = redisTemplate.opsForHash();
if (null != dataMap) {
for (Map.Entry<String, T> entry : dataMap.entrySet()) {
hashOperations.put(key, entry.getKey(), entry.getValue());
}
}
return hashOperations;
}
}
4.测试
@NoRepeatSubmit(name = "test") // 也可以不给名字,这样就会走默认名字
@GetMapping("test")
public Result test() {
return Result.success("测试阶段!");
}
15秒内重复点击就会给提示
这样就完成了一个防止重复提交、频繁申请的程序
参考:
https://blog.csdn.net/chengmin123456789/article/details/107982095
到此这篇关于Java后端防止频繁请求、重复提交的文章就介绍到这了,更多相关java重复提交内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!