Browse Source

防止重复提交

dev_shibei_match
zhaoqifeng 5 years ago
parent
commit
2d97dbfd60
  1. 17
      epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/aop/NoRepeatSubmit.java
  2. 82
      epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/aop/NoRepeatSubmitAop.java
  3. 2
      epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/exception/EpmetErrorCode.java
  4. 3
      epmet-user/epmet-user-server/src/main/java/com/epmet/controller/BadgeController.java

17
epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/aop/NoRepeatSubmit.java

@ -0,0 +1,17 @@
package com.epmet.commons.tools.aop;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* @author zhaoqifeng
* @dscription
* @date 2020/11/24 9:56
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface NoRepeatSubmit {
}

82
epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/aop/NoRepeatSubmitAop.java

@ -0,0 +1,82 @@
package com.epmet.commons.tools.aop;
import com.epmet.commons.tools.constant.NumConstant;
import com.epmet.commons.tools.exception.EpmetErrorCode;
import com.epmet.commons.tools.exception.RenException;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
/**
* @author zhaoqifeng
* @dscription
* @date 2020/11/24 9:59
*/
@Aspect
@Configuration
@Slf4j
public class NoRepeatSubmitAop {
/**
* 重复提交判断时间为2s
*/
private static final Cache<String, Object> CACHES = CacheBuilder.newBuilder()
// 最大缓存 100 个
.maximumSize(1000)
// 设置写缓存后 5 秒钟过期
.expireAfterWrite(5, TimeUnit.SECONDS)
.build();
@Before("execution(public * com.epmet..*Controller.*(..))")
public void before(JoinPoint joinPoint) {
System.out.println(joinPoint.getSignature().getName());
}
@Around("execution(public * com.epmet..*Controller.*(..)) && @annotation(com.epmet.commons.tools.aop.NoRepeatSubmit)")
public Object around(ProceedingJoinPoint pjp) {
try {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
assert attributes != null;
HttpServletRequest request = attributes.getRequest();
String key = getKey(request.getRequestURI(), pjp.getArgs());
// 如果缓存中有这个url视为重复提交
if (CACHES.getIfPresent(key) == null) {
Object o = pjp.proceed();
CACHES.put(key, NumConstant.ZERO);
return o;
} else {
log.error("重复提交");
throw new RenException(EpmetErrorCode.REPEATED_SUBMIT_ERROR.getCode());
}
} catch (RenException e) {
throw e;
} catch (Throwable e) {
log.error("验证重复提交时出现未知异常!");
throw new RenException(EpmetErrorCode.SERVER_ERROR.getCode());
}
}
private String getKey(String keyExpress, Object[] args) {
for (int i = 0; i < args.length; i++) {
keyExpress = keyExpress.replace("arg[" + i + "]", args[i].toString());
}
return keyExpress;
}
}

2
epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/exception/EpmetErrorCode.java

@ -123,7 +123,7 @@ public enum EpmetErrorCode {
// 党建声音 前端提示 88段
DRAFT_CONTENT_IS_NULL(8801, "至少需要添加一个段落"),
ARTICLE_PUBLISH_ERROR(8801, "发布文章失败,请刷新重试"),
REPEATED_SUBMIT_ERROR(8998, "请勿重复提交"),
CUSTOMER_VALIDATE_ERROR(8999, "内部数据校验异常"),
//公众号 865..开头的码

3
epmet-user/epmet-user-server/src/main/java/com/epmet/controller/BadgeController.java

@ -1,6 +1,7 @@
package com.epmet.controller;
import com.epmet.commons.tools.annotation.LoginUser;
import com.epmet.commons.tools.aop.NoRepeatSubmit;
import com.epmet.commons.tools.security.dto.TokenDto;
import com.epmet.commons.tools.utils.Result;
import com.epmet.commons.tools.validator.ValidatorUtils;
@ -82,6 +83,7 @@ public class BadgeController {
* @return com.epmet.commons.tools.utils.Result
*/
@PostMapping("edit")
@NoRepeatSubmit
public Result edit(@LoginUser TokenDto tokenDto, @RequestBody EditBadgeFormDTO formDTO) {
ValidatorUtils.validateEntity(formDTO);
badgeService.edit(tokenDto, formDTO);
@ -147,6 +149,7 @@ public class BadgeController {
* @return com.epmet.commons.tools.utils.Result
*/
@PostMapping("audit")
@NoRepeatSubmit
public Result audit(@LoginUser TokenDto tokenDto, @RequestBody BadgeAuditFormDTO formDTO) {
ValidatorUtils.validateEntity(formDTO);
badgeService.audit(tokenDto, formDTO);

Loading…
Cancel
Save