From 655ffe8d3b9b07213167386b450e69ebd4b0beb6 Mon Sep 17 00:00:00 2001
From: Helius <wangdoubleone@gmail.com>
Date: Thu, 09 Jul 2020 09:51:02 +0800
Subject: [PATCH] add submit repeat limit

---
 src/main/resources/i18n/messages_zh_CN.properties                            |    2 +
 src/main/java/com/xcong/excoin/common/annotations/SubmitRepeat.java          |   13 ++++++
 src/main/java/com/xcong/excoin/common/aop/SubmitRepeatAspect.java            |   74 +++++++++++++++++++++++++++++++++++++
 src/main/resources/i18n/messages_en_US.properties                            |    2 +
 pom.xml                                                                      |    5 ++
 src/main/java/com/xcong/excoin/common/system/controller/LoginController.java |    2 +
 6 files changed, 98 insertions(+), 0 deletions(-)

diff --git a/pom.xml b/pom.xml
index cae38c0..0438973 100644
--- a/pom.xml
+++ b/pom.xml
@@ -59,6 +59,11 @@
 
         <dependency>
             <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-aop</artifactId>
+        </dependency>
+
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-actuator</artifactId>
         </dependency>
 
diff --git a/src/main/java/com/xcong/excoin/common/annotations/SubmitRepeat.java b/src/main/java/com/xcong/excoin/common/annotations/SubmitRepeat.java
new file mode 100644
index 0000000..eb41b12
--- /dev/null
+++ b/src/main/java/com/xcong/excoin/common/annotations/SubmitRepeat.java
@@ -0,0 +1,13 @@
+package com.xcong.excoin.common.annotations;
+
+import java.lang.annotation.*;
+
+/**
+ *
+ * @author helius
+ */
+@Documented
+@Retention(RetentionPolicy.RUNTIME)
+@Target(ElementType.METHOD)
+public @interface SubmitRepeat {
+}
diff --git a/src/main/java/com/xcong/excoin/common/aop/SubmitRepeatAspect.java b/src/main/java/com/xcong/excoin/common/aop/SubmitRepeatAspect.java
new file mode 100644
index 0000000..9d00c82
--- /dev/null
+++ b/src/main/java/com/xcong/excoin/common/aop/SubmitRepeatAspect.java
@@ -0,0 +1,74 @@
+package com.xcong.excoin.common.aop;
+
+import com.xcong.excoin.common.annotations.SubmitRepeat;
+import com.xcong.excoin.common.response.Result;
+import com.xcong.excoin.utils.MessageSourceUtils;
+import com.xcong.excoin.utils.RedisUtils;
+import lombok.extern.slf4j.Slf4j;
+import org.aspectj.lang.JoinPoint;
+import org.aspectj.lang.ProceedingJoinPoint;
+import org.aspectj.lang.annotation.*;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+import org.springframework.web.context.request.RequestContextHolder;
+import org.springframework.web.context.request.ServletRequestAttributes;
+
+import javax.annotation.Resource;
+import javax.servlet.http.HttpServletRequest;
+
+/**
+ * @author wzy
+ * @date 2020-04-16 22:01
+ **/
+@Slf4j
+@Aspect
+@Component
+public class SubmitRepeatAspect {
+
+    @Resource
+    private RedisUtils redisUtil;
+
+    private String key;
+
+    @Pointcut("@annotation(submitRepeat)")
+    public void submitRepeatPointCut(SubmitRepeat submitRepeat) {
+
+    }
+
+    @Before("submitRepeatPointCut(submitRepeat)")
+    public void before(SubmitRepeat submitRepeat) {
+    }
+
+    @Around("submitRepeatPointCut(submitRepeat)")
+    public Object around(ProceedingJoinPoint joinPoint, SubmitRepeat submitRepeat) throws Throwable {
+        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
+        HttpServletRequest request = attributes.getRequest();
+
+        String token = request.getHeader("token");
+        String uri = request.getRequestURI();
+        String mId = (String) redisUtil.get(token);
+        log.info("#token : {}, uri : {}, mId : {}#", token, uri, mId);
+        key = mId + "_" + uri;
+        boolean flag = redisUtil.setNotExist(key, "1", 5);
+        log.info("#mid : {}, flag : {}#", mId, flag);
+        if (flag) {
+            Object result = joinPoint.proceed();
+            redisUtil.del(key);
+            return result;
+        } else {
+            return Result.fail(MessageSourceUtils.getString("submit_repeat"));
+        }
+    }
+
+    @After("submitRepeatPointCut(submitRepeat)")
+    public void after(SubmitRepeat submitRepeat) {
+
+    }
+
+    @AfterThrowing(throwing = "ex", pointcut = "submitRepeatPointCut(submitRepeat)")
+    public void afterThrows(JoinPoint jp, Exception ex, SubmitRepeat submitRepeat) throws Exception {
+        log.error("#submit repeat error:#", ex);
+        redisUtil.del(key);
+        throw new Exception("系统繁忙");
+    }
+}
diff --git a/src/main/java/com/xcong/excoin/common/system/controller/LoginController.java b/src/main/java/com/xcong/excoin/common/system/controller/LoginController.java
index d8bfd99..4754bb5 100644
--- a/src/main/java/com/xcong/excoin/common/system/controller/LoginController.java
+++ b/src/main/java/com/xcong/excoin/common/system/controller/LoginController.java
@@ -10,6 +10,7 @@
 import cn.hutool.crypto.asymmetric.SignAlgorithm;
 import com.alibaba.fastjson.JSONObject;
 import com.xcong.excoin.common.LoginUserUtils;
+import com.xcong.excoin.common.annotations.SubmitRepeat;
 import com.xcong.excoin.common.contants.AppContants;
 import com.xcong.excoin.common.response.Result;
 import com.xcong.excoin.common.system.bean.LoginUserBean;
@@ -110,6 +111,7 @@
         return rsa.encryptBase64(token + "_" + System.currentTimeMillis(), KeyType.PublicKey);
     }
 
+    @SubmitRepeat
     @ApiOperation(value = "app注册接口", notes = "app注册接口,验证码必须输入可默认为123456")
     @PostMapping(value = "/register")
     public Result register(@RequestBody @Validated RegisterDto registerDto) {
diff --git a/src/main/resources/i18n/messages_en_US.properties b/src/main/resources/i18n/messages_en_US.properties
index 47d0eaf..fc356e7 100644
--- a/src/main/resources/i18n/messages_en_US.properties
+++ b/src/main/resources/i18n/messages_en_US.properties
@@ -239,3 +239,5 @@
 cancellation_success=Cancellation Success
 cancellation_fail=Cancellation Fail
 
+submit_repeat=Do not repeat submission
+
diff --git a/src/main/resources/i18n/messages_zh_CN.properties b/src/main/resources/i18n/messages_zh_CN.properties
index 7cabde0..0fdc300 100644
--- a/src/main/resources/i18n/messages_zh_CN.properties
+++ b/src/main/resources/i18n/messages_zh_CN.properties
@@ -238,3 +238,5 @@
 entrust_order_not_exist=委托单不存在
 cancellation_success=撤销成功
 cancellation_fail=撤销失败
+
+submit_repeat=请勿重复提交

--
Gitblit v1.9.1