@Target({ ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface RequestLimit {
/**
* 规定时间内(单位:秒):默认两秒
*
*/
long lockTime() default 2L;
/**
* 允许访问的最大次数(默认1次)
*/
int limitCount() default 1;
}
@Aspect
@Component
public class RequestLimitAspect {
@Around("@annotation(requestLimit)")
@SneakyThrows
public Object doBefore(ProceedingJoinPoint point, RequestLimit requestLimit) {
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
String requestURI = request.getRequestURI();
//客户端IP
String clientIP = ServletUtil.getClientIP(request);
//客户端请求头的userAgent
String header = request.getHeader("User-Agent");
//MD5加密后的值
String Key = SecureUtil.md5(clientIP + requestURI + header);
Object o = RedisUtil.get(Key);
//第一次提交
if (o == null) {
RedisUtil.set(Key, 1, requestLimit.lockTime());
} else {
if (requestLimit.limitCount() == 1) {
return ResponseUtil.fail(-1, "不允许重复提交");
}
Integer redisCount = (Integer) o;
System.out.println("redisCount = " + redisCount);
if (redisCount > requestLimit.limitCount()) {
return ResponseUtil.fail(-1, "操作频繁,稍后再试");
}
RedisUtil.set(Key, redisCount + 1, requestLimit.lockTime());
}
return point.proceed();
}
}
@RequestLimit
@RequestLimit(lockTime = 30 * 60L, limitCount = 10)
内容出处:,
声明:本网站所收集的部分公开资料来源于互联网,转载的目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。如果您发现网站上有侵犯您的知识产权的作品,请与我们取得联系,我们会及时修改或删除。文章链接:http://www.yixao.com/procedure/29677.html