博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java后端限制频繁请求、重复提交
阅读量:2385 次
发布时间:2019-05-10

本文共 2595 字,大约阅读时间需要 8 分钟。

当前端按连续请求多次,请求过于频繁或者是多次重复提交数据,对系统或者是数据造成了一点的损害。

为了解决这个问题,下面介绍了一种简易的解决方法:

步骤一、写限制注解

import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;/** * @Author Minco * @Date 16:04 2020-08-13 * @Description 不重复提交注解 */@Target(ElementType.METHOD) // 作用到方法上@Retention(RetentionPolicy.RUNTIME) // 运行时有效public @interface NoRepeatSubmit {    String name() default "name:";}

步骤二、解析注解

import javax.servlet.http.HttpServletRequest;import com.hieasy.comm.core.domain.R;import com.hieasy.comm.redis.service.RedisService;import com.hieasy.system.util.TermUtil;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.aspectj.lang.ProceedingJoinPoint;import org.aspectj.lang.annotation.Around;import org.aspectj.lang.annotation.Aspect;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Component;import org.springframework.web.context.request.RequestContextHolder;import org.springframework.web.context.request.ServletRequestAttributes;import java.util.concurrent.TimeUnit;/** * @Author Minco * @Date 16:02 2020-08-13 * @Description aop解析注解 */@Aspect@Componentpublic class NoRepeatSubmitAop {     private Log logger = LogFactory.getLog(getClass());     @Autowired    private RedisService redisService;    @Around("execution(* com.hieasy.*.controller.*.*Ctrl.*(..)) && @annotation(nrs)")    public Object arround(ProceedingJoinPoint pjp, NoRepeatSubmit nrs) throws Throwable {                   ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();            HttpServletRequest request = attributes.getRequest();            String key = TermUtil.getUserId() + "-" + request.getServletPath();            if ( !redisService.haskey(key) ) {// 如果缓存中有这个url视为重复提交                Object o = pjp.proceed();                redisService.setCacheObject(key, 0, 15, TimeUnit.SECONDS);                return o;            } else {                redisService.setCacheObject(key, 0, 15, TimeUnit.SECONDS);//点了同样的URL继续限制,直到2次点击中间间隔超过了限制                return R.error("请勿重复提交或者操作过于频繁!");            }        } }

步骤三、控制层注解使用

@NoRepeatSubmit    @PostMapping("insert")    @OperLog(title = "仓库调整", businessType = BusinessType.INSERT)    @ApiOperation(value = "新增仓库调整单", notes = "")    public R insert(@RequestBody Cktzd cktzd) {        System.out.println(JSON.toJSONString(cktzd));        return R.error("测试阶段!");    }

 测试结果:

第一次提交

在不间隔15就继续点

不间断的点,只要2次点击之间没有超过15秒,都会抛出请勿重复请求。直到2次点击之间间隔足够才能正常请求! 

转载地址:http://mjnab.baihongyu.com/

你可能感兴趣的文章
关于Docker目录挂载的总结
查看>>
docker 主机 容器通信
查看>>
wkhtmltopdf 中文参数详解
查看>>
odoo 中 wkhtmltopdf 页码的读取(js)
查看>>
用 strcoll 实现中文按拼音排序
查看>>
linux adduser-s /sbin/nologin和/bin/false的区别
查看>>
linux 修改用户的shell
查看>>
浅析ATO,MTO和ETO
查看>>
Python多进程不要使用TimedRotatingFileHandler
查看>>
CSS语法和CSS优先级
查看>>
undercore.js 的几个方法
查看>>
python 类和元类(metaclass)的理解和简单运用
查看>>
git commit id相关
查看>>
odoo 慎用related(计算字段)
查看>>
psql pg_dump pg_restore等命令
查看>>
sql重复数据处理
查看>>
Python str(float) 科学计数法
查看>>
git 命令整理
查看>>
Python__slots__详解
查看>>
docker-service endpoint with name xxx already exists.
查看>>