本文小编为大家详细介绍“SpringBoot怎么使用AOP+Redis防止表单重复提交”,内容详细,步骤清晰,细节处理妥当,希望这篇“SpringBoot怎么使用AOP+Redis防止表单重复提交”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
redis:
host: 127.0.0.1
port: 6379
database: 0
password:
# 连接超时时间
timeout: 10s
/**
* 防止表单重复提交注解
*/
@Target(ElementType.METHOD) // 注解的作用目标为方法
@Retention(RetentionPolicy.RUNTIME) // 注解的保留期限为运行时
public @interface PreventDuplicateSubmission {
/**
* 时间(s)
*/
int time() default 3;
}
@Aspect // 表明这是一个切面类
@Component // 表示这是一个Bean
public class DuplicateSubmissionAspect {
@Autowired
private StringRedisTemplate stringRedisTemplate;
// 定义切入点,即标注了@PreventDuplicateSubmission注解的方法
@Pointcut("@annotation(com.example.demo.annotation.PreventDuplicateSubmission)")
public void preventDuplicateSubmission() {
}
@Around("preventDuplicateSubmission()")
public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
assert attributes != null;
HttpServletRequest request = attributes.getRequest();
String requestURI = request.getRequestURI();
String key = requestURI + ":" + JSON.toJSONString(request.getParameterMap());
if (stringRedisTemplate.hasKey(key)) { // 如果Redis中已存在该请求
throw new RuntimeException("请勿重复提交");
}
// 获取注解的参数
PreventDuplicateSubmission formSubmission = ((MethodSignature) pjp.getSignature()).getMethod().getAnnotation(PreventDuplicateSubmission.class);
int time = formSubmission.time();
// 设置请求的key和value,有效期为3秒
stringRedisTemplate.opsForValue().set(key, "1", time, TimeUnit.SECONDS);
return pjp.proceed();
}
}
在上面的代码中,我们使用了Spring Boot提供的StringRedisTemplate
来连接Redis,可以直接通过@Autowired注解来注入该对象。在@Around注解中,我们使用stringRedisTemplate.hasKey()方法来检查Redis中是否已存在该请求,如果存在,则抛出异常;如果不存在,则使用stringRedisTemplate.opsForValue().set()方法将该请求存储到Redis中,同时设置过期时间为3秒。
使用Redis存储请求需要注意以下几点:
Redis需要单独部署,不要将Redis和应用程序部署在同一台机器上。
Redis的性能相对于内存存储方式可能会有所下降,需要根据实际情况进行测试和优化。
如果Redis中出现异常,可能会影响到应用程序的正常运行,需要增加相应的容错机制。
Redis存储请求需要考虑到并发问题,可以使用Redis的分布式锁来解决。
如果应用程序中需要频繁地进行Redis操作,可能会导至Redis的性能下降,因此需要注意优化Redis的配置和使用方式,例如使用Redis Pipeline等技术来提高Redis的性能。
读到这里,这篇“SpringBoot怎么使用AOP+Redis防止表单重复提交”文章已经介绍完毕,想要掌握这篇文章的知识点还需要大家自己动手实践使用过才能领会,如果想了解更多相关内容的文章,欢迎关注亿速云行业资讯频道。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。
原文链接:https://blog.csdn.net/weixin_45626288/article/details/130245284