自定义注解:
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@Documented
@Order(Ordered.HIGHEST_PRECEDENCE)
public @interface RequestLimit {
/**
* 允许访问的次数,默认值20
*/
int count() default 20;
/**
* 时间段,单位为毫秒,默认值一分钟
*/
long time() default 60000;
}
Aop增强类:
@Component
@Aspect
@Slf4j
public class RequestLimitAop {
private Logger LOGGER = LoggerFactory.getLogger(getClass());
@Autowired
private RedisService redisService;
@Before("within(@org.springframework.stereotype.Controller *) && @annotation(limit)")
public void requestLimit(JoinPoint joinPoint, RequestLimit limit) throws Exception {
Object[] args = joinPoint.getArgs();
HttpServletRequest request = ((ServletRequestAttributes) Re