点击关注公众号,Java干货及时送达

说明:使用了注解的方式进行对接口防刷的功能,非常高大上,本文章仅供参考

一,技术要点:springboot的基本知识,redis基本操作,

首先是写一个注解类:


import java.lang.annotation.Retention;
import java.lang.annotation.Target;import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;/*** @author yhq* @date 2018/9/10 15:52*/@Retention(RUNTIME)
@Target(METHOD)
public @interface AccessLimit {int seconds();int maxCount();boolean needLogin()default true;
}

接着就是在Interceptor拦截器中实现:


import com.alibaba.fastjson.JSON;
import com.example.demo.action.AccessLimit;
import com.example.demo.redis.RedisService;
import com.example.demo.result.CodeMsg;
import com.example.demo.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;/*** @author yhq* @date 2018/9/10 16:05*/@Component
public class FangshuaInterceptor extends HandlerInterceptorAdapter {@Autowiredprivate RedisService redisService;@Overridepublic boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {//判断请求是否属于方法的请求if(handler instanceof HandlerMethod){HandlerMethod hm = (HandlerMethod) handler;//获取方法中的注解,看是否有该注解AccessLimit accessLimit = hm.getMethodAnnotation(AccessLimit.class);if(accessLimit == null){return true;}int seconds = accessLimit.seconds();int maxCount = accessLimit.maxCount();boolean login = accessLimit.needLogin();String key = request.getRequestURI();//如果需要登录if(login){//获取登录的session进行判断//.....key+=""+"1";  //这里假设用户是1,项目中是动态获取的userId}//从redis中获取用户访问的次数AccessKey ak = AccessKey.withExpire(seconds);Integer count = redisService.get(ak,key,Integer.class);if(count == null){//第一次访问redisService.set(ak,key,1);}else if(count < maxCount){//加1redisService.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();}
}

再把Interceptor注册到springboot中


import com.example.demo.ExceptionHander.FangshuaInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;/*** @author yhq* @date 2018/9/10 15:58*/
@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {@Autowiredprivate FangshuaInterceptor interceptor;@Overridepublic void addInterceptors(InterceptorRegistry registry) {registry.addInterceptor(interceptor);}
}

接着在Controller中加入注解

import com.example.demo.result.Result;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;/*** @author yhq* @date 2018/9/10 15:49*/@Controller
public class FangshuaController {@AccessLimit(seconds=5, maxCount=5, needLogin=true)@RequestMapping("/fangshua")@ResponseBodypublic Result<String> fangshua(){return Result.success("请求成功");}

本文有参考其他视频的教学,希望可以帮助更多热爱it行业的人,

作者:CS打赢你
本文链接:https://blog.csdn.net/weixin_42533856/article/details/82593123
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

热门内容:23 种设计模式的通俗解释,看完秒懂
token多平台身份认证架构设计思路
最近 GitHub 访问很慢?
好家伙!JDK16 GA 终于发布,内置 Lombok 的功能,真的顶?
最近面试BAT,整理一份面试资料《Java面试BAT通关手册》,覆盖了Java核心技术、JVM、Java并发、SSM、微服务、数据库、数据结构等等。获取方式:点“在看”,关注公众号并回复 666 领取,更多内容陆续奉上。

明天见(。・ω・。)ノ♡

一个注解搞定接口防刷!还有谁不会?相关推荐

  1. 后端技术:一个注解解决 SpringBoot 接口防刷

    说明:使用了注解的方式进行对接口防刷的功能,非常高大上,本文章仅供参考 技术要点:springboot的基本知识,redis基本操作, 首先是写一个注解类: import java.lang.anno ...

  2. 一个注解搞定 SpringBoot 接口防刷,还有谁不会?

    点击上方蓝色"方志朋",选择"设为星标" 回复"666"获取独家整理的学习资料! 作者:CS打赢你 blog.csdn.net/weixin ...

