赞
踩
import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /** * 避免重复提交 * @ClassName: AvoidRepeatableCommit */ @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) public @interface AvoidRepeatableCommit { /** * 指定时间内不可重复提交,单位毫秒 */ long timeout() default 3000 ; }
import com.ljmes.vo.ResultUtil; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.reflect.MethodSignature; import org.springframework.beans.factory.annotation.Autowired; 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.servlet.http.HttpServletRequest; import java.lang.reflect.Method; import java.util.UUID; import java.util.concurrent.TimeUnit; /** * 重复提交aop **/ @Aspect @Component public class AvoidRepeatableCommitAspect { @Autowired private RedisTemplate redisTemplate; /** * @param point */ @Around("@annotation(com.ljmes.util.AvoidRepeatableCommit)") public Object around(ProceedingJoinPoint point) throws Throwable { HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest(); // 获取访问的url StringBuffer requestURL = request.getRequestURL(); System.out.println("requestURL==" + requestURL); //获取注解 MethodSignature signature = (MethodSignature) point.getSignature(); Method method = signature.getMethod(); //参数 StringBuilder params = new StringBuilder(); for (Object arg : point.getArgs()) { params.append(arg); } System.out.println("params = " + params); //使用访问的url加上params作为key String key = requestURL+params.toString(); System.out.println("key==" + key); //获取自定义注解 AvoidRepeatableCommit avoidRepeatableCommit = method.getAnnotation(AvoidRepeatableCommit.class); long timeout = avoidRepeatableCommit.timeout(); if (timeout < 0) { timeout = 3; } //获取redis中的值 Object obj = redisTemplate.opsForValue().get(key); // 不为null说明在timeout时间内重复提交 if (obj != null) { return ResultUtil.error("重复提交!"); } redisTemplate.opsForValue().set(key, UUID.randomUUID().toString(), timeout, TimeUnit.MILLISECONDS); //执行方法 Object object = point.proceed(); System.out.println("object==" + object); return object; } }
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.example</groupId> <artifactId>demo</artifactId> <version>1.0-SNAPSHOT</version> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>2.2.4.RELEASE</version> </dependency> <!--swagger--> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-boot-starter</artifactId> <version>3.0.0</version> </dependency> <!--aop--> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.9.5</version> </dependency> <!--redis--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> <version>2.2.4.RELEASE</version> </dependency> <!--Lombok--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.4</version> </dependency> </dependencies> </project>
import com.ljmes.util.AvoidRepeatableCommit; import com.ljmes.vo.ResultUtil; import com.ljmes.vo.ResultVO; import com.ljmes.vo.UserDTO; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.springframework.web.bind.annotation.*; @Api("demo") @RestController @RequestMapping("/demo") public class DemoController { @ApiOperation("防止重复提交demo") @PostMapping("/demo") @AvoidRepeatableCommit public ResultVO demo(@RequestBody UserDTO demo, @RequestParam Integer userId) { return ResultUtil.success(demo); } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。