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(“重复提交”);

}

}

}

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