随笔 - 42  文章 - 0 评论 - 1 阅读 - 18166
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

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();

}
}
posted on   我叫福禄娃  阅读(35)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
历史上的今天:
2022-07-05 java8 新特性
点击右上角即可分享
微信分享提示