亚洲视频二区_亚洲欧洲日本天天堂在线观看_日韩一区二区在线观看_中文字幕不卡一区

公告:魔扣目錄網為廣大站長提供免費收錄網站服務,提交前請做好本站友鏈:【 網站目錄:http://www.430618.com 】, 免友鏈快審服務(50元/站),

點擊這里在線咨詢客服
新站提交
  • 網站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會員:747

定義注解

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

/**
 * 接口防刷注解類
 * @Author: Cyz
 * @Description:
 * @Date: create in 2022/6/28 16:55
 */
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface AccessLimit {
    int seconds();
    int maxCount();
    boolean needLogin() default true;
}

編寫接口防刷攔截器

import com.cyz.blog.utils.AccessLimit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;

/**
 * 接口防刷攔截器
 * @Author: Cyz
 * @Description:
 * @Date: create in 2022/6/28 16:58
 */
@Component
public class AntiBrushInterceptor extends HandlerInterceptorAdapter {

    @Autowired
    private RedisTemplate redisTemplate;


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //判斷請求是否屬于方法的請求
        if(handler instanceof HandlerMethod){
            HandlerMethod handlerMethod=(HandlerMethod) handler;

            //獲取方法中的注解,看是否有該注解
            AccessLimit accessLimit = handlerMethod.getMethodAnnotation(AccessLimit.class);
            if(accessLimit==null){
                return true;
            }
            int seconds = accessLimit.seconds();
            int maxcount = accessLimit.maxCount();
            boolean login = accessLimit.needLogin();
            String key = request.getRequestURI();
            //如果需要登錄
            if(login){
                //獲取登錄的session進行判斷
                //.......
//                key+=""+"1"; //用戶id userId
            }

            //從redis中獲取用戶訪問的次數
            //            AccessKey ak = AccessKey.withExpire(seconds);
            Integer count= (Integer)redisTemplate.opsForValue().get(key);
            if(count==null){
                //第一次訪問
                redisTemplate.opsForValue().set(key,1);
            }else if(count<maxcount){
                //加1
                redisTemplate.opsForValue().set(key,(Integer)redisTemplate.opsForValue().get(key)+1,seconds, TimeUnit.SECONDS);
            }else{
                //超出訪問次數
                System.out.println(key+":訪問次數超多!!!");
                render(response,key+":請您休息片刻再試試!");
                return false;
            }
        }
        return true;
    }

    private void render(HttpServletResponse response, String message)throws Exception {
        response.setContentType("Application/json;charset=UTF-8");
        OutputStream out = response.getOutputStream();
        String data = "{"code":501,"flag":false,"message":"+message+"}";
        out.write(data.getBytes("UTF-8"));
        out.flush();
        out.close();
    }
}

將攔截器注冊到spring容器中

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.WebMvcConfigurerAdapter;

/**
 * @Author: Cyz
 * @Description:
 * @Date: create in 2022/6/28 17:01
 */
@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {
    @Autowired
    private AntiBrushInterceptor interceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(interceptor);
    }
}

controller接口編寫

@AccessLimit(seconds = 5,maxCount = 5,needLogin = true)

接口防刷測試

Spring Boot接口限制訪問次數

 

參考原文:cyz

分享到:
標簽:Spring Boot
用戶無頭像

網友整理

注冊時間:

網站:5 個   小程序:0 個  文章:12 篇

  • 51998

    網站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會員

趕快注冊賬號,推廣您的網站吧!
最新入駐小程序

數獨大挑戰2018-06-03

數獨一種數學游戲,玩家需要根據9

答題星2018-06-03

您可以通過答題星輕松地創建試卷

全階人生考試2018-06-03

各種考試題,題庫,初中,高中,大學四六

運動步數有氧達人2018-06-03

記錄運動步數,積累氧氣值。還可偷

每日養生app2018-06-03

每日養生,天天健康

體育訓練成績評定2018-06-03

通用課目體育訓練成績評定