The controller layer gets the value returned by the aop method

problem description:
basically prevents users from making malicious requests, limits the number of visits per unit time
, and then makes an aop, but I can"t get the return value of the aop method in the controller layer, as follows:

 @Pointcut("execution(public * com.mzd.redis_springboot_mybatis_mysql.controller.*.*(..))")
    public void WebPointCut() {
    }

    @Before("WebPointCut() && @annotation(times)")
    public boolean ifovertimes(final JoinPoint joinPoint, RequestTimes times)  {
        try {
            Object[] objects = joinPoint.getArgs();
            HttpServletRequest request = null;
            for (int i = 0; i < objects.length; iPP) {
                if (objects[i] instanceof HttpServletRequest) {
                    request = (HttpServletRequest) objects[i];
                    break;
                }
            }
            if (request == null) {
                return true;
            }
            String ip = request.getRemoteAddr();
            String url = request.getRequestURL().toString();
            String key = "ifovertimes".concat(url).concat(ip);
            long count = redisTemplate.opsForValue().increment(key, 1);
            //
            if (count == 1) {
                redisTemplate.expire(key, times.time(), TimeUnit.MILLISECONDS);
            }
            if (count <= times.count()) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

then I want to know how to get this Boolean value in the controller layer, whether it has reached the upper limit of the number of visits or has not reached the upper limit of the number of visits

Mar.03,2021

this scenario should use @ Around surround injection. When it is determined that the number of times exceeds the limit, the exception information is returned directly, and the Controller method is no longer executed.
in addition, Controller can also rely on the return value of aop, but it is not a good program design, which goes against the original intention of AOP


to see if there is any API that can be obtained. If not, put the Boolean value into the request field object, which can be obtained in the Controller method.


check to see if aop scanning is configured in your spring-mvc.xml configuration file.
for more information, please see Spring MVC AOP is invalid and does not work. One of the solutions


Here is the code~

@Around("...")
public Object controllerLogAround(ProceedingJoinPoint pjp) throws Throwable {
    ...
    // 
    Object[] methodArgs = pjp.getArgs();
    // 
    Object returnValue = pjp.proceed(methodArgs);
    ...
    return returnValue;
}

in addition to configuring AOP, on the controller, it is also managed through a filter / interceptor:

Menu