SpringBoot实现接口防刷的5种实现方案

SpringBoot限制接口访问次数 SpringBoot限制接口访问次数。 阅读详情

接口防刷是保障系统安全与稳定性的重要措施。恶意的高频请求不仅会消耗服务器资源,还可能导致数据异常,甚至系统瘫痪。本文将介绍在SpringBoot框架下实现接口防刷的5种技术方案。

1. 基于注解的访问频率限制

最常见的防刷方案是通过自定义注解和AOP切面实现访问频率限制。这种方法简单易用,实现成本低。

实现步骤

1.1 创建限流注解

@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface RateLimit {
    /**
     * 限制时间段,单位为秒
     */
    int time() default 60;
    
    /**
     * 在限制时间段内允许的最大请求次数
     */
    int count() default 10;
    
    /**
     * 限流的key,支持SpEL表达式
     */
    String key() default "";
    
    /**
     * 提示信息
     */
    String message() default "操作太频繁,请稍后再试";
}

1.2 实现限流切面

@Aspect
@Component
@Slf4j
public class RateLimitAspect {
    
    @Autowired
    private StringRedisTemplate redisTemplate;
    
    @Around("@annotation(rateLimit)")
    public Object around(ProceedingJoinPoint pjp, RateLimit rateLimit) throws Throwable {
        // 获取请求的方法名
        String methodName = pjp.getSignature().getName();
        // 获取请求的类名
        String className = pjp.getTarget().getClass().getName();
        
        // 组合限流key
        String limitKey = getLimitKey(pjp, rateLimit, methodName, className);
        
        // 获取限流参数
        int time = rateLimit.time();
        int count = rateLimit.count();
        
        // 执行限流逻辑
        boolean limited = isLimited(limitKey, time, count);
        if (limited) {
            throw new RuntimeException(rateLimit.message());
        }
        
        // 执行目标方法
        return pjp.proceed();
    }
    
    private String getLimitKey(ProceedingJoinPoint pjp, RateLimit rateLimit, String methodName, String className) {
        // 获取用户自定义的key
        String key = rateLimit.key();
        
        if (StringUtils.hasText(key)) {
            // 支持SpEL表达式解析
            StandardEvaluationContext context = new StandardEvaluationContext();
            MethodSignature signature = (MethodSignature) pjp.getSignature();
            String[] parameterNames = signature.getParameterNames();
            Object[] args = pjp.getArgs();
            
            for (int i = 0; i < parameterNames.length; i++) {
                context.setVariable(parameterNames[i], args[i]);
            }
            
            ExpressionParser parser = new SpelExpressionParser();
            Expression expression = parser.parseExpression(key);
            key = expression.getValue(context, String.class);
        } else {
            // 默认使用类名+方法名+IP地址作为key
            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            String ip = getIpAddress(request);
            key = ip + ":" + className + ":" + methodName;
        }
        
        return "rate_limit:" + key;
    }
    
    private boolean isLimited(String key, int time, int count) {
        // 使用Redis的计数器实现限流
        try {
            Long currentCount = redisTemplate.opsForValue().increment(key, 1);
            
            // 如果是第一次访问,设置过期时间
            if (currentCount == 1) {
                redisTemplate.expire(key, time, TimeUnit.SECONDS);
            }
            
            return currentCount > count;
        } catch (Exception e) {
            log.error("限流异常", e);
            return false;
        }
    }
    
    private String getIpAddress(HttpServletRequest request) {
        String ip = request.getHeader("X-Forwarded-For");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_CLIENT_IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }
}

1.3 使用示例

@RestController
@RequestMapping("/api")
public class UserController {
    
    @RateLimit(time = 60, count = 3, message = "请求太频繁,请稍后再试")
    @GetMapping("/user/{id}")
    public User getUser(@PathVariable Long id) {
        return userService.getUser(id);
    }
    
    // 使用SpEL表达式指定key
    @RateLimit(time = 60, count = 1, key = "#id + '_' + #request.remoteAddr")
    @PostMapping("/user/{id}/update")
    public Result updateUser(@PathVariable Long id, @RequestBody UserDTO userDTO, HttpServletRequest request) {
        return userService.updateUser(id, userDTO);
    }
}

优缺点分析

优点:

  • 实现简单,上手容易,单机情况下可以去掉Redis换成本地缓存实现
  • 注解式使用,对业务代码无侵入
  • 可以精确控制接口粒度
  • 支持灵活的限流策略配置

缺点:

  • 限流逻辑相对简单,无法应对复杂场景
  • 缺少预警机制

2. 令牌桶算法实现限流

令牌桶算法是一种更加灵活的限流算法,可以允许突发流量,同时又能限制长期的平均流量。

实现步骤

2.1 引入依赖

Google提供的Guava库中包含了令牌桶实现:

<dependency>
    <groupId>com.google.guava</groupId>
    <artifactId>guava</artifactId>
    <version>31.1-jre</version>
</dependency>

2.2 创建令牌桶限流器

@Component
public class RateLimiter {
    // 使用ConcurrentHashMap存储不同接口的令牌桶
    private final ConcurrentHashMap<String, com.google.common.util.concurrent.RateLimiter> rateLimiterMap = new ConcurrentHashMap<>();
    
