All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.gitee.fufu669.aspect.CheckVisitFrequencyAop Maven / Gradle / Ivy

There is a newer version: 6.666.66021
Show newest version
package com.gitee.fufu669.aspect;

import com.gitee.fufu669.common.CacheKeyCommon;
import com.gitee.fufu669.config.exception.CacheServerErrorCode;
import com.gitee.fufu669.config.exception.CacheServerException;
import com.gitee.fufu669.service.CacheService;
import com.gitee.fufu669.utils.CacheAopUtil;
import com.gitee.fufu669.utils.CacheNetworkUtil;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;


import java.lang.reflect.Method;

import javax.servlet.http.HttpServletRequest;

/**
 * @author wangfupeng on 2017-5-6.
 */
@Aspect
@Component
@Order(-67)
@SuppressWarnings({"rawtypes"})
public class CheckVisitFrequencyAop {

    public static final Logger logger = LoggerFactory.getLogger(CheckVisitFrequencyAop.class);

    @Autowired
    private HttpServletRequest request;

    @Autowired
    private CacheService cacheService;

    @Before("@annotation(com.gitee.fufu669.aspect.CheckVisitFrequency)")
    public void aroundMethod(JoinPoint joinPoint) {
        Integer frequencyNumber = null;
        Integer secondsNumber = null;
        String methodName = null;
        String formulaString = "";
        String description = "";
        try {
            String targetName = joinPoint.getTarget().getClass().getName();
            methodName = joinPoint.getSignature().getName();
            Class targetClass = Class.forName(targetName);
            Method[] methods = targetClass.getMethods();
            for (Method method : methods) {
                if (method.getName().equals(methodName)) {
                    formulaString = method.getAnnotation(CheckVisitFrequency.class).formula();
                    description = method.getAnnotation(CheckVisitFrequency.class).description();
                    break;
                }
            }
            if (StringUtils.isEmpty(formulaString)) {
                formulaString = CacheKeyCommon.CHECKVISITFREQUENCY_LIMIT_VISIT_FORMULA;
            }
            String[] fromulas = formulaString.split(",");
            secondsNumber = Integer.valueOf(fromulas[0]);
            frequencyNumber = Integer.valueOf(fromulas[1]);
        } catch (Exception e) {
            throw new CacheServerException(CacheServerErrorCode.CHECKVISITFREQUENCYAOP_LIMIT_PROGRAM_AOP_PREFERENCE_ERROR);
        }
        String token = request.getHeader("token");
        if (token == null) {
            token = CacheNetworkUtil.getIpAddr(request);
        }
        if (secondsNumber > 0 && frequencyNumber > 0) {
            if (cacheService.get(CacheKeyCommon.REDIS_KEY_CHECKVISITFREQUENCY + "lock:" + CacheAopUtil.getCacheKey(joinPoint) + ":" + token) == null) {
                cacheService.incrby(CacheKeyCommon.REDIS_KEY_CHECKVISITFREQUENCY + "lock:" + CacheAopUtil.getCacheKey(joinPoint) + ":" + token, 1);
                cacheService.expire(CacheKeyCommon.REDIS_KEY_CHECKVISITFREQUENCY + "lock:" + CacheAopUtil.getCacheKey(joinPoint) + ":" + token, secondsNumber);
            } else {
                if (cacheService.ttl(CacheKeyCommon.REDIS_KEY_CHECKVISITFREQUENCY + "lock:" + CacheAopUtil.getCacheKey(joinPoint) + ":" + token).equals(-1L)) {
                    cacheService.expire(CacheKeyCommon.REDIS_KEY_CHECKVISITFREQUENCY + "lock:" + CacheAopUtil.getCacheKey(joinPoint) + ":" + token, secondsNumber);
                }
                Long visitTimes = cacheService.incrby(CacheKeyCommon.REDIS_KEY_CHECKVISITFREQUENCY + "lock:" + CacheAopUtil.getCacheKey(joinPoint) + ":" + token, 1);
                cacheService.expire(CacheKeyCommon.REDIS_KEY_CHECKVISITFREQUENCY + "lock:" + CacheAopUtil.getCacheKey(joinPoint) + ":" + token, secondsNumber);
                if (visitTimes > frequencyNumber) {
                    if (!"".equals(description)) {
                        description = " " + description + " ";
                    }
                    throw new CacheServerException(CacheServerErrorCode.CHECKVISITFREQUENCYAOP_CAN_NOT_VISIT_THIS_RESOURCE_WITH_SECONDS, secondsNumber, frequencyNumber, visitTimes, description);
                }
            }
        }
        logger.info("CheckVisitFrequencyAop:通过验证:methodName:" + methodName + ":formula:" + formulaString + ":description:" + description);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy