aop实现接口访问频率限制

引言

项目开发中我们有时会用到一些第三方付费的接口,这些接口的每次调用都会产生一些费用,有时会有别有用心之人恶意调用我们的接口,造成经济损失;或者有时需要对一些执行时间比较长的的接口进行频率限制,这里我就简单演示一下我的解决思路;

主要使用spring的aop特性实现功能;

代码实现

首先需要一个注解,找个注解可以理解为一个坐标,标记该注解的接口都将进行访问频率限制;

package com.yang.prevent;
 
import java.lang.annotation.*;
 
/**
 * 接口防刷注解
 */
@Documented
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface Prevent {
 
    /**
     * 限制的时间值(秒)默认60s
     */
    long value() default 60;

    /**
     * 限制规定时间内访问次数,默认只能访问一次
     */
    long times() default 1;
 
    /**
     * 提示
     */
    String message() default "";
 
    /**
     * 策略
     */
    PreventStrategy strategy() default PreventStrategy.DEFAULT;
}

value就是限制周期,times是在一个周期内访问次数,message是访问频率过多时的提示信息,strategy就是一个限制策略,是自定义的,如下:

package com.yang.prevent;

/**
 * 防刷策略枚举
 */
public enum PreventStrategy {

    /**
     * 默认(60s内不允许再次请求)
     */
    DEFAULT
}

下面就是aop拦截的具体代码:

package com.yang.prevent;

import com.yang.common.StatusCode;
import com.yang.constant.redis.RedisKey;
import com.yang.exception.BusinessException;
import com.yang.utils.IpUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

/**
 * 防刷切面实现类
 */
@Aspect
@Component
public class PreventAop {

    @Resource
    private RedisTemplate<String, Long> redisTemplate;


    /**
     * 切入点
     */
    @Pointcut("@annotation(com.yang.prevent.Prevent)")
    public void pointcut() {}


    /**
     * 处理前
     */
    @Before("pointcut()")
    public void joinPoint(JoinPoint joinPoint) throws Exception {
        // 获取调用者ip
        RequestAttributes requestAttributes = RequestContextHolder.currentRequestAttributes();
        HttpServletRequest httpServletRequest = ((ServletRequestAttributes) requestAttributes).getRequest();
        String userIP = IpUtils.getUserIP(httpServletRequest);
        // 获取调用接口方法名
        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
        Method method = joinPoint.getTarget().getClass().getMethod(
                methodSignature.getName(),
                methodSignature.getParameterTypes()); // 获取该接口方法
        String methodFullName = method.getDeclaringClass().getName() + method.getName(); // 获取到方法名
        Prevent preventAnnotation = method.getAnnotation(Prevent.class); // 获取该接口上的prevent注解(为了使用该注解内的参数)
        // 执行对应策略
        entrance(preventAnnotation, userIP, methodFullName);
    }


    /**
     * 通过prevent注册判断执行策略
     * @param prevent 该接口的prevent注解对象
     * @param userIP 访问该接口的用户ip
     * @param methodFullName 该接口方法名
     */
    private void entrance(Prevent prevent, String userIP, String methodFullName) throws Exception {
        PreventStrategy strategy = prevent.strategy(); // 获取校验策略
        if (Objects.requireNonNull(strategy) == PreventStrategy.DEFAULT) { // 默认就是default策略,执行default策略方法
            defaultHandle(userIP, prevent, methodFullName);
        } else {
            throw new BusinessException(StatusCode.FORBIDDEN, "无效的策略");
        }
    }


    /**
     * Default测试执行方法
     * @param userIP 访问该接口的用户ip
     * @param prevent 该接口的prevent注解对象
     * @param methodFullName 该接口方法名
     */
    private void defaultHandle(String userIP, Prevent prevent, String methodFullName) throws Exception {
        String base64StrIP = toBase64String(userIP); // 加密用户ip(避免ip存在一些特殊字符作为redis的key不合法)
        long expire = prevent.value(); // 获取访问限制时间
        long times = prevent.times(); // 获取访问限制次数
        // 限制特定时间内访问特定次数
        long count = redisTemplate.opsForValue().increment(
                RedisKey.PREVENT_METHOD_NAME + base64StrIP + ":" + methodFullName, 1); // 访问次数+1
        if (count == 1) { // 如果访问次数为1,则重置访问限制时间(即redis超时时间)
            redisTemplate.expire(
                    RedisKey.PREVENT_METHOD_NAME + base64StrIP + ":" + methodFullName,
                    expire,
                    TimeUnit.SECONDS);
        }
        if (count > times) { // 如果访问次数超出访问限制次数,则禁止访问
            // 如果有限制信息则使用限制信息,没有则使用默认限制信息
            String errorMessage =
                    !StringUtils.isEmpty(prevent.message()) ? prevent.message() : expire + "秒内不允许重复请求";
            throw new BusinessException(StatusCode.FORBIDDEN, errorMessage);
        }
    }