    /**
     * 获取特定接口的令牌桶,不存在则创建
     * @param key 限流键
     * @param permitsPerSecond 每秒允许的请求量
     * @return 令牌桶实例
     */
    public com.google.common.util.concurrent.RateLimiter getRateLimiter(String key, double permitsPerSecond) {
        return rateLimiterMap.computeIfAbsent(key, 
            k -> com.google.common.util.concurrent.RateLimiter.create(permitsPerSecond));
    }
    
    /**
     * 尝试获取令牌
     * @param key 限流键
     * @param permitsPerSecond 每秒允许的请求量
     * @param timeout 超时时间
     * @param unit 时间单位
     * @return 是否获取成功
     */
    public boolean tryAcquire(String key, double permitsPerSecond, long timeout, TimeUnit unit) {
        com.google.common.util.concurrent.RateLimiter rateLimiter = getRateLimiter(key, permitsPerSecond);
        return rateLimiter.tryAcquire(1, timeout, unit);
    }
}

2.3 创建拦截器

@Component
public class TokenBucketInterceptor implements HandlerInterceptor {
    
    @Autowired
    private RateLimiter rateLimiter;
    
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 仅对API请求进行限流
        String requestURI = request.getRequestURI();
        if (!requestURI.startsWith("/api/")) {
            return true;
        }
        
        // 获取IP地址作为限流键
        String ip = getIpAddress(request);
        String key = ip + ":" + requestURI;
        
        // 尝试获取令牌,设置每秒2个请求的速率,等待100毫秒
        boolean acquired = rateLimiter.tryAcquire(key, 2.0, 100, TimeUnit.MILLISECONDS);
        
        if (!acquired) {
            // 获取失败,返回限流响应
            response.setContentType("application/json;charset=UTF-8");
            response.setStatus(HttpStatus.TOO_MANY_REQUESTS.value());
            response.getWriter().write("{"code":429,"message":"请求过于频繁,请稍后再试"}");
            return false;
        }
        
        return true;
    }
    
    // getIpAddress方法同上
}

2.4 配置拦截器

@Configuration
public class WebConfig implements WebMvcConfigurer {
    
    @Autowired
    private TokenBucketInterceptor tokenBucketInterceptor;
    
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(tokenBucketInterceptor)
                .addPathPatterns("/**");
    }
}

优缺点分析

优点:

  • 支持突发流量,不会完全拒绝短时高峰
  • 平滑的限流效果,用户体验更好
  • 可以配置不同接口的不同限流策略
  • 无需额外的存储设施

缺点:

  • 只适用于单机部署,分布式环境需要额外改造
  • 重启应用后状态丢失
  • 无法精确控制时间窗口内的请求总量

3. 分布式限流(Redis + Lua脚本)

对于分布式系统,单机限流方案难以满足需求。利用Redis和Lua脚本可以实现高效的分布式限流。

实现步骤

3.1 定义Lua脚本

创建一个Redis限流的Lua脚本,放在resources目录下的scripts/rate_limiter.lua:

-- 限流Key
local key = KEYS[1]
-- 限流窗口,单位秒
local window = tonumber(ARGV[1])
-- 限流阈值
local threshold = tonumber(ARGV[2])
-- 当前时间戳
local now = tonumber(ARGV[3])

-- 移除过期的请求记录
redis.call('ZREMRANGEBYSCORE', key, 0, now - window * 1000)

-- 获取当前窗口内的请求数
local count = redis.call('ZCARD', key)

-- 如果请求数超过阈值,拒绝请求
if count >= threshold then
    return 0
end

-- 添加当前请求记录
redis.call('ZADD', key, now, now .. '-' .. math.random())
-- 设置过期时间
redis.call('EXPIRE', key, window)

-- 返回当前窗口剩余可用请求数
return threshold - count - 1

3.2 创建Redis限流服务

@Service
@Slf4j
public class RedisRateLimiterService {
    
    @Autowired
    private StringRedisTemplate redisTemplate;
    
    private DefaultRedisScript<Long> rateLimiterScript;
    
    @PostConstruct
    public void init() {
        // 加载Lua脚本
        rateLimiterScript = new DefaultRedisScript<>();
        rateLimiterScript.setLocation(new ClassPathResource("scripts/rate_limiter.lua"));
        rateLimiterScript.setResultType(Long.class);
    }
    
    /**
     * 尝试获取访问权限
     * @param key 限流键
     * @param window 时间窗口(秒)
     * @param threshold 阈值
     * @return 剩余可用请求数,-1表示被限流
     */
    public long isAllowed(String key, int window, int threshold) {
        try {
            // 执行lua脚本
            List<String> keys = Collections.singletonList(key);
            Long remainingCount = redisTemplate.execute(
                rateLimiterScript, 
                keys, 
                String.valueOf(window), 
                String.valueOf(threshold),
                String.valueOf(System.currentTimeMillis())
            );
            
            return remainingCount == null ? -1 : remainingCount;
        } catch (Exception e) {
            log.error("Redis rate limiter error", e);
            // 发生异常时放行请求
            return threshold;
        }
    }
}

3.3 创建分布式限流注解

