springboot项目注解限制接口访问次数
2022-11-29 本文已影响0人
lowkey港
1.导包
<dependency>
<groupId>net.jodah</groupId>
<artifactId>expiringmap</artifactId>
<version>0.5.10</version>
</dependency>
2.定义注解
package com.kc.framework.aspectj.lang.annotation;
import java.lang.annotation.*;
/**
* @ClassName LimitRequest 定义一个切面,执行方法前去ExpiringMap查询该IP在规定时间内请求了多少次,如超过次数则直接返回请求失败。
* @Author HuGang
* @e-mail 1042258937@qq.com
* @Date 15:33 2022/11/29
**/
@Documented
@Target(ElementType.METHOD) // 说明该注解只能放在方法上面
@Retention(RetentionPolicy.RUNTIME)
public @interface LimitRequest {
long time() default 6000; // 限制时间 单位:毫秒
int count() default 600; // 允许请求的次数
}
3.自定义aop切面
package com.kc.framework.aspectj;
import com.kc.framework.aspectj.lang.annotation.LimitRequest;
import com.kc.framework.web.domain.AjaxResult;
import net.jodah.expiringmap.ExpirationPolicy;
import net.jodah.expiringmap.ExpiringMap;
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.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
/**
* @Description: LimitRequestAspect
* @Author: HuGang 1042258937@qq.com
* @CreateDate: 2022/11/29 15:38
*/
@Aspect
@Component
public class LimitRequestAspect {
private static ConcurrentHashMap<String, ExpiringMap<String, Integer>> book = new ConcurrentHashMap<>();
// 定义切点
// 让所有有@LimitRequest注解的方法都执行切面方法
@Pointcut("@annotation(limitRequest)")
public void excudeService(LimitRequest limitRequest) {
}
@Around("excudeService(limitRequest)")
public Object doAround(ProceedingJoinPoint pjp, LimitRequest limitRequest) throws Throwable {
// 获得request对象
RequestAttributes ra = RequestContextHolder.getRequestAttributes();
ServletRequestAttributes sra = (ServletRequestAttributes) ra;
HttpServletRequest request = sra.getRequest();
// 获取Map对象, 如果没有则返回默认值
// 第一个参数是key, 第二个参数是默认值
ExpiringMap<String, Integer> uc = book.getOrDefault(request.getRequestURI(), ExpiringMap.builder().variableExpiration().build());
Integer uCount = uc.getOrDefault(request.getRemoteAddr(), 0);
if (uCount >= limitRequest.count()) { // 超过次数,不执行目标方法
return "请求次数过多,已经限制,"+limitRequest.time()+"毫秒内超过"+uCount+"次!";
//return AjaxResult.error("请求次数过多,已经限制,"+limitRequest.time()+"毫秒内超过"+uCount+"次!");
} else if (uCount == 0) { // 第一次请求时,设置有效时间
uc.put(request.getRemoteAddr(), uCount + 1, ExpirationPolicy.CREATED, limitRequest.time(), TimeUnit.MILLISECONDS);
} else { // 未超过次数, 记录加一
uc.put(request.getRemoteAddr(), uCount + 1);
}
book.put(request.getRequestURI(), uc);
// result的值就是被拦截方法的返回值
Object result = pjp.proceed();
return result;
}
}
4.接口添加注解
@LimitRequest(count = 600,time = 6000)
5.测试接口
image.png