一、产生原因

对于重复提交的问题,主要由于重复点击或者网络重发请求, 我要先了解产生原因几种方式:

  • 点击提交按钮两次;
  • 点击刷新按钮;
  • 使用浏览器后退按钮重复之前的操作,导致重复提交表单;
  • 使用浏览器历史记录重复提交表单;
  • 浏览器重复的HTTP请;
  • nginx重发等情况;
  • 分布式RPC的try重发等点击提交按钮两次;
  • 等… …

二、幂等

对于重复提交的问题 主要涉及到时 幂等 问题,那么先说一下什么是幂等。

幂等:F(F(X)) = F(X)多次运算结果一致;简单点说就是对于完全相同的操作,操作一次与操作多次的结果是一样的。
在开发中,我们都会涉及到对数据库操作。例如:

select 查询天然幂等

delete 删除也是幂等,删除同一个多次效果一样

update 直接更新某个值(如:状态 字段固定值),幂等

update 更新累加操作(如:商品数量 字段),非幂等

(可以采用简单的乐观锁和悲观锁 个人更喜欢乐观锁。

乐观锁:数据库表加version字段的方式;

悲观锁:用了 select…for update 的方式,* 要使用悲观锁,我们必须关闭mysql数据库的自动提交属性。

这种在大数据量和高并发下效率依赖数据库硬件能力,可针对并发量不高的非核心业务;)

insert 非幂等操作,每次新增一条 重点 (数据库简单方案:可采取数据库唯一索引方式;这种在大数据量和高并发下效率依赖数据库硬件能力,可针对并发量不高的非核心业务;)

三、解决方案

1. 方案对比

序号 前端/后端 方案 优点 缺点 代码实现
1) 前端 前端js提交后禁止按钮,返回结果后解禁等 简单 方便 只能控制页面,通过工具可绕过不安全
2) 后端 提交后重定向到其他页面,防止用户F5和浏览器前进后退等重复提交问题 简单 方便 体验不好,适用部分场景,若是遇到网络问题 还会出现
3) 后端 在表单、session、token 放入唯一标识符(如:UUID),每次操作时,保存标识一定时间后移除,保存期间有相同的标识就不处理或提示 相对简单 表单:有时需要前后端协商配合; session、token:加大服务性能开销
4) 后端 ConcurrentHashMap 、LRUMap 、google Cache 都是采用唯一标识(如:用户ID 请求路径 参数) 相对简单 适用于单机部署的应用 见下
5) 后端 redis 是线程安全的,可以实现redis分布式锁。设置唯一标识(如:用户ID 请求路径 参数)当做key ,value值可以随意(推荐设置成过期的时间点),在设置key的过期时间 单机、分布式、高并发都可以决绝 相对复杂需要部署维护redis 见下

2. 代码实现

4). google cache 代码实现 注解方式 Single lock

pom.xml 引入

<dependency>
   <groupId>com.google.guava</groupId>
    <artifactId>guava</artifactId>
    <version>28.2-jre</version>
</dependency>

配置文件 .yml

resubmit:
  local:
    timeOut: 30

实现代码

import java.lang.annotation.*;

@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
public @interface LocalLock {
	
}
import com.alibaba.fastjson.JSONObject;
import com.example.mydemo.common.utils.IpUtils;
import com.example.mydemo.common.utils.Result;
import com.example.mydemo.common.utils.SecurityUtils;
import com.example.mydemo.common.utils.sign.MyMD5Util;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author: xx
 * @description: 单机放重复提交
 */
@Data
@Aspect
@Configuration
public class LocalLockMethodInterceptor {

    @Value("${spring.profiles.active}")
    private String springProfilesActive;
    @Value("${spring.application.name}")
    private String springApplicationName;


    private static int expireTimeSecond =5;

