首页 > 应用开发 > > Spring Boot 项目的 API 接口防刷
作者:CS打赢你
来源:https://blog.csdn.net/weixin_42533856/article/details/82593123
说明:使用了注解的方式进行对接口防刷的功能,非常高大上,本文章仅供参考 一,技术要点:springboot的基本知识,redis基本操作,
首先是写一个注解类:
/**
* @author yhq
* @date 2018/9/10 15:52
*/
@Retention(RUNTIME)
@Target(METHOD)
public @interface AccessLimit {
int seconds();
int maxCount();
boolean needLogin()default true;
}
拦截器中实现:
注册到springboot中
/**
* @author yhq
* @date 2018/9/10 15:58
*/
@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {
@Autowired
private FangshuaInterceptor interceptor;
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(interceptor);
}
}
在Controller中加入注解
/**
* @author yhq
* @date 2018/9/10 15:49
*/
@Controller
public class FangshuaController {
@AccessLimit(seconds=5, maxCount=5, needLogin=true)
@RequestMapping("/fangshua")
@ResponseBody
public Result
return Result.success("请求成功");
}
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/658/viewspace-3157265/ ,如需转载,请注明出处,否则将追究法律责任。