SpringBoot项目的接口防刷功能

Java开发宝典

共 2375字,需浏览 5分钟

 ·

2020-12-05 23:59

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

首先是写一个注解类:

  1. import java.lang.annotation.Retention;

  2. import java.lang.annotation.Target;


  3. import static java.lang.annotation.ElementType.METHOD;

  4. import static java.lang.annotation.RetentionPolicy.RUNTIME;


  5. /**

  6. * @author yhq

  7. * @date 2018/9/10 15:52

  8. */


  9. @Retention(RUNTIME)

  10. @Target(METHOD)

  11. public @interface AccessLimit {


  12. int seconds();

  13. int maxCount();

  14. boolean needLogin()default true;

  15. }

拦截器中实现:

 

  1. import com.alibaba.fastjson.JSON;

  2. import com.example.demo.action.AccessLimit;

  3. import com.example.demo.redis.RedisService;

  4. import com.example.demo.result.CodeMsg;

  5. import com.example.demo.result.Result;

  6. import org.springframework.beans.factory.annotation.Autowired;

  7. import org.springframework.stereotype.Component;

  8. import org.springframework.web.method.HandlerMethod;

  9. import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;


  10. import javax.servlet.http.HttpServletRequest;

  11. import javax.servlet.http.HttpServletResponse;

  12. import java.io.OutputStream;


  13. /**

  14. * @author yhq

  15. * @date 2018/9/10 16:05

  16. */



  17. @Component

  18. public class FangshuaInterceptor extends HandlerInterceptorAdapter {


  19. @Autowired

  20. private RedisService redisService;


  21. @Override

  22. public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {


  23. //判断请求是否属于方法的请求

  24. if(handler instanceof HandlerMethod){


  25. HandlerMethod hm = (HandlerMethod) handler;


  26. //获取方法中的注解,看是否有该注解

  27. AccessLimit accessLimit = hm.getMethodAnnotation(AccessLimit.class);

  28. if(accessLimit == null){

  29. return true;

  30. }

  31. int seconds = accessLimit.seconds();

  32. int maxCount = accessLimit.maxCount();

  33. boolean login = accessLimit.needLogin();

  34. String key = request.getRequestURI();

  35. //如果需要登录

  36. if(login){

  37. //获取登录的session进行判断

  38. //.....

  39. key+=""+"1"; //这里假设用户是1,项目中是动态获取的userId

  40. }


  41. //从redis中获取用户访问的次数

  42. AccessKey ak = AccessKey.withExpire(seconds);

  43. Integer count = redisService.get(ak,key,Integer.class);

  44. if(count == null){

  45. //第一次访问

  46. redisService.set(ak,key,1);

  47. }else if(count < maxCount){

  48. //加1

  49. redisService.incr(ak,key);

  50. }else{

  51. //超出访问次数

  52. render(response,CodeMsg.ACCESS_LIMIT_REACHED); //这里的CodeMsg是一个返回参数

  53. return false;

  54. }

  55. }


  56. return true;


  57. }

  58. private void render(HttpServletResponse response, CodeMsg cm)throws Exception {

  59. response.setContentType("application/json;charset=UTF-8");

  60. OutputStream out = response.getOutputStream();

  61. String str = JSON.toJSONString(Result.error(cm));

  62. out.write(str.getBytes("UTF-8"));

  63. out.flush();

  64. out.close();

  65. }

  66. }

注册到springboot中

  1. import com.example.demo.ExceptionHander.FangshuaInterceptor;

  2. import org.springframework.beans.factory.annotation.Autowired;

  3. import org.springframework.context.annotation.Configuration;

  4. import org.springframework.web.servlet.config.annotation.InterceptorRegistry;

  5. import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;


  6. /**

  7. * @author yhq

  8. * @date 2018/9/10 15:58

  9. */

  10. @Configuration

  11. public class WebConfig extends WebMvcConfigurerAdapter {


  12. @Autowired

  13. private FangshuaInterceptor interceptor;



  14. @Override

  15. public void addInterceptors(InterceptorRegistry registry) {

  16. registry.addInterceptor(interceptor);

  17. }

  18. }

在Controller中加入注解

  1. import com.example.demo.result.Result;

  2. import org.springframework.stereotype.Controller;

  3. import org.springframework.web.bind.annotation.RequestMapping;

  4. import org.springframework.web.bind.annotation.ResponseBody;


  5. /**

  6. * @author yhq

  7. * @date 2018/9/10 15:49

  8. */


  9. @Controller

  10. public class FangshuaController {


  11. @AccessLimit(seconds=5, maxCount=5, needLogin=true)

  12. @RequestMapping("/fangshua")

  13. @ResponseBody

  14. public Result<String&gt; fangshua(){



  15. return Result.success("请求成功");

  16. }

作者:CS打赢你 原文:https://blog.csdn.net/weixin_42533856/article/details/82593123



    -END-

    我是武哥,最后给大家免费分享我写的 10 万字 Spring Boot 学习笔记(带完整目录)以及对应的源码。这是我之前在 CSDN 开的一门课,所以笔记非常详细完整,我准备将资料分享出来给大家免费学习,相信大家看完一定会有所收获(下面有下载方式)。


    可以看出,我当时备课非常详细,目录非常完整,读者可以手把手跟着笔记,结合源代码来学习。现在免费分享出来,有需要的读者可以下载学习,就在我下面的公众号Java秃头哥里回复:笔记,就行。



    如有文章对你有帮助,

    在看转发是对我最大的支持



    关注Java秃头哥

    只有秃头才能更强


    点赞是最大的支持 

浏览 10
点赞
评论
收藏
分享

手机扫一扫分享

举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

举报