    /**
     * 对象转换为base64字符串
     * @param obj 对象值
     * @return base64字符串
     */
    private String toBase64String(String obj) throws Exception {
        if (StringUtils.isEmpty(obj)) {
            return null;
        }
        Base64.Encoder encoder = Base64.getEncoder();
        byte[] bytes = obj.getBytes(StandardCharsets.UTF_8);
        return encoder.encodeToString(bytes);
    }
}

注释写的很清楚了,这里我简单说一下关键方法defaultHandle:

1,首先加密ip,原因就是避免ip存在一些特殊字符作为redis的key不合法,该ip是组成redis主键的一部分,redis主键格式为:polar:prevent:加密ip:方法名

这样就能区分不同ip,同一ip下区分不同方法的访问频率;

2,expire和times都是从@Prevent注解中获取的参数,默认是60s内最多访问1次,可以自定义;

3,然后接口访问次数+1(该设备ip下),如果该接口访问次数为1,则说明这是这个ip第一次访问该接口,或者是该接口的频率限制已经解除,即该接口访问次数+1前redis中没有该ip对应接口的限制记录,所以需要重新设置对应超时时间,表示新的一轮频率限制开始;如果访问次数超过最大次数,则禁止访问该接口,直到该轮频率限制结束,redis缓存的记录超时消失,才可以再次访问该接口;


这个方法理解了其他就不难了,其他方法就是给这个方法传参或者作为校验或数据处理的;

下面测试一下,分别标记两个接口,一个使用@Prevent默认参数,一个使用自定义参数:

image-20230212013539520

调用getToleById接口,意思是60s内只能调用该接口1次:

第一次调用成功,redis键值为1

image-20230212014446080

image-20230212014507651

第二次失败,需要等60s

image-20230212014328928

redis键值变成了2

image-20230212014419442


getRoleList是自定义参数,意思是20s内最多只能访问该接口5次:

未超出频率限制

image-20230212014612633

image-20230212014635266

超出频率限制

image-20230212014658229

image-20230212014707288


总体流程就是这样了,aop理解好了不难,也比较实用,可以在自己项目中使用;

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
实现接口调用频率限制可以使用AOP和ConcurrentHashMap结合的方式。 首先,在Spring Boot中,我们可以使用AOP来拦截接口的调用。我们可以定义一个切面,使用@Aspect注解标注,然后在切入点方法中定义需要拦截的注解。 例如,我们可以定义一个@FrequencyLimit注解,用于标注需要限制调用频率的方法: ```java @Retention(RetentionPolicy.RUNTIME) @Target({ ElementType.METHOD }) public @interface FrequencyLimit { // 限制时间段,单位为秒 int interval() default 60; // 时间段内最大请求次数 int maxCount() default 10; } ``` 然后,在切面中,我们可以拦截该注解标注的方法,并且进行限制调用频率的操作。可以使用ConcurrentHashMap来存储每个接口的调用次数和最后一次调用时间。 ```java @Component @Aspect public class FrequencyLimitAspect { private ConcurrentHashMap<String, Long> lastRequestTimeMap = new ConcurrentHashMap<>(); private ConcurrentHashMap<String, Integer> requestCountMap = new ConcurrentHashMap<>(); @Around("@annotation(frequencyLimit)") public Object frequencyLimit(ProceedingJoinPoint joinPoint, FrequencyLimit frequencyLimit) throws Throwable { Object result = null; String methodName = joinPoint.getSignature().toLongString(); long currentTime = System.currentTimeMillis(); int interval = frequencyLimit.interval(); int maxCount = frequencyLimit.maxCount(); synchronized (this) { // 获取最后一次请求时间和请求次数 Long lastRequestTime = lastRequestTimeMap.get(methodName); Integer requestCount = requestCountMap.get(methodName); if (lastRequestTime == null || currentTime - lastRequestTime >= interval * 1000) { // 如果该接口限制时间段内没有被调用过,则重置请求次数和最后一次请求时间 lastRequestTimeMap.put(methodName, currentTime); requestCountMap.put(methodName, 1); } else { // 如果该接口限制时间段内已经被调用过,则增加请求次数 requestCount++; if (requestCount > maxCount) { // 如果请求次数超过了限制,则抛出异常 throw new RuntimeException("Exceeded maximum request limit"); } lastRequestTimeMap.put(methodName, currentTime); requestCountMap.put(methodName, requestCount); } } // 调用原始方法 result = joinPoint.proceed(); return result; } } ``` 在切面中,我们使用synchronized关键字来保证线程安全,因为ConcurrentHashMap并不能完全保证线程安全。同时,我们使用了@Around注解来拦截被@FrequencyLimit注解标注的方法,然后在方法中实现限制调用频率的逻辑。 这样,我们就可以实现接口调用频率限制了。在需要限制调用频率的方法中,我们只需要加上@FrequencyLimit注解即可。例如: ```java @GetMapping("/test") @FrequencyLimit(interval = 60, maxCount = 10) public String test() { return "test"; } ``` 这样,每个IP地址每分钟内最多只能调用该方法10次,超过次数会抛出异常。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

YXXYX

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值