From 03f6d2d39edad74229d5daed9cbc150ecb4691b0 Mon Sep 17 00:00:00 2001
From: chenjiahe <763432473@qq.com>
Date: 星期一, 04 十二月 2023 18:26:04 +0800
Subject: [PATCH] 防重复提交工具

---
 src/main/java/com/hx/repeat/check/RequestRepeat.java |   37 +++++++++++++++++++++++++++++++++++++
 1 files changed, 37 insertions(+), 0 deletions(-)

diff --git a/src/main/java/com/hx/repeat/check/RequestRepeat.java b/src/main/java/com/hx/repeat/check/RequestRepeat.java
new file mode 100644
index 0000000..0f0dd28
--- /dev/null
+++ b/src/main/java/com/hx/repeat/check/RequestRepeat.java
@@ -0,0 +1,37 @@
+package com.hx.repeat.check;
+
+import com.hx.exception.TipsException;
+import com.hx.redis.RedisUtil;
+import com.hx.util.DateUtil;
+import com.hx.util.IPUtils;
+
+import javax.servlet.http.HttpServletRequest;
+import java.util.Date;
+import java.util.concurrent.TimeUnit;
+
+/**
+ * URL闃查噸澶嶆彁浜�
+ * @author CJH
+ */
+public class RequestRepeat {
+
+    /**璇锋眰閲嶅鏍¢獙
+     * @param request HttpServletRequest
+     * @param redisUtil redis
+     * @param millisecond 闂撮殧鏃堕棿锛屾绉掞紙榛樿500姣锛�
+     */
+    public static void checkRequest(HttpServletRequest request,RedisUtil redisUtil,Long millisecond){
+        //娌℃湁璁剧疆锛岄粯璁�0.5绉�
+        if(millisecond == null){
+            millisecond = 500L;
+        }
+        //鑾峰彇璇锋眰鐨処P鍦板潃
+        String ip = IPUtils.getIpAddr(request);
+        //鑾峰彇URL
+        String method = request.getServletPath();
+        if(!redisUtil.setIfAbsent(ip+"-"+method, DateUtil.formatDate(new Date(),"yyyy-MM-dd HH:mm:ss"),millisecond, TimeUnit.MILLISECONDS)){
+            throw new TipsException("璇峰嬁棰戠箒鎿嶄綔锛�");
+        }
+    }
+
+}

--
Gitblit v1.8.0