<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          SpringBoot項目的接口防刷功能

          共 2375字,需瀏覽 5分鐘

           ·

          2020-12-05 23:59

          說明:使用了注解的方式進行對接口防刷的功能,非常高大上,本文章僅供參考 一,技術(shù)要點: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. }

          攔截器中實現(xiàn):

          ?

          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"; //這里假設(shè)用戶是1,項目中是動態(tài)獲取的userId

          40. }


          41. //從redis中獲取用戶訪問的次數(shù)

          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. //超出訪問次數(shù)

          52. render(response,CodeMsg.ACCESS_LIMIT_REACHED); //這里的CodeMsg是一個返回參數(shù)

          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 學(xué)習(xí)筆記(帶完整目錄)以及對應(yīng)的源碼。這是我之前在 CSDN 開的一門課,所以筆記非常詳細(xì)完整,我準(zhǔn)備將資料分享出來給大家免費學(xué)習(xí),相信大家看完一定會有所收獲(下面有下載方式)。


            可以看出,我當(dāng)時備課非常詳細(xì),目錄非常完整,讀者可以手把手跟著筆記,結(jié)合源代碼來學(xué)習(xí)。現(xiàn)在免費分享出來,有需要的讀者可以下載學(xué)習(xí),就在我下面的公眾號Java禿頭哥里回復(fù):筆記,就行。



            如有文章對你有幫助,

            在看轉(zhuǎn)發(fā)是對我最大的支持



            關(guān)注Java禿頭哥

            只有禿頭才能更強


            點贊是最大的支持?

          瀏覽 20
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  久久久久久蜜桃 | 亚洲日韩精品一区 | 九九九精品影视 | 北条麻妃中文字幕一区 | 婷婷丁香激情综合 |