@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface DistributedRateLimit {
    /**
     * 限流的key前缀
     */
    String prefix() default "rate:";
    
    /**
     * 时间窗口,单位秒
     */
    int window() default 60;
    
    /**
     * 在时间窗口内允许的最大请求数
     */
    int threshold() default 10;
    
    /**
     * 限流模式: ip - 按IP限流, user - 按用户限流, all - 接口总体限流
     */
    String mode() default "ip";
}

3.4 实现分布式限流切面

@Aspect
@Component
@Slf4j
public class DistributedRateLimitAspect {
    
    @Autowired
    private RedisRateLimiterService rateLimiterService;
    
    @Autowired(required = false)
    private HttpServletRequest request;
    
    @Around("@annotation(rateLimit)")
    public Object around(ProceedingJoinPoint pjp, DistributedRateLimit rateLimit) throws Throwable {
        String key = generateKey(pjp, rateLimit);
        
        long remainingCount = rateLimiterService.isAllowed(
            key, 
            rateLimit.window(), 
            rateLimit.threshold()
        );
        
        if (remainingCount < 0) {
            throw new RuntimeException("接口访问过于频繁,请稍后再试");
        }
        
        // 执行目标方法
        return pjp.proceed();
    }
    
    private String generateKey(ProceedingJoinPoint pjp, DistributedRateLimit rateLimit) {
        String methodName = pjp.getSignature().getName();
        String className = pjp.getTarget().getClass().getName();
        StringBuilder key = new StringBuilder(rateLimit.prefix());
        
        key.append(className).append(".").append(methodName);
        
        // 根据限流模式添加不同的后缀
        switch (rateLimit.mode()) {
            case "ip":
                // 按IP限流
                key.append(":").append(getIpAddress());
                break;
            case "user":
                // 按用户限流
                Object userId = getUserId();
                key.append(":").append(userId != null ? userId : "anonymous");
                break;
            case "all":
                // 接口总体限流,不添加后缀
                break;
            default:
                key.append(":").append(getIpAddress());
                break;
        }
        
        return key.toString();
    }
    
    private String getIpAddress() {
        // IP获取方法同上
        if (request == null) {
            return "unknown";
        }
        // 获取IP的代码同上一个示例
        return "127.0.0.1"; // 简化处理
    }
    
    // 获取当前用户ID,根据实际认证系统实现
    private Object getUserId() {
        // 这里简化处理,实际中应从认证信息中获取
        // 例如:SecurityContextHolder.getContext().getAuthentication().getPrincipal()
        return null;
    }
}

3.5 使用示例

@RestController
@RequestMapping("/api")
public class PaymentController {
    
    @DistributedRateLimit(prefix = "pay:", window = 3600, threshold = 5, mode = "user")
    @PostMapping("/payment")
    public Result createPayment(@RequestBody PaymentRequest paymentRequest) {
        // 创建支付业务逻辑
        return paymentService.createPayment(paymentRequest);
    }
    
    @DistributedRateLimit(window = 60, threshold = 30, mode = "ip")
    @GetMapping("/products")
    public List<Product> getProducts() {
        // 查询产品列表
        return productService.findAll();
    }
    
    @DistributedRateLimit(window = 1, threshold = 100, mode = "all")
    @GetMapping("/hot/resource")
    public Resource getHotResource() {
        // 获取热门资源
        return resourceService.getHotResource();
    }
}

优缺点分析

优点:

  • 适用于分布式系统,多实例间共享限流状态
  • 支持多种限流模式:按IP、用户、接口总量等
  • 基于滑动窗口,计数更精确
  • 使用Lua脚本保证原子性,避免竞态条件

缺点:

  • 强依赖Redis
  • 实现复杂度较高

4. 集成Sentinel实现接口防刷

阿里巴巴开源的Sentinel是一个强大的流量控制组件,提供了丰富的限流、熔断、系统保护等功能。

实现步骤

4.1 添加依赖

<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
    <version>2021.0.4.0</version>
</dependency>

4.2 配置Sentinel

在application.properties中添加配置:

# Sentinel 控制台地址
spring.cloud.sentinel.transport.dashboard=localhost:8080
# 取消Sentinel控制台懒加载
spring.cloud.sentinel.eager=true
# 应用名称
spring.application.name=my-application

4.3 创建Sentinel配置

@Configuration
public class SentinelConfig {
    
    @Bean
    public SentinelResourceAspect sentinelResourceAspect() {
        return new SentinelResourceAspect();
    }
    
    @PostConstruct
    public void init() {
        // 定义流控规则
        initFlowRules();
    }
    
    private void initFlowRules() {
        List<FlowRule> rules = new ArrayList<>();
        
        // 为/api/user接口设置流控规则
        FlowRule userRule = new FlowRule();
        userRule.setResource("/api/user");
        userRule.setGrade(RuleConstant.FLOW_GRADE_QPS); // 基于QPS限流
        userRule.setCount(10); // 每秒允许10个请求
        rules.add(userRule);
        
        // 为/api/order接口设置流控规则
        FlowRule orderRule = new FlowRule();
        orderRule.setResource("/api/order");
        orderRule.setGrade(RuleConstant.FLOW_GRADE_QPS);
        orderRule.setCount(5); // 每秒允许5个请求
        orderRule.setControlBehavior(RuleConstant.CONTROL_BEHAVIOR_WARM_UP); // 预热模式
        orderRule.setWarmUpPeriodSec(10); // 10秒预热期
        rules.add(orderRule);
        
        // 加载规则
        FlowRuleManager.loadRules(rules);
    }
}

