面试官:如何做 API 接口防刷??
点击上方“方志朋”,选择“设为星标”
回复”666“获取新整理的面试资料
来源:blog.csdn.net/weixin_42533856/article/details/82593123
首先是写一个注解类
拦截器中实现
注册到springboot中
在Controller中加入注解
说明:使用了注解的方式进行对接口防刷的功能,非常高大上,本文章仅供参考 一,技术要点: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;
}
拦截器中实现:
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();}
}
注册到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("请求成功");}
热门内容:
面经:为了拿到字节跳动offer,鬼知道我经历了啥...
Spring Boot + MyBatis + Druid + PageHelper 实现多数据源并分页
是时候扔掉Postman了,又一个被低估的IDEA插件出来了...
Spring集成任务调度功能
看完知乎轮子哥的编程之路,我只想说,收下我的膝盖...
这是我读过写得最好的【秒杀系统架构】分析与实战!
Springboot总结,核心功能,优缺点
如何设计 API 接口,实现统一格式返回?
阿里巴巴为什么能抗住90秒100亿?看完这篇你就明白了!
最近面试BAT,整理一份面试资料《Java面试BAT通关手册》,覆盖了Java核心技术、JVM、Java并发、SSM、微服务、数据库、数据结构等等。
获取方式:点“在看”,关注公众号并回复 666 领取,更多内容陆续奉上。
明天见(。・ω・。)ノ♡
面试官:如何做 API 接口防刷??相关推荐
- Spring Boot 项目的 API 接口防刷
说明:使用了注解的方式进行对接口防刷的功能,非常高大上,本文章仅供参考 一,技术要点:springboot的基本知识,redis基本操作, 首先是写一个注解类: import java.lang.an ...
- java接口防刷_API 接口防刷
API 接口防刷 顾名思义,想让某个接口某个人在某段时间内只能请求N次. 在项目中比较常见的问题也有,那就是连点按钮导致请求多次,以前在web端有表单重复提交,可以通过token 来解决. 除了上面的 ...
- 接口安全----接口防刷
私有:需要企业内部资源共享 内部接口 公有:短信平台,天气预报.... 接口安全要求: 1.防伪装攻击处理方式:接口防刷出现情况:公共网络环境中,第三方有意或者恶意调用我们的接口2.防篡改攻击处理方式 ...
- 创宇滤镜|API防刷|短信邮件接口防刷|验证码防刷|搜索防刷 - 知道创宇云安全
创宇滤镜|API防刷|短信邮件接口防刷|验证码防刷|搜索防刷 - 知道创宇云安全 创宇滤镜|API防刷|短信邮件接口防刷|验证码防刷|搜索防刷 - 知道创宇云安全 posted on 2017-02- ...
- 优雅的接口防刷处理方案
点击上方"Java基基",选择"设为星标" 做积极的人,而不是积极废人! 每天 14:00 更新文章,每天掉亿点点头发... 源码精品专栏 原创 | Java ...
- Redis实现计数器---接口防刷---升级版(Redis+Lua)
[前言] Cash Loan(一):Redis实现计数器---接口防刷 中介绍了项目中应用redis来做计数器的实现过程,最近自己看了些关于Redis实现分布式锁的代码后,发现在Redis分布式锁中 ...
- Redis限流接口防刷
Redis限流接口防刷 Redis 除了做缓存,还能干很多很多事情:分布式锁.限流.处理请求接口幂等性...太多太多了- 大家好,我是llp,许久没有写博客了,今天就针对Redis实现接口限流做个记录 ...
- Java接口防刷策略(自定义注解实现)
前言 本文一定要看完,前部分为逻辑说明及简单实现,文章最后有最终版解决方案(基于lua脚本),因为前部分是防君子不防小人,无法抵挡for循环调用. 目的 短信发送及短信验证码校验接口防刷 一方面防止用 ...
- JSON API强大的WordPress做API接口插件
多数的博客系统都是使用WordPress搭建的,而作为博客系统,可能我们需要在站外调用博客的文章,请求这个API接口,可以获取最新文章列表,一般通常的做法是新建一个PHP文件,接收几个参数,查询数据库 ...
最新文章
- python 的时间模块使用
- COCO数据格式说明
- 把你的 VS Code 打造成 C++ 开发利器
- Hbase 01_初学必知
- SAP WebIDE里的JavaScript代码检查code check
- python 子串是否在字符串中_python七种方法判断字符串是否包含子串
- leetcode64 最小路径和
- leetcode 高薪_LeetCode 第 125 号问题:验证回文串
- html5 上传 原理,浅谈使用HTML5的FormData上传文件原理!!!
- MySQL order by、group by关键字优化
- 华为员工能拿多少钱,揭秘一个真实的华为
- log2 3怎样用计算机打出,红警在局域网怎么样才可以2个人打多个电脑玩家?要打3个电脑以上的...
- 计算机图形学:中点划线法(任意斜率)
- 【_ 面試 】在单点登录中,如果 cookie 被禁用了怎么办?
- Unity3D 与 3DMax 结合开发
- ERP系统-销售子系统-销售订单
- 怒怼软件测测试不良培训机构!痛斥招转培!
- Centos7搭建cisco ocserv
- cadence导出特定格式的BOM表
- PyScript运行Python第三方库