栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringBoot Redis使用AOP防止重复提交

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringBoot Redis使用AOP防止重复提交

自定义注解
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface NoRepeatSubmit {
    int seconds();
    int maxCount();
}
防止重复请求切面
import com.alibaba.fastjson.JSONObject;
import io.renren.commons.tools.utils.Result;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.util.Assert;
import javax.servlet.http.HttpServletRequest;
import java.util.concurrent.TimeUnit;


@Aspect
@Component
public class NoRepeatSubmitAspect {

    protected Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    private RedisTemplate redisTemplate;

    @Pointcut("@annotation(noRepeatSubmit)")
    public void pointCut(NoRepeatSubmit noRepeatSubmit) {
    }

    @Around("pointCut(noRepeatSubmit)")
    public Result around(ProceedingJoinPoint pjp, NoRepeatSubmit noRepeatSubmit) throws Throwable {
        ServletRequestAttributes ra= (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = ra.getRequest();
        Assert.notNull(request, "request can not null");

        int seconds = noRepeatSubmit.seconds();
        int maxCount = noRepeatSubmit.maxCount();
        //获取请求Ip
        String ip=request.getRemoteAddr();
        //将请求路径及ip组成键值 如:/finance/fastApply/test:192.168.56.1
        String key = request.getServletPath() + ":" + ip ;
        //获取请求次数
        Integer count = (Integer) redisTemplate.opsForValue().get(key);

        if (null == count) {//第一次访问
            redisTemplate.opsForValue().set(key, 1,seconds, TimeUnit.SECONDS);
            pjp.proceed();
            return new Result().ok("第一次请求成功");
        }else if (count < maxCount) {//在允许的访问次数内
            count = count+1;
            redisTemplate.opsForValue().set(key, count,0);
            pjp.proceed();
            return new Result().ok("请求成功");
        }else  {//超出访问次数
            logger.info("访问过快ip ===> " + ip + " 且在 " + seconds + " 秒内超过最大限制 ===> " + maxCount + " 请求次数达到 ===> " + count);
            return new Result().error("请求过快");
        }
    }
}


使用样例
	
    @NoRepeatSubmit(seconds=30, maxCount=1)
    @GetMapping("/test")
    public Result test()
    {
        System.out.println("我被请求了");
        return new Result().ok("请求成功");
    }

此方法只是简单的根据记录同一IP提交次数去防止重复请求

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/731596.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号