4.4 创建URL资源解析器

@Component
public class UrlCleaner implements RequestOriginParser {
    
    @Override
    public String parseOrigin(HttpServletRequest request) {
        // 获取请求的URL路径
        String path = request.getRequestURI();
        
        // 可以添加更复杂的解析逻辑,例如:
        // 1. 去除路径变量:/api/user/123 -> /api/user/{id}
        // 2. 添加请求方法前缀:GET:/api/user
        
        return path;
    }
}

4.5 创建全局异常处理器

@RestControllerAdvice
public class SentinelExceptionHandler {
    
    @ExceptionHandler(BlockException.class)
    public Result handleBlockException(BlockException e) {
        String message = "请求过于频繁,请稍后再试";
        if (e instanceof FlowException) {
            message = "接口限流:" + message;
        } else if (e instanceof DegradeException) {
            message = "服务降级:系统繁忙,请稍后再试";
        } else if (e instanceof ParamFlowException) {
            message = "热点参数限流:请求过于频繁";
        } else if (e instanceof SystemBlockException) {
            message = "系统保护:系统资源不足";
        } else if (e instanceof AuthorityException) {
            message = "授权控制:没有访问权限";
        }
        
        return Result.error(429, message);
    }
}

4.6 使用@SentinelResource注解

@RestController
@RequestMapping("/api")
public class UserController {
    
    // 使用资源名定义限流资源
    @SentinelResource(value = "getUserById", 
                      blockHandler = "getUserBlockHandler",
                      fallback = "getUserFallback")
    @GetMapping("/user/{id}")
    public User getUser(@PathVariable Long id) {
        return userService.getUser(id);
    }
    
    // 限流处理方法
    public User getUserBlockHandler(Long id, BlockException e) {
        log.warn("Get user request blocked: {}", id, e);
        throw new RuntimeException("请求频率过高,请稍后再试");
    }
    
    // 异常回退方法
    public User getUserFallback(Long id, Throwable t) {
        log.error("Get user failed: {}", id, t);
        User fallbackUser = new User();
        fallbackUser.setId(id);
        fallbackUser.setName("Unknown");
        return fallbackUser;
    }
}

4.7 更复杂的限流规则配置

@Service
@Slf4j
public class SentinelRuleService {
    
    public void initComplexFlowRules() {
        List<FlowRule> rules = new ArrayList<>();
        
        // 基于QPS + 调用关系的限流规则
        FlowRule apiRule = new FlowRule();
        apiRule.setResource("/api/data");
        apiRule.setGrade(RuleConstant.FLOW_GRADE_QPS);
        apiRule.setCount(20);
        
        // 限制调用来源
        apiRule.setLimitApp("frontend"); // 只限制来自前端应用的调用
        
        // 流控策略:关联资源
        apiRule.setStrategy(RuleConstant.STRATEGY_RELATE);
        apiRule.setRefResource("/api/important"); // 当important接口QPS高时,限制data接口
        
        rules.add(apiRule);
        
        // 基于并发线程数的限流
        FlowRule threadRule = new FlowRule();
        threadRule.setResource("/api/heavy-task");
        threadRule.setGrade(RuleConstant.FLOW_GRADE_THREAD); // 基于线程数
        threadRule.setCount(5); // 最多5个线程同时处理
        rules.add(threadRule);
        
        // 加载规则
        FlowRuleManager.loadRules(rules);
    }
    
    public void initHotspotRules() {
        // 热点参数限流规则
        List<ParamFlowRule> rules = new ArrayList<>();
        
        ParamFlowRule rule = new ParamFlowRule("/api/product");
        // 对第0个参数(productId)进行限流
        rule.setParamIdx(0);
        rule.setCount(5);
        
        // 特例配置
        ParamFlowItem item1 = new ParamFlowItem();
        item1.setObject("1"); // productId = 1的商品
        item1.setCount(10);  // 可以有更高的QPS
        
        ParamFlowItem item2 = new ParamFlowItem();
        item2.setObject("2"); // productId = 2的商品
        item2.setCount(2);   // 更严格的限制
        
        rule.setParamFlowItemList(Arrays.asList(item1, item2));
        
        rules.add(rule);
        ParamFlowRuleManager.loadRules(rules);
    }
}

优缺点分析

优点:

  • 功能全面,支持QPS限流、并发线程数限流、热点参数限流等
  • 支持多种控制策略:直接拒绝、预热、排队等
  • 提供控制台可视化管理
  • 支持动态规则调整
  • 可与Spring Cloud体系无缝集成

缺点:

  • 学习曲线较陡峭
  • 分布式场景下需要额外配置规则持久化
  • 引入了额外的依赖

5. 验证码与行为分析防刷

对于某些敏感操作(如登录、注册、支付等),可以结合验证码和行为分析来防止恶意请求。

实现步骤

5.1 图形验证码实现

首先添加依赖:

<dependency>
    <groupId>com.github.whvcse</groupId>
    <artifactId>easy-captcha</artifactId>
    <version>1.6.2</version>
