|
|
@@ -1,39 +1,134 @@
|
|
|
-package com.sckw.core.filter;
|
|
|
-
|
|
|
-import com.sckw.core.model.auth.context.LoginEnterpriseHolder;
|
|
|
-import com.sckw.core.web.context.LoginUserHolder;
|
|
|
-import jakarta.servlet.http.HttpServletRequest;
|
|
|
-import jakarta.servlet.http.HttpServletResponse;
|
|
|
-import org.springframework.lang.Nullable;
|
|
|
-import org.springframework.web.servlet.HandlerInterceptor;
|
|
|
-
|
|
|
-/**
|
|
|
- * @desc 拦截器
|
|
|
- * @author zk
|
|
|
- * @date 2023/8/18
|
|
|
- */
|
|
|
-public class RequestCheckInterceptor implements HandlerInterceptor {
|
|
|
-
|
|
|
- /**
|
|
|
- * @desc 请求处理之前执行
|
|
|
- * @author zk
|
|
|
- * @date 2023/8/18
|
|
|
- **/
|
|
|
- @Override
|
|
|
- public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
|
|
|
-
|
|
|
- return true;
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- /**
|
|
|
- * @desc preHandle成功后执行
|
|
|
- * @author zk
|
|
|
- * @date 2023/8/18
|
|
|
- **/
|
|
|
- @Override
|
|
|
- public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable Exception ex) throws Exception {
|
|
|
- LoginUserHolder.remove();
|
|
|
- LoginEnterpriseHolder.remove();
|
|
|
- }
|
|
|
-}
|
|
|
+//package com.sckw.core.filter;
|
|
|
+//
|
|
|
+//import com.sckw.core.annotation.RepeatSubmit;
|
|
|
+//import com.sckw.core.model.auth.context.LoginEnterpriseHolder;
|
|
|
+//import com.sckw.core.web.context.LoginUserHolder;
|
|
|
+//import jakarta.servlet.http.HttpServletRequest;
|
|
|
+//import jakarta.servlet.http.HttpServletResponse;
|
|
|
+//import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+//import org.springframework.lang.Nullable;
|
|
|
+//import org.springframework.web.method.HandlerMethod;
|
|
|
+//import org.springframework.web.servlet.HandlerInterceptor;
|
|
|
+//
|
|
|
+//import java.lang.reflect.Method;
|
|
|
+//import java.util.Map;
|
|
|
+//
|
|
|
+///**
|
|
|
+// * @author zk
|
|
|
+// * @desc 拦截器
|
|
|
+// * @date 2023/8/18
|
|
|
+// */
|
|
|
+//public class RequestCheckInterceptor implements HandlerInterceptor {
|
|
|
+//
|
|
|
+// /**
|
|
|
+// * @desc 请求处理之前执行
|
|
|
+// * @author zk
|
|
|
+// * @date 2023/8/18
|
|
|
+// **/
|
|
|
+// @Override
|
|
|
+// public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
|
|
|
+//// if (handler instanceof HandlerMethod) {
|
|
|
+//// HandlerMethod handlerMethod = (HandlerMethod) handler;
|
|
|
+//// Method method = handlerMethod.getMethod();
|
|
|
+//// //判断是否添加了防重复提交的注解
|
|
|
+//// RepeatSubmit annotation = method.getAnnotation(RepeatSubmit.class);
|
|
|
+//// if (annotation != null) {
|
|
|
+//// //判断该请求是否重复提交
|
|
|
+//// if (isRepeatSubmit(request, annotation)) {
|
|
|
+//// //封装错误信息,直接返回
|
|
|
+//// String message = annotation.message();
|
|
|
+//// return false;
|
|
|
+//// }
|
|
|
+//// }
|
|
|
+//// return true;
|
|
|
+//// } else {
|
|
|
+//// return true;
|
|
|
+//// }
|
|
|
+// }
|
|
|
+//
|
|
|
+//
|
|
|
+// /**
|
|
|
+// * @desc preHandle成功后执行
|
|
|
+// * @author zk
|
|
|
+// * @date 2023/8/18
|
|
|
+// **/
|
|
|
+// @Override
|
|
|
+// public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable Exception ex) throws Exception {
|
|
|
+// LoginUserHolder.remove();
|
|
|
+// LoginEnterpriseHolder.remove();
|
|
|
+// }
|
|
|
+//
|
|
|
+//
|
|
|
+// public boolean isRepeatSubmit(HttpServletRequest request, RepeatSubmit annotation)
|
|
|
+// {
|
|
|
+// String nowParams = "";
|
|
|
+// if (request instanceof RepeatedlyRequestWrapper)
|
|
|
+// {
|
|
|
+// RepeatedlyRequestWrapper repeatedlyRequest = (RepeatedlyRequestWrapper) request;
|
|
|
+// nowParams = HttpHelper.getBodyString(repeatedlyRequest);
|
|
|
+// }
|
|
|
+// //获取请求参数
|
|
|
+// // body参数为空,获取Parameter的数据
|
|
|
+// if (StringUtils.isEmpty(nowParams))
|
|
|
+// {
|
|
|
+// nowParams = JSON.toJSONString(request.getParameterMap());
|
|
|
+// }
|
|
|
+// Map<String, Object> nowDataMap = new HashMap<String, Object>();
|
|
|
+// nowDataMap.put(REPEAT_PARAMS, nowParams);
|
|
|
+// nowDataMap.put(REPEAT_TIME, System.currentTimeMillis());
|
|
|
+//
|
|
|
+// // 请求地址(作为存进Redis的key值),没有使用url是因为所有请求的前缀都是一样的(http://ip地址:端口号),微服务
|
|
|
+// 项目使用nginx所在服务器的IP地址和端口号,之后再转发到网关。
|
|
|
+// String url = request.getRequestURI();
|
|
|
+// // 唯一值(没有消息头则使用请求地址)
|
|
|
+// String submitKey = StringUtils.trimToEmpty(request.getHeader(header));
|
|
|
+//
|
|
|
+// // 唯一标识(指定key + url + 消息头) 消息头相当于登录用户的唯一标识,唯一标识主要靠token区分。
|
|
|
+// String cacheRepeatKey = CacheConstants.REPEAT_SUBMIT_KEY + url + submitKey;
|
|
|
+// //从Redis获取锁
|
|
|
+// Object sessionObj = redisCache.getCacheObject(cacheRepeatKey);
|
|
|
+// if (sessionObj != null)
|
|
|
+// {
|
|
|
+// Map<String, Object> sessionMap = (Map<String, Object>) sessionObj;
|
|
|
+// if (sessionMap.containsKey(url))
|
|
|
+// {
|
|
|
+// Map<String, Object> preDataMap = (Map<String, Object>) sessionMap.get(url);
|
|
|
+// //如果请求地址相同、请求参数相同而且时间间隔小于间隔时间,则视为重复请求,直接返回。
|
|
|
+// if (compareParams(nowDataMap, preDataMap) && compareTime(nowDataMap, preDataMap, annotation.interval()))
|
|
|
+// {
|
|
|
+// return true;
|
|
|
+// }
|
|
|
+// }
|
|
|
+// }
|
|
|
+// //如果不是重复请求则将锁存进Redis中,过期时间为所设定的时间间隔然后返回。
|
|
|
+// Map<String, Object> cacheMap = new HashMap<String, Object>();
|
|
|
+// cacheMap.put(url, nowDataMap);
|
|
|
+// redisCache.setCacheObject(cacheRepeatKey, cacheMap, annotation.interval(), TimeUnit.MILLISECONDS);
|
|
|
+// return false;
|
|
|
+// }
|
|
|
+//
|
|
|
+// /**
|
|
|
+// * 判断参数是否相同
|
|
|
+// */
|
|
|
+// private boolean compareParams(Map<String, Object> nowMap, Map<String, Object> preMap)
|
|
|
+// {
|
|
|
+// String nowParams = (String) nowMap.get(REPEAT_PARAMS);
|
|
|
+// String preParams = (String) preMap.get(REPEAT_PARAMS);
|
|
|
+// return nowParams.equals(preParams);
|
|
|
+// }
|
|
|
+//
|
|
|
+// /**
|
|
|
+// * 判断两次间隔时间
|
|
|
+// */
|
|
|
+// private boolean compareTime(Map<String, Object> nowMap, Map<String, Object> preMap, int interval)
|
|
|
+// {
|
|
|
+// long time1 = (Long) nowMap.get(REPEAT_TIME);
|
|
|
+// long time2 = (Long) preMap.get(REPEAT_TIME);
|
|
|
+// if ((time1 - time2) < interval)
|
|
|
+// {
|
|
|
+// return true;
|
|
|
+// }
|
|
|
+// return false;
|
|
|
+// }
|
|
|
+//
|
|
|
+//}
|