java aop被重复调用_Springboot 使用AOP实现防止接口重复提交

一、简述

在传统的web项目中,防止重复提交,通常做法是:后端生成一个唯一的提交令牌(uuid),并存储在服务端。页面提交请求携带这个提交令牌,后端验证并在第一次验证后删除该令牌,保证提交请求的唯一性。

思路没有问题,但是需要前后端都稍加改动,如果在业务开发完再加这个的话,改动量未免有些大了。无需前端配合,纯后端处理,是最清爽的。设计思路如下:

自定义注解@RreventReSubmit标记所有Controller中的提交请求。通过AOP 对所有标记@RreventReSubmit的方法拦截。在业务方法执行前,获取当前用户的 token(或者JSessionId)+ 当前请求地址,作为一个唯一 KEY,去获取 Redis 分布式锁(如果此时并发获取,只有一个线程会成功获取锁)。当有请求调用接口时,到redis中查找相应的key,如果能找到,则说明重复提交,如果找不到,则执行操作。业务方法执行后,释放锁。

二、导入aop依赖

org.springframework.boot

spring-boot-starter-aop

三、自定义注解

@Target(ElementType.METHOD)

@Retention(RetentionPolicy.RUNTIME)

@Documented

public @interface RreventReSubmit {

}

四、定义切面类

切面类需要使用@Aspect和@Component这两个注解做标注。

@Aspect

@Component

@Slf4j

public class UserAspect {

@Resource

private RedisUtil redisUtil;

@Value("${user.session.key}")

private String userSessionKey;

@Pointcut(value = "@annotation(com.xxp.annotation.RreventReSubmit )")

public void annotationPointCut() {

}

@Around("annotationPointCut()")

public Object NoReSubmit(ProceedingJoinPoint joinPoint) {

ServletRequestAttributes attributes =

(ServletRequestAttributes) RequestContextHolder.getRequestAttributes();

//获取request

HttpServletRequest request = attributes.getRequest();

HttpSession session = request.getSession();

//从session中获取登录的user对象,如果为null,则要求重新登录

Object sessionUser = session.getAttribute(userSessionKey);

if (sessionUser == null) {

return Response.FAIL("页面超时,请重新登录");

}

User user = (User) sessionUser;

Integer userId = user.getId();

//获取接口的请求参数,如果时Article类型,则保存为Article对象,使用Article对象里的title属性

Object[] args = joinPoint.getArgs();

Article article = null;

for (Object object : args) {

if (object instanceof Article) {

article = (Article) object;

}

}

if (args == null) {

return Response.FAIL("请求参数错误");

}

//组装redis key 从redis中获取对应的值

String key = userId + "_" + article.getTitle();

Object flag = redisUtil.getStr(key);

//如果redis中不存在对应的值,则执行原有的代码逻辑(插入文章操作)

if (flag == null) {

//redis设置key,value值为1

redisUtil.setStr(key, "1");

//设置有效期为5分钟

redisUtil.strSetExpireSeconds(key, 5 * 60L);

try {

return joinPoint.proceed();

} catch (Throwable throwable) {

redisUtil.delStr(key);

return Response.FAIL("系统错误,请联系管理员!");

}

} else {

//如果redis中存在对应的值,则证明重复提交,返回对应的信息

log.info("{}:重复提交", key);

return Response.FAIL("重复提交");

}

}

}

在想要防止重复提交的接口上添加注解即可使用。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现接口调用频率限制可以使用AOP和ConcurrentHashMap结合的方式。 首先,在Spring Boot中,我们可以使用AOP来拦截接口调用。我们可以定义一个切面,使用@Aspect注解标注,然后在切入点方法中定义需要拦截的注解。 例如,我们可以定义一个@FrequencyLimit注解,用于标注需要限制调用频率的方法: ```java @Retention(RetentionPolicy.RUNTIME) @Target({ ElementType.METHOD }) public @interface FrequencyLimit { // 限制时间段,单位为秒 int interval() default 60; // 时间段内最大请求次数 int maxCount() default 10; } ``` 然后,在切面中,我们可以拦截该注解标注的方法,并且进行限制调用频率的操作。可以使用ConcurrentHashMap来存储每个接口调用次数和最后一次调用时间。 ```java @Component @Aspect public class FrequencyLimitAspect { private ConcurrentHashMap<String, Long> lastRequestTimeMap = new ConcurrentHashMap<>(); private ConcurrentHashMap<String, Integer> requestCountMap = new ConcurrentHashMap<>(); @Around("@annotation(frequencyLimit)") public Object frequencyLimit(ProceedingJoinPoint joinPoint, FrequencyLimit frequencyLimit) throws Throwable { Object result = null; String methodName = joinPoint.getSignature().toLongString(); long currentTime = System.currentTimeMillis(); int interval = frequencyLimit.interval(); int maxCount = frequencyLimit.maxCount(); synchronized (this) { // 获取最后一次请求时间和请求次数 Long lastRequestTime = lastRequestTimeMap.get(methodName); Integer requestCount = requestCountMap.get(methodName); if (lastRequestTime == null || currentTime - lastRequestTime >= interval * 1000) { // 如果该接口在限制时间段内没有被调用过,则重置请求次数和最后一次请求时间 lastRequestTimeMap.put(methodName, currentTime); requestCountMap.put(methodName, 1); } else { // 如果该接口在限制时间段内已经被调用过,则增加请求次数 requestCount++; if (requestCount > maxCount) { // 如果请求次数超过了限制,则抛出异常 throw new RuntimeException("Exceeded maximum request limit"); } lastRequestTimeMap.put(methodName, currentTime); requestCountMap.put(methodName, requestCount); } } // 调用原始方法 result = joinPoint.proceed(); return result; } } ``` 在切面中,我们使用synchronized关键字来保证线程安全,因为ConcurrentHashMap并不能完全保证线程安全。同时,我们使用了@Around注解来拦截被@FrequencyLimit注解标注的方法,然后在方法中实现限制调用频率的逻辑。 这样,我们就可以实现接口调用频率限制了。在需要限制调用频率的方法中,我们只需要加上@FrequencyLimit注解即可。例如: ```java @GetMapping("/test") @FrequencyLimit(interval = 60, maxCount = 10) public String test() { return "test"; } ``` 这样,每个IP地址每分钟内最多只能调用该方法10次,超过次数会抛出异常。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值