</dependency>

5.2 创建验证码服务

@Service
public class CaptchaService {
    
    @Autowired
    private StringRedisTemplate redisTemplate;
    
    private static final long CAPTCHA_EXPIRE_TIME = 5 * 60; // 5分钟
    
    /**
     * 生成验证码
     * @param request HTTP请求
     * @param response HTTP响应
     * @return 验证码Base64字符串
     */
    public String generateCaptcha(HttpServletRequest request, HttpServletResponse response) {
        // 生成验证码
        SpecCaptcha captcha = new SpecCaptcha(130, 48, 5);
        
        // 生成验证码ID
        String captchaId = UUID.randomUUID().toString();
        
        // 将验证码存入Redis
        redisTemplate.opsForValue().set(
            "captcha:" + captchaId, 
            captcha.text().toLowerCase(), 
            CAPTCHA_EXPIRE_TIME, 
            TimeUnit.SECONDS
        );
        
        // 设置Cookie
        Cookie cookie = new Cookie("captchaId", captchaId);
        cookie.setMaxAge((int) CAPTCHA_EXPIRE_TIME);
        cookie.setPath("/");
        response.addCookie(cookie);
        
        // 返回Base64编码的验证码图片
        return captcha.toBase64();
    }
    
    /**
     * 验证验证码
     * @param request HTTP请求
     * @param captchaCode 用户输入的验证码
     * @return 是否验证通过
     */
    public boolean validateCaptcha(HttpServletRequest request, String captchaCode) {
        // 从Cookie获取验证码ID
        Cookie[] cookies = request.getCookies();
        String captchaId = null;
        
        if (cookies != null) {
            for (Cookie cookie : cookies) {
                if ("captchaId".equals(cookie.getName())) {
                    captchaId = cookie.getValue();
                    break;
                }
            }
        }
        
        if (captchaId == null) {
            return false;
        }
        
        // 从Redis获取正确的验证码
        String key = "captcha:" + captchaId;
        String correctCode = redisTemplate.opsForValue().get(key);
        
        // 验证成功后删除验证码
        if (correctCode != null && correctCode.equals(captchaCode.toLowerCase())) {
            redisTemplate.delete(key);
            return true;
        }
        
        return false;
    }
}

5.3 创建验证码控制器

@RestController
@RequestMapping("/api/captcha")
public class CaptchaController {
    
    @Autowired
    private CaptchaService captchaService;
    
    @GetMapping
    public Map<String, String> getCaptcha(HttpServletRequest request, HttpServletResponse response) {
        String base64 = captchaService.generateCaptcha(request, response);
        return Map.of("captcha", base64);
    }
}

5.4 创建验证码注解

@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface CaptchaRequired {
    String captchaParam() default "captchaCode";
}

5.5 实现验证码拦截器

@Component
public class CaptchaInterceptor implements HandlerInterceptor {
    
    @Autowired
    private CaptchaService captchaService;
    
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if (!(handler instanceof HandlerMethod)) {
            return true;
        }
        
        HandlerMethod handlerMethod = (HandlerMethod) handler;
        CaptchaRequired captchaRequired = handlerMethod.getMethodAnnotation(CaptchaRequired.class);
        
        if (captchaRequired == null) {
            return true;
        }
        
        // 获取验证码参数
        String captchaParam = captchaRequired.captchaParam();
        String captchaCode = request.getParameter(captchaParam);
        
        if (StringUtils.hasText(captchaCode)) {
            // 验证验证码
            boolean valid = captchaService.validateCaptcha(request, captchaCode);
            if (valid) {
                return true;
            }
        }
        
        // 验证失败
        response.setContentType("application/json;charset=UTF-8");
        response.setStatus(HttpStatus.BAD_REQUEST.value());
        response.getWriter().write("{"code":400,"message":"验证码错误或已过期"}");
        return false;
    }
}

5.6 创建行为分析服务

@Service
@Slf4j
public class BehaviorAnalysisService {
    
    @Autowired
    private StringRedisTemplate redisTemplate;
    
    /**
     * 检查是否是可疑的机器行为
     * @param request HTTP请求
     * @return 是否可疑
     */
    public boolean isSuspicious(HttpServletRequest request) {
        // 1. 获取客户端信息
        String ip = getIpAddress(request);
        String userAgent = request.getHeader("User-Agent");
        String requestId = request.getSession().getId();
        
        // 2. 检查访问频率
        String freqKey = "behavior:freq:" + ip;
        Long count = redisTemplate.opsForValue().increment(freqKey, 1);
        redisTemplate.expire(freqKey, 1, TimeUnit.MINUTES);
        
        if (count != null && count > 30) {
            log.warn("访问频率异常: IP={}, count={}", ip, count);
            return true;
        }
        
        // 3. 检查User-Agent
        if (userAgent == null || isBotuserAgent(userAgent)) {
            log.warn("可疑的User-Agent: {}", userAgent);
            return true;
        }
        
        // 4. 检查请求时间模式
        String timeKey = "behavior:time:" + ip;
        long now = System.currentTimeMillis();
        String lastTimeStr = redisTemplate.opsForValue().get(timeKey);
        
        if (lastTimeStr != null) {
            long lastTime = Long.parseLong(lastTimeStr);
            long interval = now - lastTime;
            
            // 如果请求间隔非常均匀,可能是机器人
            if (isUniformInterval(ip, interval)) {
                log.warn("请求间隔异常均匀: IP={}, interval={}", ip, interval);
                return true;
            }
        }
        
        redisTemplate.opsForValue().set(timeKey, String.valueOf(now), 10, TimeUnit.MINUTES);
        
        // 更多高级检测逻辑...
        
        return false;
    }
    
