一,技術(shù)要點(diǎn):springboot的基本知識,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{ //判斷請求是否屬于方法的請求 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)目中是動態(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
注冊到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("請求成功"); }
審核編輯:劉清
聲明:本文內(nèi)容及配圖由入駐作者撰寫或者入駐合作網(wǎng)站授權(quán)轉(zhuǎn)載。文章觀點(diǎn)僅代表作者本人,不代表電子發(fā)燒友網(wǎng)立場。文章及其配圖僅供工程師學(xué)習(xí)之用,如有內(nèi)容侵權(quán)或者其他違規(guī)問題,請聯(lián)系本站處理。
舉報(bào)投訴
-
JAVA
+關(guān)注
關(guān)注
19文章
2946瀏覽量
104362 -
Framework
+關(guān)注
關(guān)注
0文章
24瀏覽量
8565
原文標(biāo)題:一個(gè)注解搞定 SpringBoot 接口防刷,還有誰不會?
文章出處:【微信號:芋道源碼,微信公眾號:芋道源碼】歡迎添加關(guān)注!文章轉(zhuǎn)載請注明出處。
發(fā)布評論請先 登錄
相關(guān)推薦
Spring Boot的注解原理是什么
@SpringBootApplication來看,發(fā)現(xiàn)@SpringBootApplication是一個(gè)組合注解。 @Target(ElementType.TYPE) @Retention
教你如何用一個(gè)注解搞定Spring Boot接口防刷
一,技術(shù)要點(diǎn): Spring Boot的基本知識,Redis基本操作,首先是寫一個(gè)注解類: import java.lang.annotation.Retention; import
Spring Boot常用注解與使用方式
企業(yè)開發(fā)項(xiàng)目SpringBoot已經(jīng)是必備框架了,其中注解是開發(fā)中的小工具(誰處可見哦),用好了開發(fā)效率大大提升,當(dāng)然用錯(cuò)了也會引入缺陷。
求一種SpringBoot定時(shí)任務(wù)動態(tài)管理通用解決方案
SpringBoot的定時(shí)任務(wù)的加強(qiáng)工具,實(shí)現(xiàn)對SpringBoot原生的定時(shí)任務(wù)進(jìn)行動態(tài)管理,完全兼容原生@Scheduled注解,無需對原本的定時(shí)任務(wù)進(jìn)行修改
一個(gè)無需注解的SpringBoot API文檔生成神器
如果提交的表單是 application/x-www-form-urlencoded 類型的key/value格式,你可以在 SpringBoot 端通過在 @param 參數(shù)后添加字段解釋或者在相關(guān)的JavaBean對象里面添加解釋:
分享一種優(yōu)雅的接口防刷處理方案
通過在Interceptor中攔截請求,從Redis中統(tǒng)計(jì)用戶訪問接口次數(shù)從而達(dá)到接口防刷目的
SpringBoot常用注解及使用方法1
基于 SpringBoot 平臺開發(fā)的項(xiàng)目數(shù)不勝數(shù),與常規(guī)的基于`Spring`開發(fā)的項(xiàng)目最大的不同之處,SpringBoot 里面提供了大量的注解用于快速開發(fā),而且非常簡單,基本可以做到開箱即用!
那
SpringBoot常用注解及使用方法2
基于 SpringBoot 平臺開發(fā)的項(xiàng)目數(shù)不勝數(shù),與常規(guī)的基于Spring開發(fā)的項(xiàng)目最大的不同之處,SpringBoot 里面提供了大量的注解用于快速開發(fā),而且非常簡單,基本可以做到開箱即用!
Springboot常用注解合集
前幾章,在系統(tǒng)啟動類里面,都加入了此啟動注解,此注解是個(gè)組合注解,包括了`@SpringBootConfiguration`、`@EnableAutoConfiguration`和`@
SpringBoot常用注解及原理
SpringBootConfiguration繼承自@Configuration,二者功能也一致,標(biāo)注當(dāng)前類是配置類, 并會將當(dāng)前類內(nèi)聲明的一個(gè)或多個(gè)以@Bean注解標(biāo)記的方法的實(shí)例納
SpringBoot的核心注解1
今天跟大家來探討下SpringBoot的核心注解@SpringBootApplication以及run方法,理解下springBoot為什么不需要XML,達(dá)到零配置
SpringBoot的核心注解2
今天跟大家來探討下SpringBoot的核心注解@SpringBootApplication以及run方法,理解下springBoot為什么不需要XML,達(dá)到零配置
springboot核心注解
Spring Boot 是基于 Spring 框架的開源框架,它可以幫助開發(fā)者快速構(gòu)建、部署和運(yùn)行獨(dú)立的、生產(chǎn)級的 Spring 應(yīng)用程序。Spring Boot 提供了一系列核心注解,這些注解可以
SpringBoot核心注解由幾個(gè)注解組成
等。本文將詳盡介紹這些核心注解。 @SpringBootApplication @SpringBootApplication 是一個(gè)復(fù)合注解,包含了 @Configuration、@
評論