api端防重复提交

This commit is contained in:
Chopper
2022-01-25 10:22:45 +08:00
parent 039e85d5e5
commit 3972e0231c
33 changed files with 181 additions and 28 deletions

View File

@@ -4,6 +4,7 @@ import cn.lili.cache.limit.enums.LimitTypeEnums;
import cn.lili.cache.limit.annotation.LimitPoint;
import cn.lili.common.enums.ResultCode;
import cn.lili.common.exception.ServiceException;
import cn.lili.common.utils.IpUtils;
import com.google.common.collect.ImmutableList;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
@@ -54,7 +55,8 @@ public class LimitInterceptor {
key = limitPointAnnotation.key();
break;
default:
key = limitPointAnnotation.key() + getIpAddress();
key = limitPointAnnotation.key() + IpUtils
.getIpAddress(((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest());
}
ImmutableList<String> keys = ImmutableList.of(StringUtils.join(limitPointAnnotation.prefix(), key));
try {
@@ -71,32 +73,8 @@ public class LimitInterceptor {
} catch (ServiceException e) {
throw e;
} catch (Exception e) {
throw new RuntimeException("服务器异常,请稍后再试");
throw new ServiceException(ResultCode.ERROR);
}
}
/**
* 默认unknown常量值
*/
private static final String UNKNOWN = "unknown";
/**
* 获取ip
* @return ip
*/
public String getIpAddress() {
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
String ip = request.getHeader("x-forwarded-for");
if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
ip = request.getHeader("Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
ip = request.getHeader("WL-Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
}
return ip;
}
}

View File

@@ -4,6 +4,8 @@ import java.lang.annotation.*;
/**
* 演示站点注解
* <p>
* PS 此注解需要用户登录之后才可以使用
*
* @author Bulbasaur
* @since 2021/7/9 1:40 上午

View File

@@ -0,0 +1,25 @@
package cn.lili.common.aop.annotation;
import java.lang.annotation.*;
import java.util.concurrent.TimeUnit;
/**
* 防止重复提交注解
*
* @author liushuai(liushuai711 @ gmail.com)
* @version v4.0
* @Description:
* @since 2022/1/25 09:17
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
public @interface PreventDuplicateSubmissions {
/**
* 过期时间
*/
long expire() default 3;
}

View File

@@ -0,0 +1,67 @@
package cn.lili.common.aop.interceptor;
/**
* 防重复提交业务
*
* @author Chopper
* @version v1.0
* 2022-01-25 09:20
*/
import cn.lili.cache.Cache;
import cn.lili.common.aop.annotation.PreventDuplicateSubmissions;
import cn.lili.common.enums.ResultCode;
import cn.lili.common.exception.ServiceException;
import cn.lili.common.security.context.UserContext;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
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;
@Aspect
@Component
@Slf4j
public class PreventDuplicateSubmissionsInterceptor {
@Autowired
private Cache<String> cache;
@Before("@annotation(preventDuplicateSubmissions)")
public void interceptor(PreventDuplicateSubmissions preventDuplicateSubmissions) {
try {
Long count = cache.incr(getParams(), preventDuplicateSubmissions.expire());
//如果超过1或者设置的参数则表示重复提交了
if (count.intValue() >= preventDuplicateSubmissions.expire()) {
throw new ServiceException(ResultCode.LIMIT_ERROR);
}
}
//如果参数为空,则表示用户未登录,直接略过,不做处理
catch (NullPointerException e) {
return;
} catch (ServiceException e) {
throw e;
} catch (Exception e) {
throw new ServiceException(ResultCode.ERROR);
}
}
/**
* 获取表单参数
*
* @return
*/
private String getParams() {
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
//请求地址
return request.getRequestURI() + UserContext.getCurrentUser().getId() + UserContext.getCurrentUser().getUsername();
}
}