Spring BootはAPIアンチブラシ電流制限にRedisをどのように使用しますか?

Javaテクノロジスタック

www.javastack.cn

より質の高い記事を読むためにフォロー

電流制限の要求は、多くの一般的なシナリオで発生します。

API電流制限戦闘

まず、アノテーションクラスを記述AccessLimitし、メソッドの上限フローでアノテーションメソッドを使用して、よりエレガントで便利にします。Spring BootがRedisをどのように統合するかについては、ここをクリックください

3つのパラメーターはそれぞれ、有効時間、訪問の最大数、およびログインする必要があるかどうかを表します。これは、訪問の最大数maxCount回(秒以内)として理解できます。

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

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface AccessLimit {
    int seconds();
    int maxCount();
    boolean needLogin() default true;
}

現在の制限のアイデア:

  • パスとしてip使用し、値としてアクセス時間を使用して、特定のユーザーのリクエストを一意に識別します

  • あなたが訪問するたびに、keyそれが存在するかどうか、それcountが限られた訪問数を超えているかどうかを判断

  • アクセスが制限を超えた場合は、フロントエンドにresponse戻しmsg:请求过于频繁て表示する必要があります

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class AccessLimtInterceptor implements HandlerInterceptor {

    @Autowired
    private RedisService redisService;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        if (handler instanceof HandlerMethod) {
            HandlerMethod hm = (HandlerMethod) handler;
            AccessLimit accessLimit = hm.getMethodAnnotation(AccessLimit.class);
            if (null == accessLimit) {
                return true;
            }
            int seconds = accessLimit.seconds();
            int maxCount = accessLimit.maxCount();
            boolean needLogin = accessLimit.needLogin();

            if (needLogin) {
                //判断是否登录
            }

            String key = request.getContextPath() + ":" + request.getServletPath() + ":" + ip ;

            Integer count = redisService.get(key);

            if (null == count || -1 == count) {
                redisService.set(key, 1);
                redisService.expire(seconds);
                return true;
            }

            if (count < maxCount) {
                redisService.inCr(key);
                return true;
            }

            if (count >= maxCount) {
//                response 返回 json 请求过于频繁请稍后再试
                return false;
            }
        }

        return true;
    }
}

インターセプターを登録し、インターセプトパスと非インターセプトパスを構成します。

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

// extends WebMvcConfigurerAdapter 已经废弃,java 8开始直接继承就可以
@Configuration
public class IntercepterConfig  implements WebMvcConfigurer {
    @Autowired
    private AccessLimtInterceptor accessLimtInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(accessLimtInterceptor)
                .addPathPatterns("/拦截路径")
                .excludePathPatterns("/不被拦截路径 通常为登录注册或者首页");
    }
}

Controllerあなたは直接注釈を使用することができます上の層方法@AccessLimit

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("test")
public class TestControler {

    @GetMapping("accessLimit")
    @AccessLimit(seconds = 3, maxCount = 10)
    public String testAccessLimit() {
        //xxxx
        return "";
    }
}

著者:海へ

出典:cnblogs.com/haixiang/p/12012728.html

Javaテクノロジースタックに注目して、より多くのドライグッズを見る

元のテキストをクリックして、さらに多くの特典を手に入れましょう!

おすすめ

転載: blog.csdn.net/youanyyou/article/details/108543918