    /**
     * 检查是否是机器人UA
     */
    private boolean isBotuserAgent(String userAgent) {
        String ua = userAgent.toLowerCase();
        return ua.contains("bot") || ua.contains("spider") || ua.contains("crawl") ||
               ua.isEmpty() || ua.length() < 40;
    }
    
    /**
     * 检查请求间隔是否异常均匀
     */
    private boolean isUniformInterval(String ip, long interval) {
        String key = "behavior:intervals:" + ip;
        
        // 获取最近的几个间隔
        List<String> intervalStrs = redisTemplate.opsForList().range(key, 0, 4);
        redisTemplate.opsForList().leftPush(key, String.valueOf(interval));
        redisTemplate.opsForList().trim(key, 0, 9);  // 只保留最近10个
        redisTemplate.expire(key, 10, TimeUnit.MINUTES);
        
        if (intervalStrs == null || intervalStrs.size() < 5) {
            return false;
        }
        
        // 计算间隔的方差,方差小说明请求间隔很均匀
        List<Long> intervals = intervalStrs.stream()
                .map(Long::parseLong)
                .collect(Collectors.toList());
        
        double mean = intervals.stream().mapToLong(Long::longValue).average().orElse(0);
        double variance = intervals.stream()
                .mapToDouble(i -> Math.pow(i - mean, 2))
                .average()
                .orElse(0);
        
        return variance < 100;  // 方差阈值,需要根据实际情况调整
    }
    
    // getIpAddress方法同上
}

5.7 创建行为分析拦截器

@Component
public class BehaviorAnalysisInterceptor implements HandlerInterceptor {
    
    @Autowired
    private BehaviorAnalysisService behaviorAnalysisService;
    
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 对于需要保护的端点进行检查
        String path = request.getRequestURI();
        if (path.startsWith("/api/") && isPotentialRiskEndpoint(path)) {
            boolean suspicious = behaviorAnalysisService.isSuspicious(request);
            
            if (suspicious) {
                // 需要验证码或其他额外验证
                response.setContentType("application/json;charset=UTF-8");
                response.setStatus(HttpStatus.TOO_MANY_REQUESTS.value());
                response.getWriter().write("{"code":429,"message":"检测到异常访问,请进行验证","needCaptcha":true}");
                return false;
            }
        }
        
        return true;
    }
    
    /**
     * 判断是否是高风险端点
     */
    private boolean isPotentialRiskEndpoint(String path) {
        return path.contains("/login") || 
               path.contains("/register") || 
               path.contains("/payment") || 
               path.contains("/order") ||
               path.contains("/password");
    }
}

5.8 使用示例

@RestController
@RequestMapping("/api")
public class UserController {
    
    @CaptchaRequired
    @PostMapping("/login")
    public Result login(@RequestParam String username, 
                        @RequestParam String password,
                        @RequestParam String captchaCode) {
        // 登录逻辑
        return userService.login(username, password);
    }
    
    @CaptchaRequired
    @PostMapping("/register")
    public Result register(@RequestBody UserRegisterDTO registerDTO,
                          @RequestParam String captchaCode) {
        // 注册逻辑
        return userService.register(registerDTO);
    }
}

优缺点分析

优点:

  • 能有效区分人类用户和自动化脚本
  • 对恶意用户有较强的阻止作用
  • 针对敏感操作提供额外安全层
  • 可以实现自适应安全策略

缺点:

  • 增加了用户操作成本,可能影响用户体验
  • 实现复杂,需要前后端配合
  • 某些验证码可能被OCR技术破解
  • 行为分析可能产生误判

方案对比与选择

方案

实现难度

防刷效果

分布式支持

用户体验

适用场景

基于注解的访问频率限制

低

中

需配合Redis

一般

一般接口,简单场景

令牌桶算法

中

中高

单机

好

允许突发流量的场景

分布式限流(Redis+Lua)

高

高

支持

一般

分布式系统,精确限流

Sentinel

中高

高

需额外配置

可配置

复杂系统,多维度防护

验证码与行为分析

高

高

支持

较差

敏感操作,关键业务

总结

接口防刷是一个系统性工程,需要考虑多方面因素:安全性、用户体验、性能开销和运维复杂度等。本文介绍的5种方案各有优缺点,可以根据实际需求灵活选择和组合。

无论采用哪种方案,接口防刷都应该遵循以下原则:

  • 最小影响原则:尽量不影响正常用户的体验
  • 梯度防护原则:根据接口的重要程度采用不同强度的防护措施
  • 可监控原则:提供充分的监控和告警机制
  • 灵活调整原则:支持动态调整防护参数和策略

通过合理实施接口防刷策略,可以有效提高系统的安全性和稳定性,为用户提供更好的服务体验。

