当前位置:   article > 正文

java 表单重复提交,进行幂等性控制,防止_java接口幂等性,防止重复提交

java接口幂等性,防止重复提交

1.通过注解方式实现,注解加拦截器方式

注解代码:

  1. @Target(ElementType.METHOD)
  2. @Retention(RetentionPolicy.RUNTIME)
  3. @Documented
  4. public @interface RepeatSubmit {
  5. /**
  6. * 过期时间,单位毫秒
  7. */
  8. long expireTime() default 500L;
  9. }

注解工具类:

  1. package com.psbchn.config;
  2. import com.psbchn.handler.annotation.RepeatSubmit;
  3. import com.psbchn.handler.exception.base.BaseException;
  4. import lombok.extern.slf4j.Slf4j;
  5. import org.aspectj.lang.ProceedingJoinPoint;
  6. import org.aspectj.lang.annotation.Around;
  7. import org.aspectj.lang.annotation.Aspect;
  8. import org.aspectj.lang.annotation.Pointcut;
  9. import org.aspectj.lang.reflect.MethodSignature;
  10. import org.springframework.data.redis.core.RedisTemplate;
  11. import org.springframework.stereotype.Component;
  12. import org.springframework.web.context.request.RequestContextHolder;
  13. import org.springframework.web.context.request.ServletRequestAttributes;
  14. import javax.annotation.Resource;
  15. import javax.servlet.http.HttpServletRequest;
  16. import java.lang.reflect.Method;
  17. import java.util.concurrent.TimeUnit;
  18. /**
  19. * 防止表单重复提交切面
  20. *
  21. * @Author lwz
  22. * @Date 2023/11/22 10:13
  23. */
  24. @Slf4j
  25. @Aspect
  26. @Component
  27. public class RepeatSubmitAspect {
  28. private static final String KEY_PREFIX = "repeat_submit:";
  29. @Resource
  30. private RedisTemplate redisTemplate;
  31. @Pointcut("@annotation(com.psbchn.handler.annotation.RepeatSubmit)")
  32. public void repeatSubmit() {}
  33. @Around("repeatSubmit()")
  34. public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
  35. //joinPoint获取方法对象
  36. Method method = ((MethodSignature) joinPoint.getSignature()).getMethod();
  37. //获取方法上的@RepeatSubmit注解
  38. RepeatSubmit annotation = method.getAnnotation(RepeatSubmit.class);
  39. //获取HttpServletRequest对象,以获取请求uri
  40. ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
  41. HttpServletRequest request = requestAttributes.getRequest();
  42. String uri = request.getRequestURI();
  43. //拼接Redis的key,这里只是简单根据uri来判断是否重复提交。可以根据自己业务调整,比如根据用户id或者请求token等
  44. String cacheKey = KEY_PREFIX.concat(uri);
  45. Boolean flag = null;
  46. try {
  47. //借助setIfAbsent(),key不存在才能设值成功
  48. flag = redisTemplate.opsForValue().setIfAbsent(cacheKey, "", annotation.expireTime(), TimeUnit.MILLISECONDS);
  49. } catch (Exception e) {
  50. //如果Redis不可用,则打印日志记录,但依然对请求放行
  51. log.error("", e);
  52. return joinPoint.proceed();
  53. }
  54. //Redis可用的情况,如果flag=true说明单位时间内这是第一次请求,放行
  55. if (flag) {
  56. return joinPoint.proceed();
  57. } else {
  58. //进入else说明单位时间内进行了多次请求,则拦截请求并提示稍后重试
  59. throw new BaseException("系统繁忙,请稍后重试");
  60. }
  61. }
  62. }

使用方法 :

  1. /**
  2. * 新增文件分类
  3. *
  4. * @param fileCategoryVo
  5. * @return true
  6. */
  7. @RepeatSubmit
  8. @ResponseBody
  9. @ApiOperation(value = "文件分类新增", notes = "", httpMethod = "POST")
  10. @RequestMapping(value = "/insert", produces = {MediaType.APPLICATION_JSON_VALUE}, consumes = {MediaType.APPLICATION_JSON_VALUE}, method = RequestMethod.POST)
  11. public RestResponse<Boolean> insertFileCategory(@RequestBody FileCategoryVo fileCategoryVo) {
  12. boolean flag = fileCategoryService.insertFileCategory(fileCategoryVo);
  13. if (flag) {
  14. return RestResponse.ok("增加成功");
  15. } else {
  16. return RestResponse.fail("增加失败");
  17. }
  18. }

2.通过幂等性控制

这个不懂啊~~谁知道怎么做,推荐一个链接

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/550805
推荐阅读
相关标签
  

闽ICP备14008679号