怎么在springboot中限制redis接口的访问频率-创新互联
本篇文章为大家展示了怎么在spring boot中限制redis 接口的访问频率,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。
目前累计服务客户上千家,积累了丰富的产品开发及服务经验。以网站设计水平和技术实力,树立企业形象,为客户提供成都网站建设、网站建设、网站策划、网页设计、网络营销、VI设计、网站改版、漏洞修补等服务。成都创新互联始终以务实、诚信为根本,不断创新和提高建站品质,通过对领先技术的掌握、对创意设计的研究、对客户形象的视觉传递、对应用系统的结合,为客户提供更好的一站式互联网解决方案,携手广大客户,共同发展进步。生产环境下可以解决的问题:
1.短信验证码请求评率限制(防止抓包短信轰炸)
2.热点数据请求评率限制(防止数据库爆炸)
@Component public class BlackInterceper implements HandlerInterceptor { @Autowired private RedisTemplateredisTemplate; private Logger log = LoggerFactory.getLogger(this.getClass()); @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { response.setHeader("Content-type", "text/html;charset=UTF-8"); String token = request.getHeader(Cons.TOKEN.WECHAT); String requestURI = request.getRequestURI(); if (StringUtils.isBlank(token)) { response.setHeader("Content-type", "text/html;charset=UTF-8"); response.getWriter().write(JsonUtils.marshalToString(ReturnResult.build(401, "未授权"))); return false; } Integer userId = (Integer) redisTemplate.opsForValue().get(Cons.TOKEN.WECHAT + ":" + token); log.error("userId={},访问了url={},请求ip={}",token,requestURI, IpUtil.getIpAddress(request)); if(redisTemplate.hasKey("black")){ if(redisTemplate.opsForSet().isMember("black", userId)){ response.getWriter().write(JsonUtils.marshalToString(ReturnResult.build(500, "由于存在恶意攻击你已被限制访问"))); return false; } } Integer count = (Integer)redisTemplate.opsForValue().get("limit:"+token); if(count==null){ redisTemplate.opsForValue().set("limit:"+token, 1, 60, TimeUnit.SECONDS); return true; }else{ if(count>100 && count<150){ response.getWriter().write(JsonUtils.marshalToString(ReturnResult.build(500, "请求太频繁,请稍后再试"))); redisTemplate.opsForValue().increment("limit:"+token, 1); return false; }else if(count>=150){ redisTemplate.opsForSet().add("black",userId,2,TimeUnit.DAYS); response.getWriter().write(JsonUtils.marshalToString(ReturnResult.build(500, "请求太频繁,已经被限制访问"))); //redisTemplate.opsForSet().add("black",token); return false; }else{ redisTemplate.opsForValue().increment("limit:"+token, 1); return true; } } } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { // TODO Auto-generated method stub } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { // TODO Auto-generated method stub } }
上述内容就是怎么在spring boot中限制redis 接口的访问频率,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注创新互联行业资讯频道。
分享题目:怎么在springboot中限制redis接口的访问频率-创新互联
标题URL:http://ybzwz.com/article/iiicc.html