一,技術(shù)要點(diǎn):springboot的基本知識(shí),redis基本操作,
首先是寫一個(gè)注解類:
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; }
攔截器中實(shí)現(xiàn):
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{ //判斷請(qǐng)求是否屬于方法的請(qǐng)求 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進(jìn)行判斷 //..... key+=""+"1";//這里假設(shè)用戶是1,項(xiàng)目中是動(dòng)態(tài)獲取的userId } //從redis中獲取用戶訪問的次數(shù) AccessKeyak=AccessKey.withExpire(seconds); Integercount=redisService.get(ak,key,Integer.class); if(count==null){ //第一次訪問 redisService.set(ak,key,1); }elseif(count
注冊(cè)到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("請(qǐng)求成功"); }
審核編輯:劉清
-
JAVA
+關(guān)注
關(guān)注
19文章
2943瀏覽量
104089 -
Framework
+關(guān)注
關(guān)注
0文章
24瀏覽量
8563
原文標(biāo)題:一個(gè)注解搞定 SpringBoot 接口防刷,還有誰不會(huì)?
文章出處:【微信號(hào):芋道源碼,微信公眾號(hào):芋道源碼】歡迎添加關(guān)注!文章轉(zhuǎn)載請(qǐng)注明出處。
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
評(píng)論