Springboot使用redis进行api防刷限流过程详解
这篇文章主要介绍了Springboot使用redis进行api防刷限流过程详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
api限流的场景
限流的需求出现在许多常见的场景中
- 秒杀活动,有人使用软件恶意刷单抢货,需要限流防止机器参与活动
- 某api被各式各样系统广泛调用,严重消耗网络、内存等资源,需要合理限流
- 淘宝获取ip所在城市接口、微信公众号识别微信用户等开发接口,免费提供给用户时需要限流,更具有实时性和准确性的接口需要付费。
api限流实战
首先我们编写注解类AccessLimit,使用注解方式在方法上限流更优雅更方便!三个参数分别代表有效时间、最大访问次数、是否需要登录,可以理解为seconds内最多访问maxCount次。
importjava.lang.annotation.ElementType;
importjava.lang.annotation.Retention;
importjava.lang.annotation.RetentionPolicy;
importjava.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public@interfaceAccessLimit{
intseconds();
intmaxCount();
booleanneedLogin()defaulttrue;
}
限流的思路
- 通过路径:ip的作为key,访问次数为value的方式对某一用户的某一请求进行唯一标识
- 每次访问的时候判断key是否存在,是否count超过了限制的访问次数
- 若访问超出限制,则应response返回msg:请求过于频繁给前端予以展示
importorg.springframework.beans.factory.annotation.Autowired;
importorg.springframework.stereotype.Component;
importorg.springframework.web.method.HandlerMethod;
importorg.springframework.web.servlet.HandlerInterceptor;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
@Component
publicclassAccessLimtInterceptorimplementsHandlerInterceptor{
@Autowired
privateRedisServiceredisService;
@Override
publicbooleanpreHandle(HttpServletRequestrequest,HttpServletResponseresponse,Objecthandler)throwsException{
if(handlerinstanceofHandlerMethod){
HandlerMethodhm=(HandlerMethod)handler;
AccessLimitaccessLimit=hm.getMethodAnnotation(AccessLimit.class);
if(null==accessLimit){
returntrue;
}
intseconds=accessLimit.seconds();
intmaxCount=accessLimit.maxCount();
booleanneedLogin=accessLimit.needLogin();
if(needLogin){
//判断是否登录
}
Stringkey=request.getContextPath()+":"+request.getServletPath()+":"+ip;
Integercount=redisService.get(key);
if(null==count||-1==count){
redisService.set(key,1);
redisService.expire(seconds);
returntrue;
}
if(count=maxCount){
//response返回json请求过于频繁请稍后再试
returnfalse;
}
}
returntrue;
}
}
注册拦截器并配置拦截路径和不拦截路径
importorg.springframework.beans.factory.annotation.Autowired;
importorg.springframework.context.annotation.Configuration;
importorg.springframework.web.servlet.config.annotation.InterceptorRegistry;
importorg.springframework.web.servlet.config.annotation.WebMvcConfigurer;
//extendsWebMvcConfigurerAdapter已经废弃,java8开始直接继承就可以
@Configuration
publicclassIntercepterConfigimplementsWebMvcConfigurer{
@Autowired
privateAccessLimtInterceptoraccessLimtInterceptor;
@Override
publicvoidaddInterceptors(InterceptorRegistryregistry){
registry.addInterceptor(accessLimtInterceptor)
.addPathPatterns("/拦截路径")
.excludePathPatterns("/不被拦截路径通常为登录注册或者首页");
}
}
在Controller层的方法上直接可以使用注解@AccessLimit
importorg.springframework.web.bind.annotation.GetMapping;
importorg.springframework.web.bind.annotation.RequestMapping;
importorg.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("test")
publicclassTestControler{
@GetMapping("accessLimit")
@AccessLimit(seconds=3,maxCount=10)
publicStringtestAccessLimit(){
//xxxx
return"";
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。