Spring Boot中实现接口的防抖效果(禁止用户连续访问接口)

在Spring Boot中,可以使用一些库或框架来实现接口的防抖效果。以下是一种常见的实现方式:

  1. 使用Spring的AOP(面向切面编程)机制:通过定义一个自定义的注解,结合AOP,实现对接口方法的防抖控制。

首先,你可以定义一个@Debounce注解,用于标记需要进行防抖处理的接口方法。例如:

package com.example.demo.utils;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Debounce {
    long value() default 1000; // 设置默认的防抖时间间隔,单位为毫秒
}

接下来,在你的AOP处理类中,使用@Around注解创建一个环绕通知,对使用了@Debounce注解的方法进行防抖处理。例如:

package com.example.demo.aspect;

import com.example.demo.utils.Debounce;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.reflect.MethodSignature;

import java.lang.reflect.Method;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * DebounceAop
 * 防抖
 */
public class DebounceAop {
    private final ConcurrentMap<Method, Lock> lockMap = new ConcurrentHashMap<>();

    @Around("@annotation(debounce)")
    public Object debounce(ProceedingJoinPoint pjp, Debounce debounce) throws Throwable {
        MethodSignature signature = (MethodSignature) pjp.getSignature();
        Method method = signature.getMethod();
        Lock lock = lockMap.computeIfAbsent(method, k -> new ReentrantLock());
        try {
            if (lock.tryLock(debounce.value(), TimeUnit.MILLISECONDS)) {
                return pjp.proceed();
            } else {
                // 防抖时间内的请求不执行方法,可以根据需要进行处理
                return null;
            }
        } finally {
            lock.unlock();
        }
    }
}

在上述实现中,我们使用ConcurrentHashMap来维护不同的方法对应的锁,防止并发访问冲突。在防抖逻辑中,使用tryLock()方法尝试获取锁,如果成功获取到锁则执行目标方法,否则阻止方法执行。

最后,在需要进行防抖处理的接口方法上,使用@Debounce注解标记,并可以传入自定义的防抖时间间隔。例如:

    @CrossOrigin
    @PostMapping("/test")
    @Debounce(1000) // 设置防抖时间间隔为1秒
    public void test(@RequestBody LoginPO po) {
        System.out.println("我进来了");
    }

这样,被@Debounce注解标记的接口方法在设定的防抖时间间隔内只会执行一次,后续的请求会被防抖处理。

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Spring Boot,可以通过自定义注解和AOP实现接口限流。具体实现步骤如下: 1. 定义注解 自定义一个注解,用于标记需要进行限流的方法。例如: ``` @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) public @interface RateLimit { int value() default 10; // 默认每秒最多处理10个请求 } ``` 2. 实现拦截器 定义一个切面拦截器,拦截被RateLimit注解标记的方法,并根据注解定义的限流参数进行限流。例如: ``` @Aspect @Component public class RateLimitInterceptor { private final Map<String, Integer> counterMap = new ConcurrentHashMap<>(); @Pointcut("@annotation(com.example.demo.annotation.RateLimit)") public void rateLimit() {} @Around("rateLimit()") public Object around(ProceedingJoinPoint joinPoint) throws Throwable { MethodSignature signature = (MethodSignature) joinPoint.getSignature(); Method method = signature.getMethod(); RateLimit rateLimit = method.getAnnotation(RateLimit.class); String methodName = method.getName(); Integer counter = counterMap.get(methodName); if (counter == null) { counter = 0; } if (counter >= rateLimit.value()) { throw new RuntimeException("接口访问频率过高"); } counterMap.put(methodName, counter + 1); try { return joinPoint.proceed(); } finally { counterMap.put(methodName, counter); } } } ``` 3. 配置拦截器 将切面拦截器注册到Spring容器,并配置AOP自动代理。 ``` @Configuration @EnableAspectJAutoProxy public class RateLimitConfig { @Bean public RateLimitInterceptor rateLimitInterceptor() { return new RateLimitInterceptor(); } } ``` 4. 使用注解 在需要进行限流的方法上添加RateLimit注解,并指定限流参数。例如: ``` @RestController public class UserController { @RateLimit(5) @GetMapping("/users") public List<User> getUsers() { // ... } } ``` 以上就是在Spring Boot自己实现接口限流的基本步骤。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值