    @Value("${resubmit:local:timeOut}")
    public void setExpireTimeSecond(int expireTimeSecond) {
        LocalLockMethodInterceptor.expireTimeSecond = expireTimeSecond;
    }
    //定义缓存,设置最大缓存数及过期日期
    private static final Cache<String,Object> CACHE =
            CacheBuilder.newBuilder().maximumSize(1000).expireAfterWrite(expireTimeSecond, TimeUnit.SECONDS).build();

    @Around("execution(public * *(..))  && @annotation(com.example.mydemo.common.interceptor.annotation.LocalLock)")
    public Object interceptor(ProceedingJoinPoint joinPoint){

        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
//        LocalLock localLock = method.getAnnotation(LocalLock.class);
        try{
        String key = getLockUniqueKey(signature,joinPoint.getArgs());
        if(CACHE.getIfPresent(key) != null){
            return Result.fail("不允许重复提交,请稍后再试");
        }
        CACHE.put(key,key);

            return joinPoint.proceed();
        }catch (Throwable throwable){
            throw new RuntimeException(throwable.getMessage());
        }finally {

        }
    }



    /**
     * 获取唯一标识key
     *
     * @param methodSignature
     * @param args
     * @return
     */
    private String getLockUniqueKey(MethodSignature methodSignature, Object[] args) throws NoSuchAlgorithmException {
        //请求uri, 获取类名称,方法名称
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) requestAttributes;
        HttpServletRequest request = servletRequestAttributes.getRequest();
//        HttpServletResponse responese = servletRequestAttributes.getResponse();

        //获取用户信息
        String userMsg = SecurityUtils.getUsername(); //获取登录用户名称
        //1.判断用户是否登录
        if (StringUtils.isEmpty(userMsg)) { //未登录用户获取真实ip
            userMsg = IpUtils.getIpAddr(request);
        }
        String hash = "";
        List list = new ArrayList();
        if (args.length > 0) {
            String[] parameterNames = methodSignature.getParameterNames();
            for (int i = 0; i < parameterNames.length; i  ) {
                Object obj = args[i];
                list.add(obj);
            }
            hash = JSONObject.toJSONString(list);

        }
        //项目名称   环境编码   获取类名称   方法名称   唯一key
        String key = "locallock:"   springApplicationName   ":"   springProfilesActive   ":"   userMsg   ":"   request.getRequestURI();
        if (StringUtils.isNotEmpty(key)) {
            key = key   ":"   hash;
        }
        key = MyMD5Util.getMD5(key);
        return key;
    }

使用:

	@LocalLock
    public void save(@RequestBody User user) {
       
    }

5)redis

pom.xml 引入

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

.yml文件 redis 配置

spring:
  redis:
    host: localhost
    port: :6379
    password: 123456
import java.lang.annotation.*;

@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
public @interface RedisLock {

    int expire() default 5;
}
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.heshu.sz.blockchain.utonhsbs.common.utils.MyMD5Util;
import com.heshu.sz.blockchain.utonhsbs.common.utils.SecurityUtils;
import com.heshu.sz.blockchain.utonhsbs.common.utils.ip.IpUtils;
import com.heshu.sz.blockchain.utonhsbs.framework.interceptor.annotation.RedisLock;
import com.heshu.sz.blockchain.utonhsbs.framework.system.domain.BaseResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
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.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.script.DefaultRedisScript;
import org.springframework.data.redis.core.script.RedisScript;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author :xx
 * @description:
 * @date : 2022/7/1 9:41
 */
@Slf4j
@Aspect
@Configuration
public class RedisLockMethodInterceptor {

    @Value("${spring.profiles.active}")
    private String springProfilesActive;
    @Value("${spring.application.name}")
    private String springApplicationName;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;


    @Pointcut("@annotation(com.heshu.sz.blockchain.utonhsbs.framework.interceptor.annotation.RedisLock)")
    public void point() {
    }

