iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >Java后端限制频繁请求和重复提交的实现
  • 938
分享到

Java后端限制频繁请求和重复提交的实现

2024-04-02 19:04:59 938人浏览 独家记忆

Python 官方文档:入门教程 => 点击学习

摘要

目录步骤一、写限制注解步骤二、解析注解步骤三、控制层注解使用当前端按连续请求多次,请求过于频繁或者是多次重复提交数据,对系统或者是数据造成了一点的损害。 为了解决这个问题,下面介绍了

前端按连续请求多次,请求过于频繁或者是多次重复提交数据,对系统或者是数据造成了一点的损害。

为了解决这个问题,下面介绍了一种简易的解决方法:

步骤一、写限制注解

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 {
 
    String name() default "name:";
}

步骤二、解析注解

import javax.servlet.Http.httpservletRequest;
import com.hieasy.comm.core.domain.R;
import com.hieasy.comm.Redis.service.RedisService;
import com.hieasy.system.util.TermUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.WEB.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import java.util.concurrent.TimeUnit;
 
 

 
@Aspect
@Component
public class NoRepeatSubmitaop {
 
    private Log logger = LogFactory.getLog(getClass());
 
    @Autowired
    private RedisService redisService;
 
    @Around("execution(* com.hieasy.*.controller.*.*Ctrl.*(..)) && @annotation(nrs)")
    public Object arround(ProceedingJoinPoint pjp, NoRepeatSubmit nrs) throws Throwable {
       
            ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
            HttpServletRequest request = attributes.getRequest();
            String key = TermUtil.getUserId() + "-" + request.getServletPath();
            if ( !redisService.haskey(key) ) {// 如果缓存中有这个url视为重复提交
                Object o = pjp.proceed();
                redisService.setCacheObject(key, 0, 15, TimeUnit.SECONDS);
                return o;
            } else {
                redisService.setCacheObject(key, 0, 15, TimeUnit.SECONDS);//点了同样的URL继续限制,直到2次点击中间间隔超过了限制
                return R.error("请勿重复提交或者操作过于频繁!");
            }
    
    }
 
}
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.*;
import org.springframework.stereotype.Component;
 
import java.util.*;
import java.util.concurrent.TimeUnit;
 

@Component
@SuppressWarnings(value = {"unchecked", "rawtypes"})
public class RedisService {
 
    @Autowired
    public RedisTemplate redisTemplate;
 
    
    public <T> ValueOperations<String, T> setCacheObject(String key, T value) {
        ValueOperations<String, T> operation = redisTemplate.opsForValue();
        operation.set(key, value);
        return operation;
    }
 
    
    public <T> ValueOperations<String, T> setCacheObject(String key, T value, Integer timeout, TimeUnit timeUnit) {
        ValueOperations<String, T> operation = redisTemplate.opsForValue();
        operation.set(key, value, timeout, timeUnit);
        return operation;
    }
 
    
    public <T> T getCacheObject(String key) {
        ValueOperations<String, T> operation = redisTemplate.opsForValue();
        return operation.get(key);
    }
 
    
    public void deleteObject(String key) {
        redisTemplate.delete(key);
    }
 
    
    public void deleteObject(Collection collection) {
        redisTemplate.delete(collection);
    }
 
    
    public <T> ListOperations<String, T> setCacheList(String key, List<T> dataList) {
        ListOperations listOperation = redisTemplate.opsForList();
        if (null != dataList) {
            int size = dataList.size();
            for (int i = 0; i < size; i++) {
                listOperation.leftPush(key, dataList.get(i));
            }
        }
        return listOperation;
    }
 
    
    public <T> List<T> getCacheList(String key) {
        List<T> dataList = new ArrayList<T>();
        ListOperations<String, T> listOperation = redisTemplate.opsForList();
        Long size = listOperation.size(key);
 
        for (int i = 0; i < size; i++) {
            dataList.add(listOperation.index(key, i));
        }
        return dataList;
    }
 
    
    public <T> BoundSetOperations<String, T> setCacheSet(String key, Set<T> dataSet) {
        BoundSetOperations<String, T> setOperation = redisTemplate.boundSetOps(key);
        Iterator<T> it = dataSet.iterator();
        while (it.hasNext()) {
            setOperation.add(it.next());
        }
        return setOperation;
    }
 
    
    public <T> Set<T> getCacheSet(String key) {
        Set<T> dataSet = new HashSet<T>();
        BoundSetOperations<String, T> operation = redisTemplate.boundSetOps(key);
        dataSet = operation.members();
        return dataSet;
    }
 
    
    public <T> HashOperations<String, String, T> setCacheMap(String key, Map<String, T> dataMap) {
        HashOperations hashOperations = redisTemplate.opsForHash();
        if (null != dataMap) {
            for (Map.Entry<String, T> entry : dataMap.entrySet()) {
                hashOperations.put(key, entry.geTKEy(), entry.getValue());
            }
        }
        return hashOperations;
    }
 
    
    public <T> Map<String, T> getCacheMap(String key) {
        Map<String, T> map = redisTemplate.opsForHash().entries(key);
        return map;
    }
 
    
    public Collection<String> keys(String pattern) {
        return redisTemplate.keys(pattern);
    }
 
    
    public boolean  haskey(String key){
        return redisTemplate.hasKey(key);
    }
 
    public Long getExpire(String key){
       return redisTemplate.getExpire(key);
    }
 
 
    public <T> ValueOperations<String, T> setBillObject(String key, List<Map<String, Object>> value) {
        ValueOperations<String, T> operation = redisTemplate.opsForValue();
        operation.set(key, (T) value);
        return operation;
    }
    
    public <T> ValueOperations<String, T> setBillObject(String key, List<Map<String, Object>> value, Integer timeout, TimeUnit timeUnit) {
        ValueOperations<String, T> operation = redisTemplate.opsForValue();
        operation.set(key,(T)value, timeout, timeUnit);
        return operation;
    }
    
    public <T> HashOperations<String, String, T> setCKdBillMap(String key, Map<String, T> dataMap) {
        HashOperations hashOperations = redisTemplate.opsForHash();
        if (null != dataMap) {
            for (Map.Entry<String, T> entry : dataMap.entrySet()) {
                hashOperations.put(key, entry.getKey(), entry.getValue());
            }
        }
        return hashOperations;
    }
}

步骤三、控制层注解使用

    @NoRepeatSubmit
    @PostMapping("insert")
    @OperLog(title = "仓库调整", businessType = BusinessType.INSERT)
    @apiOperation(value = "新增仓库调整单", notes = "")
    public R insert(@RequestBody Cktzd cktzd) {
        System.out.println(JSON.tojsONString(cktzd));
        return R.error("测试阶段!");
    }

 测试结果:

第一次提交

在不间隔15就继续点

不间断的点,只要2次点击之间没有超过15秒,都会抛出请勿重复请求。直到2次点击之间间隔足够才能正常请求! 

到此这篇关于Java后端限制频繁请求和重复提交的实现的文章就介绍到这了,更多相关Java后端限制频繁请求内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: Java后端限制频繁请求和重复提交的实现

本文链接: https://www.lsjlt.com/news/145648.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作