第一种方法:单个防止
在spring boot应用中使用redis来防止表单的重复提交,可以通过以下几个步骤来实现:
步骤 1:添加依赖
确保你的项目中添加了spring boot starter data redis和spring boot starter web依赖。
在pom.xml文件中添加以下依赖:
<dependencies>
<dependency>
<groupid>org.springframework.boot</groupid>
<artifactid>spring-boot-starter-data-redis</artifactid>
</dependency>
<dependency>
<groupid>org.springframework.boot</groupid>
<artifactid>spring-boot-starter-web</artifactid>
</dependency>
</dependencies>
步骤 2:配置redis
在application.properties或application.yml中配置redis服务器的信息:
spring.redis.host=localhost spring.redis.port=6379
步骤 3:创建一个工具类处理redis操作
创建一个工具类来封装redis的一些常用操作,比如设置键值对、获取键值对等。
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.data.redis.core.stringredistemplate;
import org.springframework.stereotype.component;
@component
public class redisutil {
@autowired
private stringredistemplate stringredistemplate;
public void set(string key, string value) {
stringredistemplate.opsforvalue().set(key, value);
}
public string get(string key) {
return stringredistemplate.opsforvalue().get(key);
}
public void delete(string key) {
stringredistemplate.delete(key);
}
}
步骤 4:实现防重逻辑
在controller中使用上面的工具类来实现防重逻辑。
通常的做法是在请求开始时检查redis中是否存在该请求的唯一标识(如用户id + 时间戳),如果不存在则允许请求继续,并将此标识存入redis;如果存在,则直接返回错误信息。
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.web.bind.annotation.postmapping;
import org.springframework.web.bind.annotation.restcontroller;
@restcontroller
public class mycontroller {
@autowired
private redisutil redisutil;
@postmapping("/submit")
public string submit(@requestparam("userid") string userid) {
// 使用用户id和时间戳作为key,防止并发下的重复提交
string key = "form_submit_" + userid + "_" + system.currenttimemillis();
if (redisutil.get(key) != null) {
return "请求重复,请勿重复提交!";
} else {
// 执行业务逻辑
// ...
// 将key存入redis并设置过期时间
redisutil.set(key, "1", 5, timeunit.minutes);
return "提交成功!";
}
}
}
请注意:
在实际应用中,你可能需要根据具体需求调整上述代码,比如更改存储在redis中的键的生成方式、设置更合理的过期时间等。
此外,为了提高性能和避免并发问题,可以考虑使用redis的原子操作,例如setnx命令。
第二种方法:aop全局方法
使用aop(面向切面编程)来统一处理防重复提交是一种很好的实践,它能减少重复代码并保持业务逻辑的清晰性。下面是如何使用spring aop来实现这一功能的示例。
首先,你需要在项目中添加spring aop的支持,通常情况下spring boot项目已经默认包含aop支持。
步骤 1:创建切面类
创建一个切面类,用于定义防重提交的逻辑。
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.data.redis.core.stringredistemplate;
import org.springframework.stereotype.component;
import java.util.concurrent.timeunit;
@aspect
@component
public class antiduplicatesubmissionaspect {
private static final string key_prefix = "anti_duplicate_";
@autowired
private stringredistemplate stringredistemplate;
@around("@annotation(antiduplicatesubmission)")
public object antiduplicatesubmit(proceedingjoinpoint joinpoint, antiduplicatesubmission antiduplicatesubmission) throws throwable {
// 从注解中获取参数名
string paramname = antiduplicatesubmission.value();
// 获取方法参数
object[] args = joinpoint.getargs();
// 假设参数位置已知,或者通过参数名获取参数值
string userid = (string) args[0];
// 生成唯一标识符
string key = key_prefix + userid + "_" + system.currenttimemillis();
// 检查是否已经存在
if (stringredistemplate.haskey(key)) {
throw new runtimeexception("请求重复,请勿重复提交!");
}
// 设置过期时间,例如5分钟
stringredistemplate.opsforvalue().set(key, "1", 5, timeunit.minutes);
try {
return joinpoint.proceed();
} finally {
// 清理key,这一步在大多数情况下可选,因为设置了过期时间
stringredistemplate.delete(key);
}
}
}
步骤 2:创建自定义注解
创建一个自定义注解,用于标记需要防重提交的控制器方法。
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 antiduplicatesubmission {
string value();
}
步骤 3:应用注解
在需要防重提交的controller方法上应用这个注解。
import org.springframework.web.bind.annotation.postmapping;
import org.springframework.web.bind.annotation.restcontroller;
@restcontroller
public class mycontroller {
@postmapping("/submit")
@antiduplicatesubmission(value = "userid")
public string submit(@requestparam("userid") string userid) {
// 执行业务逻辑
// ...
return "提交成功!";
}
}
这样,每次有请求到达带有@antiduplicatesubmission注解的方法时,aop切面会先执行防重检查,如果发现重复提交,则阻止业务逻辑执行并抛出异常。
注意:
这里的antiduplicatesubmission注解的value属性应该与请求参数名匹配,以便正确地获取到用户id或其他用于生成唯一标识符的参数。
同时,使用system.currenttimemillis()生成的时间戳作为一部分唯一标识符可能在高并发场景下存在并发问题,你可能需要一个更安全的唯一生成策略,例如uuid或者结合用户id、请求时间以及随机数等生成一个更复杂的唯一标识符。
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。
发表评论