教你如何实现接口防刷 我们在浏览网站后台的时候,假如我们频繁请求,那么网站会提示的字样,那么这个功能究竟有什么用呢,又是如何实现的呢?其实这就是接口防刷的一种处理方式,通过在一定时间内限制同一用户对同一个接口的请求次数,其目的是为了防止恶意访问导致服务器和数据库的压力增大,也可以防止用户重复提交。 阅读详情

相关推荐

SpringBoot注解限制接口访问次数

Retention;Target;

qq_45506362的博客 1125

SpringBoot接口防刷(限制指定用户对指定接口指定时间范围内的访问次数)

在你请求的时候,服务器通过redis记录下你的请求次数,如果次数超出限制,则不给访问,在redis保存key是有时效性的,过期就会删除。这次我们使用 注解 拦截器 缓存 来实现这个功能。

liuerpeng1904的博客 4329

SpringBoot实现接口防刷的5种高效方案详解

在当今互联网应用中,接口安全是系统设计中不可忽视的重要环节。恶意用户或自动化脚本的高频请求不仅会消耗宝贵的服务器资源,还可能导致数据异常、服务不可用甚至系统崩溃。本文将详细介绍在SpringBoot框架下实现接口防刷的5种技术方案,帮助开发者构建更加健壮的应用系统。本文详细介绍了SpringBoot框架下5种接口防刷方案,从简单的注解限流到复杂的Sentinel集成,开发者可以根据实际需求选择合适的方案。在实际项目中,通常需要组合多种方案来构建全方位的防护体系。技术选型关键点评估系统规模(单机/分布式)

iku_n的博客 1325

springboot接口防刷实现

本文主要介绍一种通过实现自定义注解,实现一种比较通用的接口防刷方式

Terry Tian' coding Life 1817

太强了!SpringBoot 3 接口防刷的 8 种高效解决方案,你用对了吗

方案优点缺点适用场景Nginx限流性能高,防护前置粒度粗,用户维度不可控静态资源、统一入口Redis+注解限流灵活,适配复杂场景侵入代码,复杂度高登录/操作类接口Guava限流轻量无依赖单机不可集群共享单体系统限流Sentinel限流支持控制台、维度丰富需额外部署 Sentinel 控制台微服务系统图形验证码防止脚本攻击用户体验一般注册/登录滑块验证码智能行为识别接入复杂,成本高高安全场景IP白名单简洁、安全无法动态扩展内部系统/回调接口。

nihao2q的博客 916

Apache Paimon快照管理实战:如何避免Flink作业中的反压问题?

本文深入探讨了如何通过优化Apache Paimon的快照管理机制来避免Flink作业中的反压问题。详细解析了同步与异步快照过期模式的适用场景及配置建议,并提供了Checkpoint策略与状态后端的协同优化方案,帮助提升实时数据处理的性能和稳定性。

i1j2k的博客 178

【后端分享】SpringBoot实现接口防刷的5种实现方案!

创建一个Redis限流的Lua脚本,放在resources目录下的。

weixin_60937489的博客 1980

Visio中高效绘制空间坐标系的实用技巧

本文详细介绍了在Visio中从零开始高效绘制精准空间坐标系的实用技巧。文章摒弃了现成形状的局限,重点讲解了如何利用直线、箭头和文本框等基础工具,结合网格设置、绘图比例调整和图层管理,构建完全可控的三维坐标系。通过分步指南,帮助用户掌握刻度添加、标注对齐及模板创建等高级方法,从而在科研绘图与工程制图中实现专业级的视觉效果。

e3f4g5的博客 560

SpringBoot 接口防刷的5种方案,太强了!

然后,我们移除过期的请求记录,统计当前窗口内的请求数。令牌桶算法是一种更加灵活的限流算法,它就像一个装有令牌的桶,系统会以固定的速率向桶中添加令牌,每个请求需要从桶中获取一个令牌才能被处理。恶意的高频请求如同隐藏在暗处的“杀手”,不仅会大量消耗服务器宝贵的资源,还可能引发数据异常,严重时甚至会导致整个系统瘫痪,给业务带来不可估量的损失。阿里巴巴开源的 Sentinel 是一个强大的流量控制组件,它就像一个智能的“交通警察”,可以对系统的流量进行实时监控和控制,提供了丰富的限流、熔断、系统保护等功能。

码猿技术专栏 457

基于STM32的多路火灾报警系统设计

本文设计了一款基于STM32的多路火灾报警系统,利用多个传感器组合(如烟雾传感器、温度传感器等)进行数据采集与处理,精确判断火灾情况,并及时发出报警信号,保障生命与财产安全。本系统设计了一款基于STM32的多路火灾报警系统,通过结合烟雾传感器和温度传感器,提升了火灾检测的准确性和可靠性。未来,系统可以进一步集成更多传感器,如气体传感器、湿度传感器等,进一步提升火灾报警系统的智能化水平。本系统采用STM32F103单片机作为核心处理单元,结合烟雾传感器、温度传感器和报警模块,实现多通道火灾报警功能。

stm32d1219的博客 2034

Unity UI适配实战:从Canvas Scaler到主流游戏适配方案解析