    @Around("point()")
    public Object doaround(ProceedingJoinPoint joinPoint) {

        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        RedisLock localLock = method.getAnnotation(RedisLock.class);
        try {
            String lockUniqueKey = getLockUniqueKey(signature, joinPoint.getArgs());

            Integer expire = localLock.expire();
            if (expire < 0) {
                expire = 5;
            }
            ArrayList<String> keys = Lists.newArrayList(lockUniqueKey);
            String result = stringRedisTemplate.execute(setNxWithExpireTime, keys, expire.toString());
            if (!"ok".equalsIgnoreCase(result)) {//不存在
                return BaseResult.error("不允许重复提交,请稍后再试");
            }
            return joinPoint.proceed();
        } catch (Throwable throwable) {
            throw new RuntimeException(throwable.getMessage());
        }
    }

    /**
     * lua脚本
     */
    private RedisScript<String> setNxWithExpireTime = new DefaultRedisScript<>(
            "return redis.call('set', KEYS[1], 1, 'ex', ARGV[1], 'nx');",
            String.class
    );


    /**
     * 获取唯一标识key
     *
     * @param methodSignature
     * @param args
     * @return
     */
    private String getLockUniqueKey(MethodSignature methodSignature, Object[] args) throws NoSuchAlgorithmException {
        //请求uri, 获取类名称,方法名称
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) requestAttributes;
        HttpServletRequest request = servletRequestAttributes.getRequest();
//        HttpServletResponse responese = servletRequestAttributes.getResponse();

        //获取用户信息
        String userMsg = SecurityUtils.getUsername(); //获取登录用户名称
        //1.判断用户是否登录
        if (StringUtils.isEmpty(userMsg)) { //未登录用户获取真实ip
            userMsg = IpUtils.getIpAddr(request);
        }
        String hash = "";
        List list = new ArrayList();
        if (args.length > 0) {
            String[] parameterNames = methodSignature.getParameterNames();
            for (int i = 0; i < parameterNames.length; i  ) {
                Object obj = args[i];
                list.add(obj);
            }
            String param = JSONObject.toJSONString(list);
            hash = MyMD5Util.getMD5(param);
        }
        //项目名称   环境编码   获取类名称   加密参数
        String key = "lock:"   springApplicationName   ":"   springProfilesActive   ":"   userMsg   ":"   request.getRequestURI();
        if (StringUtils.isNotEmpty(key)) {
            key = key   ":"   hash;
        }

        return key;
    }

使用

	@RedisLock
    public void save(@RequestBody User user) {
       
    }

总结

到此这篇关于java开发中防止重复提交的几种解决方案的文章就介绍到这了,更多相关java防止重复提交内容请搜索Devmax以前的文章或继续浏览下面的相关文章希望大家以后多多支持Devmax!

java开发中防止重复提交的几种解决方案的更多相关文章

  1. Java利用POI实现导入导出Excel表格

    这篇文章主要为大家详细介绍了Java利用POI实现导入导出Excel表格,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  2. Java 阻塞队列BlockingQueue详解

    本文详细介绍了BlockingQueue家庭中的所有成员,包括他们各自的功能以及常见使用场景,通过实例代码介绍了Java 阻塞队列BlockingQueue的相关知识,需要的朋友可以参考下

  3. Java Bean 作用域及它的几种类型介绍

    这篇文章主要介绍了Java Bean作用域及它的几种类型介绍,Spring框架作为一个管理Bean的IoC容器,那么Bean自然是Spring中的重要资源了,那Bean的作用域又是什么,接下来我们一起进入文章详细学习吧

  4. Java实现世界上最快的排序算法Timsort的示例代码

    Timsort 是一个混合、稳定的排序算法,简单来说就是归并排序和二分插入排序算法的混合体,号称世界上最好的排序算法。本文将详解Timsort算法是定义与实现,需要的可以参考一下

  5. Java日期工具类的封装详解

    在日常的开发中,我们难免会对日期格式化,对日期进行计算,对日期进行校验,为了避免重复写这些琐碎的逻辑,我这里封装了一个日期工具类,方便以后使用,直接复制代码到项目中即可使用,需要的可以参考一下

  6. Java设计模式之模板方法模式Template Method Pattern详解

    在我们实际开发中,如果一个方法极其复杂时,如果我们将所有的逻辑写在一个方法中,那维护起来就很困难,要替换某些步骤时都要重新写,这样代码的扩展性就很差,当遇到这种情况就要考虑今天的主角——模板方法模式

  7. Java 中 Class Path 和 Package的使用详解

    这篇文章主要介绍了Java 中 Class Path和Package的使用详解,文章围绕主题展开详细的内容介绍,具有一定的参考价值,需要的朋友可以参考一下

  8. java SpringBoot 分布式事务的解决方案(JTA+Atomic+多数据源)

    这篇文章主要介绍了java SpringBoot 分布式事务的解决方案(JTA+Atomic+多数据源),文章围绕主题展开详细的内容介绍,具有一定的参考价值,感兴趣的小伙伴可以参考一下

  9. Java一维数组和二维数组元素默认初始化值的判断方式

    这篇文章主要介绍了Java一维数组和二维数组元素默认初始化值的判断方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

  10. java实现emqx设备上下线监听详解

    这篇文章主要为大家介绍了java实现emqx设备上下线监听详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪

