0
  • 聊天消息
  • 系統(tǒng)消息
  • 評(píng)論與回復(fù)
登錄后你可以
  • 下載海量資料
  • 學(xué)習(xí)在線課程
  • 觀看技術(shù)視頻
  • 寫文章/發(fā)帖/加入社區(qū)
會(huì)員中心
創(chuàng)作中心

完善資料讓更多小伙伴認(rèn)識(shí)你,還能領(lǐng)取20積分哦,立即完善>

3天內(nèi)不再提示

一個(gè)注解搞定SpringBoot接口防刷

jf_ro2CN3Fa ? 來源:CSDN ? 2023-11-28 10:46 ? 次閱讀

一,技術(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)求成功");

}








審核編輯:劉清

聲明:本文內(nèi)容及配圖由入駐作者撰寫或者入駐合作網(wǎng)站授權(quán)轉(zhuǎn)載。文章觀點(diǎn)僅代表作者本人,不代表電子發(fā)燒友網(wǎng)立場。文章及其配圖僅供工程師學(xué)習(xí)之用,如有內(nèi)容侵權(quán)或者其他違規(guī)問題,請(qǐng)聯(lián)系本站處理。 舉報(bào)投訴
  • 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)注明出處。

收藏 人收藏

    評(píng)論

    相關(guān)推薦

    Spring Boot的注解原理是什么

    @SpringBootApplication來看,發(fā)現(xiàn)@SpringBootApplication是個(gè)組合注解。 @Target(ElementType.TYPE) @Retention
    的頭像 發(fā)表于 08-27 09:24 ?2118次閱讀

    教你如何用個(gè)注解搞定Spring Boot接口

    ,技術(shù)要點(diǎn): Spring Boot的基本知識(shí),Redis基本操作,首先是寫個(gè)注解類: import java.lang.annotation.Retention; import
    的頭像 發(fā)表于 09-13 09:23 ?1705次閱讀

    Spring Boot常用注解與使用方式

    企業(yè)開發(fā)項(xiàng)目SpringBoot已經(jīng)是必備框架了,其中注解是開發(fā)中的小工具(誰處可見哦),用好了開發(fā)效率大大提升,當(dāng)然用錯(cuò)了也會(huì)引入缺陷。
    的頭像 發(fā)表于 07-08 10:57 ?1260次閱讀

    SpringBoot定時(shí)任務(wù)動(dòng)態(tài)管理通用解決方案

    SpringBoot的定時(shí)任務(wù)的加強(qiáng)工具,實(shí)現(xiàn)對(duì)SpringBoot原生的定時(shí)任務(wù)進(jìn)行動(dòng)態(tài)管理,完全兼容原生@Scheduled注解,無需對(duì)原本的定時(shí)任務(wù)進(jìn)行修改
    的頭像 發(fā)表于 02-03 09:49 ?691次閱讀

    個(gè)無需注解SpringBoot API文檔生成神器

    如果提交的表單是 application/x-www-form-urlencoded 類型的key/value格式,你可以在 SpringBoot 端通過在 @param 參數(shù)后添加字段解釋或者在相關(guān)的JavaBean對(duì)象里面添加解釋:
    的頭像 發(fā)表于 03-13 09:38 ?833次閱讀

    分享種優(yōu)雅的接口處理方案

    通過在Interceptor中攔截請(qǐng)求,從Redis中統(tǒng)計(jì)用戶訪問接口次數(shù)從而達(dá)到接口目的
    的頭像 發(fā)表于 03-29 14:56 ?685次閱讀

    什么是 SpringBoot?

    本文從為什么要有 `SpringBoot`,以及 `SpringBoot` 到底方便在哪里開始入手,逐步分析了 `SpringBoot` 自動(dòng)裝配的原理,最后手寫了
    的頭像 發(fā)表于 04-07 11:28 ?1181次閱讀
    什么是 <b class='flag-5'>SpringBoot</b>?

    SpringBoot常用注解及使用方法1

    基于 SpringBoot 平臺(tái)開發(fā)的項(xiàng)目數(shù)不勝數(shù),與常規(guī)的基于`Spring`開發(fā)的項(xiàng)目最大的不同之處,SpringBoot 里面提供了大量的注解用于快速開發(fā),而且非常簡單,基本可以做到開箱即用! 那
    的頭像 發(fā)表于 04-07 11:51 ?587次閱讀

    SpringBoot常用注解及使用方法2

    基于 SpringBoot 平臺(tái)開發(fā)的項(xiàng)目數(shù)不勝數(shù),與常規(guī)的基于Spring開發(fā)的項(xiàng)目最大的不同之處,SpringBoot 里面提供了大量的注解用于快速開發(fā),而且非常簡單,基本可以做到開箱即用!
    的頭像 發(fā)表于 04-07 11:52 ?554次閱讀

    Springboot常用注解合集

    前幾章,在系統(tǒng)啟動(dòng)類里面,都加入了此啟動(dòng)注解,此注解個(gè)組合注解,包括了`@SpringBootConfiguration`、`@EnableAutoConfiguration`和`@
    的頭像 發(fā)表于 04-07 14:27 ?646次閱讀
    <b class='flag-5'>Springboot</b>常用<b class='flag-5'>注解</b>合集

    SpringBoot常用注解及原理

    SpringBootConfiguration繼承自@Configuration,二者功能也致,標(biāo)注當(dāng)前類是配置類, 并會(huì)將當(dāng)前類內(nèi)聲明的個(gè)或多個(gè)以@Bean注解標(biāo)記的方法的實(shí)例納
    的頭像 發(fā)表于 04-07 14:30 ?509次閱讀

    SpringBoot的核心注解1

    今天跟大家來探討下SpringBoot的核心注解@SpringBootApplication以及run方法,理解下springBoot為什么不需要XML,達(dá)到零配置
    的頭像 發(fā)表于 04-07 14:34 ?607次閱讀
    <b class='flag-5'>SpringBoot</b>的核心<b class='flag-5'>注解</b>1

    SpringBoot的核心注解2

    今天跟大家來探討下SpringBoot的核心注解@SpringBootApplication以及run方法,理解下springBoot為什么不需要XML,達(dá)到零配置
    的頭像 發(fā)表于 04-07 14:34 ?1870次閱讀
    <b class='flag-5'>SpringBoot</b>的核心<b class='flag-5'>注解</b>2

    springboot核心注解

    Spring Boot 是基于 Spring 框架的開源框架,它可以幫助開發(fā)者快速構(gòu)建、部署和運(yùn)行獨(dú)立的、生產(chǎn)級(jí)的 Spring 應(yīng)用程序。Spring Boot 提供了系列核心注解,這些注解可以
    的頭像 發(fā)表于 11-23 09:23 ?425次閱讀

    SpringBoot核心注解由幾個(gè)注解組成

    等。本文將詳盡介紹這些核心注解。 @SpringBootApplication @SpringBootApplication 是個(gè)復(fù)合注解,包含了 @Configuration、@
    的頭像 發(fā)表于 12-03 15:09 ?564次閱讀