本文深入解析Unity UI适配的核心技术,重点介绍Canvas Scaler组件的关键参数如Reference Resolution和Screen Match Mode,并提供主流游戏适配方案实战分析。通过九宫格拉伸、动态调整等技巧,帮助开发者解决多设备适配难题,提升游戏UI在不同分辨率下的表现。

weixin_29001419的博客 975

SpringBoot 3 接口防刷的 8 种高效解决方案

SpringBoot 3 接口防刷的 8 种高效解决方案

weixin_46619605的博客 1353

JW01二氧化碳传感器实战:5分钟搞定Arduino环境监测系统(附完整代码)

本文详细介绍了如何使用JW01二氧化碳传感器与Arduino快速搭建环境监测系统。通过简单的硬件连接和完整的代码示例,读者可以在5分钟内实现CO2浓度实时监测,并了解数据解析、常见问题解决及进阶应用扩展。JW01传感器的高精度和易用性使其成为创客和嵌入式开发者的理想选择。

kite3的博客 384

关于Springboot集成Redis实现接口防刷

关于Springboot集成Redis实现接口防刷 小贴士:目前大部分公司都采用前后端分离的开发方式,进行项目的并行开发。在项目中后台只需要提供一套API接口,就可以接入安卓、小程序、IOS、web等多个应用程序,这样可以节约开发成本。但是一个后台接入这么多应用程序的http请求,必然导致后端的压力非常大。所以对于一些请求进行过滤和拦截是非常有必要的,能够有效地减轻后台的压力。 接口防刷机制:主要防止短时间接口被大量调用(攻击),出现系统崩溃和系统爬虫问题,提升服务的可用性。 方案一(AOP) 一、什么是A

weixin_49919104的博客 972

深入解析NRF52832安全DFU Bootloader实现机制与蓝牙OTA升级流程

本文深入解析了NRF52832芯片安全DFU Bootloader的实现机制与蓝牙OTA升级流程。详细阐述了Bootloader的核心概念、双区升级策略、源码初始化流程以及蓝牙DFU服务的数据传输协议,并提供了从环境搭建、密钥生成到编译烧录、生成升级包的完整实战指南,帮助开发者构建安全可靠的无线固件更新系统。

weixin_29188043的博客 495

fanuc机器人I/O分配

FANUC机器人在和其他外部设备进行通讯时,需要对信号进行配置。 在机器人处于UOP自动分配状态时,系统会自动检索已配对设备,并分配相应的机架号。 当我们需要手动对信号进行分配时,可参考以下机架号进行配置。 不同的通讯协议对应的机架号不同,外部通讯协议常用的机架号参考下表: 序号 机架号 通讯类型 1 0 Process I/O 2 1-15

haier123888的博客 1万+

SpringBoot接口防刷

SpringBoot接口防刷 API 接口防刷 顾名思义,想让某个接口某个人在某段时间内只能请求N次。 在项目中比较常见的问题也有,那就是连点按钮导致请求多次,以前在web端有表单重复提交,可以通过token 来解决。 除了上面的方法外,前后端配合的方法。现在全部由后端来控制。 原理 在你请求的时候,服务器通过redis 记录下你请求的次数,如果次数超过限制就不给访问。 在redis 保存的key 是有时效性的,过期就会删除。 代码实现: 为了让它看起来逼格高一点,所以以自定义注解的方式实现 @Reques

YSecret_Y的博客 1858

手把手教你学Simulink——基于Simulink的模糊自适应PI调节FOC电流环

本文提出基于模糊自适应PI控制的FOC电流环优化方案,针对传统PI控制参数固化、抗扰性差等问题,通过模糊逻辑在线调整PI参数。方案采用双输入(电流误差及变化率)和三输出(ΔKp/ΔKi/PI调节)结构,实现动态响应时间<30μs(提升38%)、超调<5%(降低71%)、参数摄动下误差<±0.4A(精度提升73%)。详细介绍了Simulink建模步骤,包括模糊规则设计(7语言变量、49条规则)、隶属度函数配置及自适应PI实现逻辑,并提供实机部署建议。仿真结果表明,该方法显著提升系统鲁棒性,特

MHD0815的博客 79

SpringBoot API 接口防刷

SpringBoot API 接口防刷

mry6的博客 961

android 蓝牙通讯demo

android 下蓝牙通讯demo,实现了发现蓝牙,使能蓝牙,通过蓝牙发送消息。

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

springboot接口防刷

API 接口防刷 顾名思义,想让某个接口某个人在某段时间内只能请求N次。 在项目中比较常见的问题也有,那就是连点按钮导致请求多次,以前在web端有表单重复提交,可以通过token 来解决。 除了上面的方法外,前后端配合的方法。现在全部由后端来控制。 原理 在你请求的时候,服务器通过redis 记录下你请求的次数,如果次数超过限制就不给访问。 在redis 保存的key 是有时效性的,过期就会删除。 代码实现: 为了让它看起来逼格高一点,所以以自定义注解的方式实现 @RequestLimit 注解 impor

qq_37491970的博客 1056

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

上一篇: SpringBoot中3种优雅停机的实现方式
下一篇: SpringBoot中的4种重试机制实现方案
程序员职业指南
博客等级 码龄4年 2140粉丝 960原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值