135-1821-9792

SpringBoot中怎样防止重复提交

这期内容当中小编将会给大家带来有关SpringBoot中怎样防止重复提交,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

成都创新互联专注于海宁企业网站建设,成都响应式网站建设,成都商城网站开发。海宁网站建设公司,为海宁等地区提供建站服务。全流程定制开发,专业设计,全程项目跟踪,成都创新互联专业和态度为您提供的服务

思考逻辑:

1.从数据库方面考虑,数据设计的时候,某些数据有没有唯一性,如果有唯一性,要考虑设置唯一索引,可以避免脏数据。

2.从应用层面考虑,首先判断是单机服务还是分布式服务,则此时需要考虑一些缓存,利用缓存,来保证数据的重复提交。

假设是分布式应用,则可以将用户的信息,例如token和请求的url进行组装在一起,存储到缓存存,例如redis,并设置超时时间为2秒,如此来保证数据的唯一性。

以下是代码实现:

Application.java

package com; import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication; /** * @author www.spring.tsh * @功能描述 防重复提交 * @date 2018-08-26 */@SpringBootApplicationpublic class Application {  public static void main(String[] args) {    SpringApplication.run(Application.class, args);  }}

application.yml

spring: redis:  host: 127.0.0.1  port: 6379  password: 123456

RedisConfig.java

package com.common; import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.data.redis.connection.RedisStandaloneConfiguration;import org.springframework.data.redis.connection.jedis.JedisClientConfiguration;import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;import org.springframework.data.redis.core.RedisTemplate; @Configuration public class RedisConfig {  @Bean  @ConfigurationProperties(prefix = "spring.redis")  public JedisConnectionFactory getConnectionFactory() {    return new JedisConnectionFactory(new RedisStandaloneConfiguration(), JedisClientConfiguration.builder().build());  }   @Bean   RedisTemplate getRedisTemplate() {    RedisTemplate redisTemplate = new RedisTemplate();    redisTemplate.setConnectionFactory(getConnectionFactory());    return redisTemplate;  } }

自定义注解NoRepeatSubmit.java

package com.common; 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) // 运行时有效/** * @功能描述 防止重复提交标记注解 * @author www.srping.tsh * @date 2018-08-26 */public @interface NoRepeatSubmit {}

aop解析注解NoRepeatSubmitAop.java

package com.common; import javax.servlet.http.HttpServletRequest; 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.data.redis.core.RedisTemplate;import org.springframework.data.redis.core.ValueOperations;import org.springframework.stereotype.Component;import org.springframework.web.context.request.RequestContextHolder;import org.springframework.web.context.request.ServletRequestAttributes; @Aspect@Component/** * @功能描述 aop解析注解 * @author www.gaozz.club * @date 2018-11-02 */public class NoRepeatSubmitAop {   private Log logger = LogFactory.getLog(getClass());   @Autowired  private RedisTemplate template;   @Around("execution(* com.example..*Controller.*(..)) && @annotation(nrs)")  public Object arround(ProceedingJoinPoint pjp, NoRepeatSubmit nrs) {    ValueOperations opsForValue = template.opsForValue();    try {      ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();      String sessionId = RequestContextHolder.getRequestAttributes().getSessionId();      HttpServletRequest request = attributes.getRequest();      String key = sessionId + "-" + request.getServletPath();      if (opsForValue.get(key) == null) {// 如果缓存中有这个url视为重复提交        Object o = pjp.proceed();        opsForValue.set(key, 0, 2, TimeUnit.SECONDS);        return o;      } else {        logger.error("重复提交");        return null;      }    } catch (Throwable e) {      e.printStackTrace();      logger.error("验证重复提交时出现未知异常!");      return "{\"code\":-889,\"message\":\"验证重复提交时出现未知异常!\"}";    }   } }

测试类:

package com.example; import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController; import com.common.NoRepeatSubmit; /** * @功能描述 测试Controller * @author www.spring.tsh * @date 2018-08-26 */@RestControllerpublic class TestController {  @RequestMapping("/test")  @NoRepeatSubmit  public String test() {    return ("程序逻辑返回");  } }

上述就是小编为大家分享的SpringBoot中怎样防止重复提交了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注创新互联行业资讯频道。


分享标题:SpringBoot中怎样防止重复提交
本文URL:http://kswsj.com/article/ghodds.html

其他资讯



Copyright © 2009-2022 www.kswsj.com 成都快上网科技有限公司 版权所有 蜀ICP备19037934号