
com.github.mengweijin.quickboot.redis.inteceptor.SameUrlDataInterceptor Maven / Gradle / Ivy
package com.github.mengweijin.quickboot.redis.inteceptor;
import cn.hutool.extra.servlet.ServletUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.mengweijin.quickboot.filter.repeatable.RepeatedlyRequestWrapper;
import com.github.mengweijin.quickboot.redis.RedisService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
/**
* 判断请求url和数据是否和上一次相同,
* 如果和上次相同,则是重复提交表单。 有效时间为10秒内。
*/
public class SameUrlDataInterceptor extends RepeatSubmitInterceptor {
public final String REPEAT_PARAMS = "repeatParams";
public final String REPEAT_TIME = "repeatTime";
@Autowired
private ObjectMapper objectMapper;
@Autowired
private RedisService redisService;
@SuppressWarnings("unchecked")
@Override
public boolean isRepeatSubmit(HttpServletRequest request, RepeatSubmit repeatSubmit) throws JsonProcessingException {
String nowParams = "";
if (request instanceof RepeatedlyRequestWrapper) {
RepeatedlyRequestWrapper repeatedlyRequest = (RepeatedlyRequestWrapper) request;
nowParams = ServletUtil.getBody(repeatedlyRequest);
}
// body参数为空,获取Parameter的数据
if (StringUtils.isEmpty(nowParams)) {
nowParams = objectMapper.writeValueAsString(request.getParameterMap());
}
Map nowDataMap = new HashMap<>();
nowDataMap.put(REPEAT_PARAMS, nowParams);
nowDataMap.put(REPEAT_TIME, System.currentTimeMillis());
// 请求地址(作为存放cache的key值)
String url = request.getRequestURI();
// 唯一标识(指定key + url)
String cacheRepeatKey = REPEAT_SUBMIT_KEY + url;
Object sessionObj = redisService.getCacheObject(cacheRepeatKey);
if (sessionObj != null) {
Map sessionMap = (Map) sessionObj;
if (sessionMap.containsKey(url)) {
Map preDataMap = (Map) sessionMap.get(url);
if (compareParams(nowDataMap, preDataMap) && compareTime(nowDataMap, preDataMap, repeatSubmit.interval())) {
return true;
}
}
}
Map cacheMap = new HashMap<>(1);
cacheMap.put(url, nowDataMap);
redisService.setCacheObject(cacheRepeatKey, cacheMap, repeatSubmit.interval(), TimeUnit.MILLISECONDS);
return false;
}
/**
* 判断参数是否相同
*/
private boolean compareParams(Map nowMap, Map preMap) {
String nowParams = (String) nowMap.get(REPEAT_PARAMS);
String preParams = (String) preMap.get(REPEAT_PARAMS);
return nowParams.equals(preParams);
}
/**
* 判断两次间隔时间
*/
private boolean compareTime(Map nowMap, Map preMap, int interval) {
long time1 = (Long) nowMap.get(REPEAT_TIME);
long time2 = (Long) preMap.get(REPEAT_TIME);
return (time1 - time2) < interval;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy