一,技术要点:springboot的基本知识,redis基本操作,
首先是写一个注解类:
importjava.lang.annotation.Retention; importjava.lang.annotation.Target; importstaticjava.lang.annotation.ElementType.METHOD; importstaticjava.lang.annotation.RetentionPolicy.RUNTIME; /** *@authoryhq *@date2018/9/1015:52 */ @Retention(RUNTIME) @Target(METHOD) public@interfaceAccessLimit{ intseconds(); intmaxCount(); booleanneedLogin()defaulttrue; }
拦截器中实现:
importcom.alibaba.fastjson.JSON; importcom.example.demo.action.AccessLimit; importcom.example.demo.redis.RedisService; importcom.example.demo.result.CodeMsg; importcom.example.demo.result.Result; importorg.springframework.beans.factory.annotation.Autowired; importorg.springframework.stereotype.Component; importorg.springframework.web.method.HandlerMethod; importorg.springframework.web.servlet.handler.HandlerInterceptorAdapter; importjavax.servlet.http.HttpServletRequest; importjavax.servlet.http.HttpServletResponse; importjava.io.OutputStream; /** *@authoryhq *@date2018/9/1016:05 */ @Component publicclassFangshuaInterceptorextendsHandlerInterceptorAdapter{ @Autowired privateRedisServiceredisService; @Override publicbooleanpreHandle(HttpServletRequestrequest,HttpServletResponseresponse,Objecthandler)throwsException{ //判断请求是否属于方法的请求 if(handlerinstanceofHandlerMethod){ HandlerMethodhm=(HandlerMethod)handler; //获取方法中的注解,看是否有该注解 AccessLimitaccessLimit=hm.getMethodAnnotation(AccessLimit.class); if(accessLimit==null){ returntrue; } intseconds=accessLimit.seconds(); intmaxCount=accessLimit.maxCount(); booleanlogin=accessLimit.needLogin(); Stringkey=request.getRequestURI(); //如果需要登录 if(login){ //获取登录的session进行判断 //..... key+=""+"1";//这里假设用户是1,项目中是动态获取的userId } //从redis中获取用户访问的次数 AccessKeyak=AccessKey.withExpire(seconds); Integercount=redisService.get(ak,key,Integer.class); if(count==null){ //第一次访问 redisService.set(ak,key,1); }elseif(count< maxCount){ //加1 redisService.incr(ak,key); }else{ //超出访问次数 render(response,CodeMsg.ACCESS_LIMIT_REACHED); //这里的CodeMsg是一个返回参数 return false; } } return true; } private void render(HttpServletResponse response, CodeMsg cm)throws Exception { response.setContentType("application/json;charset=UTF-8"); OutputStream out = response.getOutputStream(); String str = JSON.toJSONString(Result.error(cm)); out.write(str.getBytes("UTF-8")); out.flush(); out.close(); } }
注册到springboot中
importcom.example.demo.ExceptionHander.FangshuaInterceptor; importorg.springframework.beans.factory.annotation.Autowired; importorg.springframework.context.annotation.Configuration; importorg.springframework.web.servlet.config.annotation.InterceptorRegistry; importorg.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; /** *@authoryhq *@date2018/9/1015:58 */ @Configuration publicclassWebConfigextendsWebMvcConfigurerAdapter{ @Autowired privateFangshuaInterceptorinterceptor; @Override publicvoidaddInterceptors(InterceptorRegistryregistry){ registry.addInterceptor(interceptor); } }
在Controller中加入注解
importcom.example.demo.result.Result; importorg.springframework.stereotype.Controller; importorg.springframework.web.bind.annotation.RequestMapping; importorg.springframework.web.bind.annotation.ResponseBody; /** *@authoryhq *@date2018/9/1015:49 */ @Controller publicclassFangshuaController{ @AccessLimit(seconds=5,maxCount=5,needLogin=true) @RequestMapping("/fangshua") @ResponseBody publicResultfangshua(){ returnResult.success("请求成功"); }
审核编辑:刘清
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。
举报投诉
-
JAVA
+关注
关注
19文章
2904浏览量
102996 -
Framework
+关注
关注
0文章
23浏览量
8543
原文标题:一个注解搞定 SpringBoot 接口防刷,还有谁不会?
文章出处:【微信号:芋道源码,微信公众号:芋道源码】欢迎添加关注!文章转载请注明出处。
发布评论请先 登录
相关推荐
为什么要给PCB刷三防漆?
接插件不用喷漆。贴胶纸太慢而且撕的时候有太多残留的胶,可考虑按接插件形状、大小、位置,做一个组合式罩子,用安装孔定位。罩住不用喷漆部位。三防漆操作工艺要求1、 清洁和烘板,除去潮气和水分。须先将欲涂物件
发表于 12-17 08:57
SpringBoot应用启动运行run方法
)、refreshContext(context);SpringBoot刷新IOC容器【创建IOC容器对象,并初始化容器,创建容器中的每一个组件】;如果是web应用创建**AnnotationConfigEmbeddedWebApplicationContext**,否则
发表于 12-20 06:16
Springboot是如何获取自定义异常并进行返回的
/DispatcherServlet.java的源码,然后我们来分析一下这个方法都干啥了吧那Springboot是如何选择哪一个是符合条件的自定义异常处理呢?如果我们定义了两个处理类,
发表于 03-22 14:15
Spring Boot常用注解与使用方式
企业开发项目SpringBoot已经是必备框架了,其中注解是开发中的小工具(谁处可见哦),用好了开发效率大大提升,当然用错了也会引入缺陷。
求一种SpringBoot定时任务动态管理通用解决方案
SpringBoot的定时任务的加强工具,实现对SpringBoot原生的定时任务进行动态管理,完全兼容原生@Scheduled注解,无需对原本的定时任务进行修改
一个无需注解的SpringBoot API文档生成神器
如果提交的表单是 application/x-www-form-urlencoded 类型的key/value格式,你可以在 SpringBoot 端通过在 @param 参数后添加字段解释或者在相关的JavaBean对象里面添加解释:
什么是 SpringBoot?
本文从为什么要有 `SpringBoot`,以及 `SpringBoot` 到底方便在哪里开始入手,逐步分析了 `SpringBoot` 自动装配的原理,最后手写了一个简单的 `start` 组件,通过实战来体会了 `
SpringBoot常用注解及使用方法1
基于 SpringBoot 平台开发的项目数不胜数,与常规的基于`Spring`开发的项目最大的不同之处,SpringBoot 里面提供了大量的注解用于快速开发,而且非常简单,基本可以做到开箱即用!
那
SpringBoot常用注解及使用方法2
基于 SpringBoot 平台开发的项目数不胜数,与常规的基于Spring开发的项目最大的不同之处,SpringBoot 里面提供了大量的注解用于快速开发,而且非常简单,基本可以做到开箱即用!
Springboot常用注解合集
前几章,在系统启动类里面,都加入了此启动注解,此注解是个组合注解,包括了`@SpringBootConfiguration`、`@EnableAutoConfiguration`和`@ComponentScan`
SpringBoot常用注解及原理
SpringBootConfiguration继承自@Configuration,二者功能也一致,标注当前类是配置类, 并会将当前类内声明的一个或多个以@Bean注解标记的方法的实例纳入到spring容器中,并且实例名就是方法名。
springboot核心注解
Spring Boot 是基于 Spring 框架的开源框架,它可以帮助开发者快速构建、部署和运行独立的、生产级的 Spring 应用程序。Spring Boot 提供了一系列核心注解,这些注解可以
SpringBoot核心注解由几个注解组成
简化应用程序开发的注解,其中核心注解包括 @SpringBootApplication、@RestController、@RequestMapping、@Autowired、@ComponentScan
评论