随机推荐

  1. 基于EJB技术的商务预订系统的开发

    用EJB结构开发的应用程序是可伸缩的、事务型的、多用户安全的。总的来说,EJB是一个组件事务监控的标准服务器端的组件模型。基于EJB技术的系统结构模型EJB结构是一个服务端组件结构,是一个层次性结构,其结构模型如图1所示。图2:商务预订系统的构架EntityBean是为了现实世界的对象建造的模型,这些对象通常是数据库的一些持久记录。

  2. Java利用POI实现导入导出Excel表格

    这篇文章主要为大家详细介绍了Java利用POI实现导入导出Excel表格,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  3. Mybatis分页插件PageHelper手写实现示例

    这篇文章主要为大家介绍了Mybatis分页插件PageHelper手写实现示例,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪

  4. (jsp/html)网页上嵌入播放器(常用播放器代码整理)

    网页上嵌入播放器,只要在HTML上添加以上代码就OK了,下面整理了一些常用的播放器代码,总有一款适合你,感兴趣的朋友可以参考下哈,希望对你有所帮助

  5. Java 阻塞队列BlockingQueue详解

    本文详细介绍了BlockingQueue家庭中的所有成员,包括他们各自的功能以及常见使用场景,通过实例代码介绍了Java 阻塞队列BlockingQueue的相关知识,需要的朋友可以参考下

  6. Java异常Exception详细讲解

    异常就是不正常,比如当我们身体出现了异常我们会根据身体情况选择喝开水、吃药、看病、等 异常处理方法。 java异常处理机制是我们java语言使用异常处理机制为程序提供了错误处理的能力,程序出现的错误,程序可以安全的退出,以保证程序正常的运行等

  7. Java Bean 作用域及它的几种类型介绍

    这篇文章主要介绍了Java Bean作用域及它的几种类型介绍,Spring框架作为一个管理Bean的IoC容器,那么Bean自然是Spring中的重要资源了,那Bean的作用域又是什么,接下来我们一起进入文章详细学习吧

  8. 面试突击之跨域问题的解决方案详解

    跨域问题本质是浏览器的一种保护机制,它的初衷是为了保证用户的安全,防止恶意网站窃取数据。那怎么解决这个问题呢?接下来我们一起来看

  9. Mybatis-Plus接口BaseMapper与Services使用详解

    这篇文章主要为大家介绍了Mybatis-Plus接口BaseMapper与Services使用详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪

  10. mybatis-plus雪花算法增强idworker的实现

    今天聊聊在mybatis-plus中引入分布式ID生成框架idworker,进一步增强实现生成分布式唯一ID,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

返回
顶部