首先是写一个注解类:

  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. }