| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 |
- package com.cyksj.web.aop;
- import cn.hutool.extra.servlet.ServletUtil;
- import com.cyksj.common.annotation.NoSubmit;
- import com.cyksj.common.exception.BusinessRuntimeException;
- import com.google.common.cache.Cache;
- import org.apache.commons.logging.Log;
- import org.apache.commons.logging.LogFactory;
- import org.aspectj.lang.ProceedingJoinPoint;
- import org.aspectj.lang.annotation.Around;
- import org.aspectj.lang.annotation.Aspect;
- 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.servlet.http.HttpServletRequest;
- /**
- * @author zwhui
- * @date 2020/7/16 上午10:55
- */
- @Aspect
- @Component
- public class NoSubmitAop {
- private Log logger = LogFactory.getLog(getClass());
- @Autowired
- private Cache<String, Integer> cache;
- @Around("execution(* com.cyksj..*.*(..)) && @annotation(nrs)")
- public synchronized Object arround(ProceedingJoinPoint pjp, NoSubmit nrs) throws Throwable{
- ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
- HttpServletRequest request = attributes.getRequest();
- String ipAddr = ServletUtil.getClientIP(request);
- String key = ipAddr + "-" + request.getServletPath();
- if (cache.getIfPresent(key) == null) {
- // 如果缓存中有这个url视为重复提交
- cache.put(key, 0);
- return pjp.proceed();
- } else {
- logger.error("重复提交");
- throw BusinessRuntimeException.getInstance("Please do not submit repeatedly");
- }
- }
- }
|