springboot项目实例_Springboot项目的接口防刷的实例
今天跟大家分享Springboot项目的接口防刷的实例的知识。
1 Springboot项目的接口防刷的实例
说明:使用了注解的方式进行对接口防刷的功能,非常高大上,本文章仅供参考。
技术要点: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 */@Componentpublic class FangshuaInterceptor extends HandlerInterceptorAdapter { @Autowired private RedisService redisService; @Override public 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){ //加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(); }}
再把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 */@Configurationpublic class WebConfig extends WebMvcConfigurerAdapter { @Autowired private FangshuaInterceptor interceptor; @Override public 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 */@Controllerpublic class FangshuaController { @AccessLimit(seconds=5, maxCount=5, needLogin=true) @RequestMapping("/fangshua") @ResponseBody public Result fangshua(){ return Result.success("请求成功"); }
今天就分享这么多,大家周末愉快!
如有收获请划至底部
点击“在看”支持,谢谢!
关注马士兵
每天分享技术干货
点赞是最大的支持
springboot项目实例_Springboot项目的接口防刷的实例相关推荐
- springboot项目实例_Springboot项目的接口防刷(实例)
来源:https://urlify.cn/ERf6Rr 说明:使用了注解的方式进行对接口防刷的功能,非常高大上,本文章仅供参考. 技术要点:springboot的基本知识,redis基本操作, 首先是 ...
- springboot接口防刷实现
本文主要介绍一种通过实现自定义注解,实现一种比较通用的接口防刷方式 前言 1.基本准备 jdk 8 redis springboot 2.7.6 2.基本思路 主要就是借助 redis 来实现接口的防 ...
- java抢购防止多次请求_springboot项目中接口防止恶意请求多次
springboot项目中接口防止恶意请求多次 在项目中,接口的暴露在外面,很多人就会恶意多次快速请求,那我们开发的接口和服务器在这样的频率下的话,服务器和数据库很快会奔溃的,那我们该怎么防止接口防刷 ...
- 优雅的接口防刷处理方案
点击上方"Java基基",选择"设为星标" 做积极的人,而不是积极废人! 每天 14:00 更新文章,每天掉亿点点头发... 源码精品专栏 原创 | Java ...
- Redis实现计数器---接口防刷---升级版(Redis+Lua)
[前言] Cash Loan(一):Redis实现计数器---接口防刷 中介绍了项目中应用redis来做计数器的实现过程,最近自己看了些关于Redis实现分布式锁的代码后,发现在Redis分布式锁中 ...
- java接口防刷_API 接口防刷
API 接口防刷 顾名思义,想让某个接口某个人在某段时间内只能请求N次. 在项目中比较常见的问题也有,那就是连点按钮导致请求多次,以前在web端有表单重复提交,可以通过token 来解决. 除了上面的 ...
- spring boot + redis 实现网站限流和接口防刷功能
源码url: https://github.com/zhzhair/accesslimit-spring-boot.git 注解@AccessLimit 实现接口防刷功能,在方法上的注解参数优先于类上 ...
- 创宇滤镜|API防刷|短信邮件接口防刷|验证码防刷|搜索防刷 - 知道创宇云安全
创宇滤镜|API防刷|短信邮件接口防刷|验证码防刷|搜索防刷 - 知道创宇云安全 创宇滤镜|API防刷|短信邮件接口防刷|验证码防刷|搜索防刷 - 知道创宇云安全 posted on 2017-02- ...
- Redis限流接口防刷
Redis限流接口防刷 Redis 除了做缓存,还能干很多很多事情:分布式锁.限流.处理请求接口幂等性...太多太多了- 大家好,我是llp,许久没有写博客了,今天就针对Redis实现接口限流做个记录 ...
最新文章
- 使用openpyxl处理表格数据
- D3.js学习(四)
- stl中各种容器的自定义比较函数
- mongodb性能分析方法:explain()
- SAP Spartacus的发布方式以及语义化版本管理机制
- P2014-选课【树形dp,背包】
- Greenplum 添加或删除standby master节点
- python函数定义时参数相当于占位符_python中函数的参数
- cmake安装更新(解决cmake报错:CMake 3.8 or higher is required. You are running version 3.5.1
- 现在当兵有什么待遇复原以后_当兵多少年最好呢?这些关键点会影响在部队发展,很重要、很实用...
- 1.简述计算机硬盘如何保养,电脑硬盘的保养知识
- 如何将原生PDF转为纯图像Word?
- JDK 和 JRE 有什么区别
- 传感器自学笔记第三章——LM393电压比较芯片+MQ_2烟雾传感器
- 排列组合算法的javascript实现
- LeetCode 714. 买卖股票的最佳时机含手续费
- 分享 25 个有用的 JS 单行代码
- 美国国家机器人计划2.0部分项目简介
- bootloader 详细介绍
- android 设置壁纸页面,Android实现手机壁纸改变的方法