  3. 数据翻译的代码辅助插件,一个注解搞定,减少30%SQL代码量

    一.开源项目简介 Easy Trans是一款用于做数据翻译的代码辅助插件,利用MyBatis Plus/JPA/BeetlSQL 等ORM框架的能力自动查表,让开发者可以快速的把ID/字典码 翻译为前 ...

  4. Java后端:一个注解搞定 Spring Boot 日志!

    此组件解决的问题是: 「谁」在「什么时间」对「什么」做了「什么事」 本组件目前针对 Spring-boot 做了 Autoconfig,如果是 SpringMVC,也可自己在 xml 初始化 bean ...

  5. Android线程创建aop,【android安卓】一个注解搞定线程切换,基于AOP的线程转换框架...

    最简单的使用方法: 模拟进度展示: @RunOnIOThread public void progress() { for (int i = 0; i <= 100; i++) { showPr ...

  6. spring boot + redis 实现网站限流和接口防刷功能

    源码url: https://github.com/zhzhair/accesslimit-spring-boot.git 注解@AccessLimit 实现接口防刷功能,在方法上的注解参数优先于类上 ...

  7. Java接口防刷策略(自定义注解实现)

    前言 本文一定要看完,前部分为逻辑说明及简单实现,文章最后有最终版解决方案(基于lua脚本),因为前部分是防君子不防小人,无法抵挡for循环调用. 目的 短信发送及短信验证码校验接口防刷 一方面防止用 ...

  8. php微信sdk接口文档,php一个文件搞定微信jssdk配置

    学习PHP的小伙伴在处理微信jssdk配置的时候可能会遇到一些问题.以下是百分网小编精心为大家整理的php一个文件搞定微信jssdk配置,希望对大家有所帮助!更多内容请关注应届毕业生网! 包括缓存,包 ...

  9. 优雅的接口防刷处理方案

    点击上方"Java基基",选择"设为星标" 做积极的人,而不是积极废人! 每天 14:00 更新文章,每天掉亿点点头发... 源码精品专栏 原创 | Java ...

最新文章

  1. FPGA在人工智能时代的独特优势
  2. 扩增子图表解读7三元图:三组差异数量和关系
  3. 白话——胡说图像分类器
  4. 11.6 如何使用内嵌资源类(ResourceRetriever)?
  5. docker kaniko push推送镜像至harbor报错:x509: certificate signed by unknown authority(命令中添加 --skip-tls-ver)
  6. CodeForces - 375D Tree and Queries(树上启发式合并)
  7. vpr文件转换flac_关于便携播放器音频格式转换的问题
  8. SwiftUI优秀文章经典案例制作简易的新闻列表Demo
  9. 具有代理设置的Spring Cloud AWS
  10. Quantumas,作者太NB了,俺发现俺菜得跟猪一样!!!
  11. 【机器人学与计算机视觉基础】(一)位置与姿态描述 1 位姿的抽象符号表示
  12. 论“天才球员”有多重要!
  13. Shell笔记5——函数的知识与实践
  14. PWA 应用列表及常用工具
  15. 联通光猫IPV6配置
  16. 品牌出海:如何做好本土化运营?
  17. 人工智能时代下,Python与C/C++谁将成为人工智能核心算法选择?
  18. textarea中的内容保存与显示时换行符的处理方法
  19. Eclipse导出JavaDoc中文乱码问题解决
  20. 蓝桥杯:互质数及其定义

热门文章

  1. Java绘图之AWT中的继承关系图
  2. Python常用函数--文档字符串DocStrings
  3. java自学 day1
  4. Codeforces Round #308 (Div. 2) C. Vanya and Scales dfs
  5. dos下命令行执行程序时候注意程序所使用文件的路径问题
  6. centos 网卡聚合及Cisco交换机链路聚合
  7. C++类的静态成员详细讲解
  8. html超链接button
  9. SQL Server 2005 18452登录错误 的解决方法
  10. 关系数据理论中的范式