首页 > 其他分享 >通过aop 注解的方式防止表单重复提交

通过aop 注解的方式防止表单重复提交

时间:2023-07-05 15:22:18浏览次数:37  
标签:lang String org aop springframework 表单 import 注解 annotation

pom.xml

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

annotation
import java.lang.annotation.*;
@Inherited
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface PreventRepeatSubmit {

public int interval() default 40000;

public String message() default "不允许重复提交,请稍后再试";
}

aop
import com.alibaba.fastjson.JSON;
import com.zan.rwj.common.exception.BusinessException;
import com.zan.rwj.common.result.ResponseEnum;
import com.zan.rwj.system.annotation.PreventRepeatSubmit;
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.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.concurrent.TimeUnit;

@Component
@Aspect
public class PreventRepeatSubmitAspect {

@Value("{token.header}")
private String header;

@Resource
private RedisTemplate redisTemplate;

@Pointcut("@annotation(com.zan.rwj.system.annotation.PreventRepeatSubmit)")
public void preventRepeatSubmit(){

}
@Around("preventRepeatSubmit()")
public Object checkPrs(ProceedingJoinPoint pjp) throws Throwable {
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
String requestURI = request.getRequestURI();
Object[] args = pjp.getArgs();
String argStr = JSON.toJSONString(args);
argStr = argStr.replace(":","#");
// String submitKey = request.getHeader(header).trim();
// String cacheRepeatKey = "repeat_submit:" +requestURI+":"+argStr+":"+submitKey;
String cacheRepeatKey = "repeat_submit:" +requestURI+":"+argStr;
MethodSignature ms = (MethodSignature) pjp.getSignature();
Method method = ms.getMethod();
PreventRepeatSubmit preventRepeatSubmit = method.getAnnotation(PreventRepeatSubmit.class);
int interval = preventRepeatSubmit.interval();
Boolean aBoolean = redisTemplate.opsForValue().setIfAbsent(cacheRepeatKey, 1, preventRepeatSubmit.interval(), TimeUnit.SECONDS);
if(!aBoolean){
//JSON.toJSONString(ResponseResult.errorResult(HttpCodeEnum.SYSTEM_ERROR.getCode(),annotation.message())));
throw new BusinessException(ResponseEnum.ALIYUN_SMS_ERROR);
}
return pjp.proceed();

}
}

标签:lang,String,org,aop,springframework,表单,import,注解,annotation
From: https://www.cnblogs.com/rwjnb/p/17528639.html

相关文章

  • Element-常用组件-表格-表单-对话框-分页工具条
     <!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><title>Title</title><!--格式--><style>.el-table.warning-row{background:oldlace;......
  • Element select表单必填验证
    特别注意:如第一段代码这里的区别是prop和v-model绑定的值不一样,这样的话是不行的,他们两个的值必须一样!!!!!而且还有一种情况,就是roleStatus必须要放在form里面,而且还必须是一个数组!!!!错误写法prop和v-model不一致<el-formlabel-width="300px":rules="rules":model="changeSourceDa......
  • 条件注解之@ConditionalOnProperty注解:通过配置文件的配置来控制配置类是否加入spring
    一、条件注解分类常见的@ConditionalOnxxx开头的注解我们称之为条件注解,常见的条件注解有class条件注解:@ConditionalOnClassbean条件注解:@ConditionalOnBean属性条件注解:@ConditionalOnProperty…@ConditionalOnProperty:如果有指定的配置,条件生效;@ConditionalOnBean:如果......
  • form表单验证
    1、典型表单 <el-formref="form":model="form"label-width="80px"><el-form-itemlabel="活动名称"><el-inputv-model="form.name"></el-input></el-form-item><el-form-ite......
  • springboot封装redission的分布式锁逻辑为注解
    场景概述使用分布式锁的时候,每次都需要使用trycatch处理方法中的逻辑。考虑是否可以这块逻辑抽离出来。实现在自定义的注解中添加属性来设置锁的等待时间、租赁时间和时间单位importjava.lang.annotation.*;@Target(ElementType.METHOD)@Retention(RetentionPolicy.RUNTI......
  • swagger常用的几个注解
    类上@Api()tags:说明该类的作用,参数是个数组,可以填多个。value="该参数没什么意义,在UI界面上不显示,所以不用配置"description="用户基本信息操作"方法上@ApiOperation()value="方法的用途和作用"notes="方法的注意事项和备注"tags:说明该方法的作用,参数是个数组......
  • 解放表单填写!睿鸿动态表单系统2.0助您高效迈进
    随着信息化进程的不断深入和电子化技术的日益成熟,在公共服务领域,越来越多的政府部门开始把传统纸质化的流程转变为数字化的流程。在这个过程中,表单作为重要的信息采集和处理工具,也逐渐由传统的纸质表单向电子表单转换。想要高效表单填写?试试睿鸿动态表单系统2.0相比于1.0版本,睿鸿动......
  • Java 常用注解@Configuration,@Bean及@ConfigurationProperties(prefix = "spring.data
    @ConfigurationpublicclassEventDataSourceConfig{@Bean(name="eventdataSource")@ConfigurationProperties(prefix="datasource.event")publicDataSourceoldDataSource(){returnDataSourceBuilder.create().build();......
  • 32. Spring Boot使用@SpringBootApplication注解【从零开始学Spring Boot】
     如果看了我之前的文章,这个节你就可以忽略了,这个是针对一些刚入门的选手存在的困惑进行写的一篇文章。很多SpringBoot开发者总是使用@Configuration,@EnableAutoConfiguration和@ComponentScan注解他们的main类。由于这些注解被如此频繁地一块使用(特别是你遵循以上最佳实践时),S......
  • (六)Spring源码解析:Spring AOP源码解析
    〇、AOP概念Aspect:切面给业务方法增加到功能,切面泛指交叉业务逻辑。上例中的事务处理、日志处理就可以理解为切面。常用的切面是通知(Advice)。实际就是对主业务逻辑的一种增强。Pointcut:切入点切入点指声明的一个或多个连接点的集合,通过切入点指定一组方法。被标记为final......