PageRenderTime 40ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/framework/src/main/java/com/ruoyi/framework/interceptor/impl/SameUrlDataInterceptor.java

https://gitlab.com/mrsunchangemyselfsun/ruoyi-vue
Java | 110 lines | 80 code | 13 blank | 17 comment | 8 complexity | 435cb063105c677e8d8b78508f6e1adc MD5 | raw file
  1. package com.ruoyi.framework.interceptor.impl;
  2. import java.util.HashMap;
  3. import java.util.Map;
  4. import java.util.concurrent.TimeUnit;
  5. import javax.servlet.http.HttpServletRequest;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.beans.factory.annotation.Value;
  8. import org.springframework.stereotype.Component;
  9. import com.alibaba.fastjson.JSONObject;
  10. import com.ruoyi.common.annotation.RepeatSubmit;
  11. import com.ruoyi.common.constant.Constants;
  12. import com.ruoyi.common.core.redis.RedisCache;
  13. import com.ruoyi.common.filter.RepeatedlyRequestWrapper;
  14. import com.ruoyi.common.utils.StringUtils;
  15. import com.ruoyi.common.utils.http.HttpHelper;
  16. import com.ruoyi.framework.interceptor.RepeatSubmitInterceptor;
  17. /**
  18. * 判断请求url和数据是否和上一次相同,
  19. * 如果和上次相同,则是重复提交表单。 有效时间为10秒内。
  20. *
  21. * @author ruoyi
  22. */
  23. @Component
  24. public class SameUrlDataInterceptor extends RepeatSubmitInterceptor
  25. {
  26. public final String REPEAT_PARAMS = "repeatParams";
  27. public final String REPEAT_TIME = "repeatTime";
  28. // 令牌自定义标识
  29. @Value("${token.header}")
  30. private String header;
  31. @Autowired
  32. private RedisCache redisCache;
  33. @SuppressWarnings("unchecked")
  34. @Override
  35. public boolean isRepeatSubmit(HttpServletRequest request, RepeatSubmit annotation)
  36. {
  37. String nowParams = "";
  38. if (request instanceof RepeatedlyRequestWrapper)
  39. {
  40. RepeatedlyRequestWrapper repeatedlyRequest = (RepeatedlyRequestWrapper) request;
  41. nowParams = HttpHelper.getBodyString(repeatedlyRequest);
  42. }
  43. // body参数为空,获取Parameter的数据
  44. if (StringUtils.isEmpty(nowParams))
  45. {
  46. nowParams = JSONObject.toJSONString(request.getParameterMap());
  47. }
  48. Map<String, Object> nowDataMap = new HashMap<String, Object>();
  49. nowDataMap.put(REPEAT_PARAMS, nowParams);
  50. nowDataMap.put(REPEAT_TIME, System.currentTimeMillis());
  51. // 请求地址(作为存放cache的key值)
  52. String url = request.getRequestURI();
  53. // 唯一值(没有消息头则使用请求地址)
  54. String submitKey = StringUtils.trimToEmpty(request.getHeader(header));
  55. // 唯一标识(指定key + url + 消息头)
  56. String cacheRepeatKey = Constants.REPEAT_SUBMIT_KEY + url + submitKey;
  57. Object sessionObj = redisCache.getCacheObject(cacheRepeatKey);
  58. if (sessionObj != null)
  59. {
  60. Map<String, Object> sessionMap = (Map<String, Object>) sessionObj;
  61. if (sessionMap.containsKey(url))
  62. {
  63. Map<String, Object> preDataMap = (Map<String, Object>) sessionMap.get(url);
  64. if (compareParams(nowDataMap, preDataMap) && compareTime(nowDataMap, preDataMap, annotation.interval()))
  65. {
  66. return true;
  67. }
  68. }
  69. }
  70. Map<String, Object> cacheMap = new HashMap<String, Object>();
  71. cacheMap.put(url, nowDataMap);
  72. redisCache.setCacheObject(cacheRepeatKey, cacheMap, annotation.interval(), TimeUnit.MILLISECONDS);
  73. return false;
  74. }
  75. /**
  76. * 判断参数是否相同
  77. */
  78. private boolean compareParams(Map<String, Object> nowMap, Map<String, Object> preMap)
  79. {
  80. String nowParams = (String) nowMap.get(REPEAT_PARAMS);
  81. String preParams = (String) preMap.get(REPEAT_PARAMS);
  82. return nowParams.equals(preParams);
  83. }
  84. /**
  85. * 判断两次间隔时间
  86. */
  87. private boolean compareTime(Map<String, Object> nowMap, Map<String, Object> preMap, int interval)
  88. {
  89. long time1 = (Long) nowMap.get(REPEAT_TIME);
  90. long time2 = (Long) preMap.get(REPEAT_TIME);
  91. if ((time1 - time2) < interval)
  92. {
  93. return true;
  94. }
  95. return false;
  96. }
  97. }