| | |
| | | package com.yeshi.fanli.aspect;
|
| | |
|
| | | import java.io.IOException;
|
| | |
|
| | | import javax.annotation.Resource;
|
| | | import javax.servlet.http.HttpServletRequest;
|
| | | import javax.servlet.http.HttpServletResponse;
|
| | |
|
| | | import org.aspectj.lang.ProceedingJoinPoint;
|
| | | import org.aspectj.lang.annotation.Around;
|
| | | import org.aspectj.lang.annotation.Aspect;
|
| | | import org.springframework.core.annotation.Order;
|
| | | import org.springframework.stereotype.Component;
|
| | | import org.springframework.web.context.request.RequestContextHolder;
|
| | | import org.springframework.web.context.request.ServletRequestAttributes;
|
| | | import org.yeshi.utils.JsonUtil;
|
| | |
|
| | | import com.yeshi.fanli.log.LogHelper;
|
| | | import com.yeshi.fanli.util.Constant;
|
| | | import com.yeshi.fanli.util.RedisManager;
|
| | |
|
| | | //请求频率验证
|
| | | @Component
|
| | | @Aspect
|
| | | @Order(1)
|
| | | public class RequestFrequencyLimitAspect {
|
| | | public static final String EDP = "execution(* com.yeshi.fanli.controller.h5.*.*(..)) or execution(* com.yeshi.fanli.controller.web.*.*(..)) or execution(* com.yeshi.fanli.controller.xcx.*.*(..))";
|
| | |
|
| | | @Resource
|
| | | private RedisManager redisManager;
|
| | |
|
| | | @Around(EDP)
|
| | | public Object testAround(ProceedingJoinPoint joinPoint) throws IOException {
|
| | | Object[] args = joinPoint.getArgs();
|
| | | ServletRequestAttributes servletContainer = (ServletRequestAttributes) RequestContextHolder
|
| | | .getRequestAttributes();
|
| | | HttpServletRequest request = servletContainer.getRequest();
|
| | | HttpServletResponse response = servletContainer.getResponse();
|
| | | boolean limit = false;
|
| | | if (request != null && response != null) {
|
| | | String uri = request.getRequestURI();
|
| | | String ip = request.getRemoteHost();
|
| | | if (Constant.IS_OUTNET) {
|
| | | limit = redisManager.ipFrequencyLimit(ip, uri);
|
| | | }
|
| | | }
|
| | |
|
| | | if (limit) {
|
| | | LogHelper.error("IP被限制了");
|
| | | response.getWriter().print(JsonUtil.loadFalseResult("请求过快"));
|
| | | return null;
|
| | | }
|
| | |
|
| | | Object obj = null;
|
| | | try {
|
| | | obj = joinPoint.proceed(args);
|
| | | } catch (Throwable e) {
|
| | | e.printStackTrace();
|
| | | }
|
| | | return obj;
|
| | | }
|
| | | }
|
| | | package com.yeshi.fanli.aspect; |
| | | |
| | | import java.io.IOException; |
| | | |
| | | import javax.annotation.Resource; |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import javax.servlet.http.HttpServletResponse; |
| | | |
| | | import org.aspectj.lang.ProceedingJoinPoint; |
| | | import org.aspectj.lang.annotation.Around; |
| | | import org.aspectj.lang.annotation.Aspect; |
| | | import org.springframework.core.annotation.Order; |
| | | import org.springframework.stereotype.Component; |
| | | import org.springframework.web.context.request.RequestContextHolder; |
| | | import org.springframework.web.context.request.ServletRequestAttributes; |
| | | import org.yeshi.utils.JsonUtil; |
| | | |
| | | import com.yeshi.fanli.log.LogHelper; |
| | | import com.yeshi.fanli.util.Constant; |
| | | import com.yeshi.fanli.util.RedisManager; |
| | | |
| | | //请求频率验证 |
| | | @Component |
| | | @Aspect |
| | | @Order(1) |
| | | public class RequestFrequencyLimitAspect { |
| | | public static final String EDP = "execution(* com.yeshi.fanli.controller.h5.*.*(..)) or execution(* com.yeshi.fanli.controller.web.*.*(..)) or execution(* com.yeshi.fanli.controller.xcx.*.*(..))"; |
| | | |
| | | @Resource |
| | | private RedisManager redisManager; |
| | | |
| | | @Around(EDP) |
| | | public Object testAround(ProceedingJoinPoint joinPoint) throws IOException { |
| | | Object[] args = joinPoint.getArgs(); |
| | | ServletRequestAttributes servletContainer = (ServletRequestAttributes) RequestContextHolder |
| | | .getRequestAttributes(); |
| | | HttpServletRequest request = servletContainer.getRequest(); |
| | | HttpServletResponse response = servletContainer.getResponse(); |
| | | boolean limit = false; |
| | | if (request != null && response != null) { |
| | | String uri = request.getRequestURI(); |
| | | String ip = request.getRemoteHost(); |
| | | if (Constant.IS_OUTNET) { |
| | | limit = redisManager.ipFrequencyLimit(ip, uri); |
| | | } |
| | | } |
| | | |
| | | if (limit) { |
| | | LogHelper.error("IP被限制了"); |
| | | response.getWriter().print(JsonUtil.loadFalseResult("请求过快")); |
| | | return null; |
| | | } |
| | | |
| | | Object obj = null; |
| | | try { |
| | | obj = joinPoint.proceed(args); |
| | | } catch (Throwable e) { |
| | | e.printStackTrace(); |
| | | } |
| | | return obj; |
| | | } |
| | | } |