|
@@ -5,6 +5,7 @@ import cn.hutool.core.convert.Convert;
|
|
import com.xjrsoft.common.constant.GlobalConstant;
|
|
import com.xjrsoft.common.constant.GlobalConstant;
|
|
import com.xjrsoft.common.enums.RoleEnum;
|
|
import com.xjrsoft.common.enums.RoleEnum;
|
|
import com.xjrsoft.common.utils.RedisUtil;
|
|
import com.xjrsoft.common.utils.RedisUtil;
|
|
|
|
+import com.xjrsoft.config.RateLimitConfig;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.stereotype.Component;
|
|
import org.springframework.stereotype.Component;
|
|
import org.springframework.web.servlet.HandlerInterceptor;
|
|
import org.springframework.web.servlet.HandlerInterceptor;
|
|
@@ -19,11 +20,14 @@ public class RateLimitInterceptor implements HandlerInterceptor {
|
|
@Autowired
|
|
@Autowired
|
|
private RedisUtil redisUtil;
|
|
private RedisUtil redisUtil;
|
|
|
|
|
|
|
|
+ @Autowired
|
|
|
|
+ private RateLimitConfig rateLimitConfig;
|
|
|
|
+
|
|
@Override
|
|
@Override
|
|
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
|
|
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
|
|
- if (isLimiting()) {
|
|
|
|
|
|
+ if (rateLimitConfig.isEnabled() && isLimiting()) {
|
|
// 暂停5秒
|
|
// 暂停5秒
|
|
- Thread.sleep(5000);
|
|
|
|
|
|
+ Thread.sleep(rateLimitConfig.getTimeout());
|
|
System.out.println("触发限流");
|
|
System.out.println("触发限流");
|
|
}
|
|
}
|
|
return true;
|
|
return true;
|