4 changed files with 103 additions and 1 deletions
@ -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 { |
||||
|
|
||||
|
} |
@ -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; |
||||
|
} |
||||
|
|
||||
|
|
||||
|
} |
Loading…
Reference in new issue