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