防止表单重复提交(springboot,redis)
我们在web项目中经常需要在后台对用户提交的表单进行校验防止重复提交。下面通过springboot的aop、redis来解决表单重复提交的问题。
通过在controller加上CheckSubmitForm注解 ,用户访问连接时,aop进行代理拦截
@PostMapping("/comment/add")
@CheckSubmitForm(delaySeconds = 6)
public MallTradeResult comment(@RequestBody SiteUserCommentDTO siteUserCommentDTO,@User UserDTO userDTO) {
siteUserCommentDTO.setUserId(userDTO.getUicId());
siteUserCommentDTO.setPhone(userDTO.getPhoneNumber());
siteUserCommentDTO.setNickName(userDTO.getUserName());
siteUserCommentDTO.setUserHeadImg(userDTO.getAvatarUrl());
return siteCommentFacade.add(siteUserCommentDTO);
}
requesetDTO中加入formId字段,fromId可以是前端提交的fromId,也可以是我们自己业务定义的一个关键字。fromId用做redis 的key
@Data
public class SiteUserCommentDTO extends RequesetDTO{
private String itemCode ;
private String formId;
public String getFormId() {
return itemCode ;
}
}
CheckSubmitFrom 注解是拦截哪些请求对用户提交的表单进行校验
作者项目中是使用 methodname:userId:formId来作为redis key。例如下图实例key为comment:userId:itemCode 表示用户userId对某件商品itemCode的评价comment。如果key存在则阻止提交。也可以由前端传递formId直接作为key。key业务范围尽量要小,太大可能对用户的其他表单提交有影响。
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface CheckSubmitForm {
int delaySeconds() default 5;
}
ReSubmitAspect 定义aop切面
/**
* Description:
*
* @author lixj on 2019/10/11.
*/
@Slf4j
@Component
@Aspect
public class ReSubmitAspect {
@Autowired
private StringRedisTemplate stringRedisTemplate;
private String keyId = "formId";
@Around("@annotation(com.fcbox.mall.web.support.CheckSubmitForm)")
public Object around(ProceedingJoinPoint joinPoint) throws Throwable{
log.info("resubmitApsec do around");
MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
CheckSubmitForm checkSubmitForm = methodSignature.getMethod().getAnnotation(CheckSubmitForm.class);
if (checkSubmitForm == null) {
return joinPoint.proceed();
} else {
Object[] args = joinPoint.getArgs();
String formId = null;
String userId = null;
for (Object arg : args) {
if (StringUtils.isEmpty(formId)) {
JSONObject jsonObject = JSONObject.parseObject(JSONObject.toJSONString(arg));
if (!StringUtils.isEmpty(jsonObject.getString(keyId))) {
formId = jsonObject.getString(keyId);
}
}
if (StringUtils.isEmpty(userId)) {
if (arg.getClass().getAnnotation(User.class) != null) {
UserDTO userDTO = (UserDTO)arg;
userId = userDTO.getUicId().toString();
}
}
}
if (!StringUtils.isEmpty(formId) && !StringUtils.isEmpty(userId)) {
Class<?> returnType = ((MethodSignature) joinPoint.getSignature()).getMethod().getReturnType();
String redisKey = ((MethodSignature) joinPoint.getSignature()).getMethod().getName().concat(":").concat(userId)
.concat(":").concat(formId);
log.info("resubmit {}", redisKey, checkSubmitForm.delaySeconds());
boolean submitAble = stringRedisTemplate.opsForValue().setIfAbsent(redisKey, "1");
if (!submitAble) {
long ttl = stringRedisTemplate.getExpire(redisKey);
if (ttl > 0) {
return MallTradeResult.fail(ResultCode.REPEAT_SUBMIT_ERROR.getCode(), ResultCode.REPEAT_SUBMIT_ERROR.getMsg());
}
}
}
stringRedisTemplate.expire(redisKey, checkSubmitForm.delaySeconds(), TimeUnit.SECONDS);
return joinPoint.proceed();
} else {
log.error("重复表单提交检验 失效: 参数错误:fromId-{},uicId-{}",formId,userId);
}
}
return joinPoint.proceed();
}
}
注意:代码中stringRedisTemplate的setNx 和 expire 不是原子操作。可以使用lua脚本实现或者Jedis开源组件来实现原子操作。
低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
2020年六大物联网安全趋势
踏入新十年,物联网安全领域逐渐成熟,但也伴随着新一波的风险。 就在不久之前,“物联网安全” 这个术语听起来还有点自相矛盾。但现在,对物联网安全重要性的认知已经达到了空前的高度。联网设备如今已渗透到我们生活的方方面面,从家居到工厂无处不在,恶意黑客如今手握大把形形色色的终端目标。现在我们就来预测一下,来年网络安全领域的猫鼠游戏中会出现些什么。 1. 智能楼宇安全问题引人关注 2020 年,智能楼宇安全问题可能会成为设施管理者的首要考虑。Gartner 的调查研究表明,2020 年,80% 的联网设备与楼宇相关,智能楼宇为对手提供了新的攻击途径。但在明年此类攻击会不会大幅增加的问题上,专家们意见不一。Honeywell Building Solutions 网络安全全球总监 Mirel Sehic 预期会出现大幅增长。攻击者可能将楼宇管理系统作为跳板,用以获取 IT 数据,以及操纵建筑控制。 Kudelski Security 首席执行官 Andrew Howard 表示:我不认为明年会看到更多此类攻击,因为很多建筑内部的网络都是高度隔离的。 尽管可能会有一两个系统接入互联网,但事实上大部...
- 下一篇
什么鬼,面试官竟然让我用Redis实现一个消息队列?
众所周知,redis是一个高性能的key-value数据库,在NoSQL数据库市场上,redis自己就占据了将近半壁江山,足以见到其强大之处。同时,由于redis的单线程特性,我们可以将其用作为一个消息队列。本篇文章就来讲讲如何将redis整合到spring boot中,并用作消息队列的……一、什么是消息队列“消息队列”是在消息的传输过程中保存消息的容器。——《百度百科》消息我们可以理解为在计算机中或在整个计算机网络中传递的数据。队列是我们在学习数据结构的时候学习的基本数据结构之一,它具有先进先出的特性。所以,消息队列就是一个保存消息的容器,它具有先进先出的特性。为什么会出现消息队列? 1、异步:常见的B/S架构下,客户端向服务器发送请求,但是服务器处理这个消息需要花费的时间很长的时间,如果客户端一直等待服务器处理完消息,会造成客户端的系统资源浪费;而使用消息队列后,服务器直接将消息推送到消息队列中,由专门的处理消息程序处理消息,这样客户端就不必花费大量时间等待服务器的响应了; 2、解耦:传统的软件开发模式,模块之间的调用是直接调用,这样的系统很不利于系统的扩展,同时,模块之间的相互调...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- Docker使用Oracle官方镜像安装(12C,18C,19C)
- CentOS7,CentOS8安装Elasticsearch6.8.6
- Jdk安装(Linux,MacOS,Windows),包含三大操作系统的最全安装
- Linux系统CentOS6、CentOS7手动修改IP地址
- CentOS7安装Docker,走上虚拟化容器引擎之路
- CentOS7编译安装Cmake3.16.3,解决mysql等软件编译问题
- SpringBoot2全家桶,快速入门学习开发网站教程
- CentOS7设置SWAP分区,小内存服务器的救世主
- Docker快速安装Oracle11G,搭建oracle11g学习环境
- CentOS7编译安装Gcc9.2.0,解决mysql等软件编译问题