Helius
2020-07-09 655ffe8d3b9b07213167386b450e69ebd4b0beb6
add submit repeat limit
2 files added
4 files modified
98 ■■■■■ changed files
pom.xml 5 ●●●●● patch | view | raw | blame | history
src/main/java/com/xcong/excoin/common/annotations/SubmitRepeat.java 13 ●●●●● patch | view | raw | blame | history
src/main/java/com/xcong/excoin/common/aop/SubmitRepeatAspect.java 74 ●●●●● patch | view | raw | blame | history
src/main/java/com/xcong/excoin/common/system/controller/LoginController.java 2 ●●●●● patch | view | raw | blame | history
src/main/resources/i18n/messages_en_US.properties 2 ●●●●● patch | view | raw | blame | history
src/main/resources/i18n/messages_zh_CN.properties 2 ●●●●● patch | view | raw | blame | history
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>
src/main/java/com/xcong/excoin/common/annotations/SubmitRepeat.java
New file
@@ -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 {
}
src/main/java/com/xcong/excoin/common/aop/SubmitRepeatAspect.java
New file
@@ -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("系统繁忙");
    }
}
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) {
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
src/main/resources/i18n/messages_zh_CN.properties
@@ -238,3 +238,5 @@
entrust_order_not_exist=委托单不存在
cancellation_success=撤销成功
cancellation_fail=撤销失败
submit_repeat=请勿重复提交