欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring Boot如何防止重復提交

 更新時間:2019年08月06日 10:25:09   作者:談胖胖  
這篇文章主要為大家詳細介紹了Spring Boot如何防止重復提交,具有一定的參考價值,感興趣的小伙伴們可以參考一下

場景:同一個用戶在2秒內(nèi)對同一URL的提交視為重復提交。

思考邏輯:

1.從數(shù)據(jù)庫方面考慮,數(shù)據(jù)設計的時候,某些數(shù)據(jù)有沒有唯一性,如果有唯一性,要考慮設置唯一索引,可以避免臟數(shù)據(jù)。

2.從應用層面考慮,首先判斷是單機服務還是分布式服務,則此時需要考慮一些緩存,利用緩存,來保證數(shù)據(jù)的重復提交。

假設是分布式應用,則可以將用戶的信息,例如token和請求的url進行組裝在一起,存儲到緩存存,例如redis,并設置超時時間為2秒,如此來保證數(shù)據(jù)的唯一性。

以下是代碼實現(xiàn):

Application.java

package com;
 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
 
/**
 * @author www.spring.tsh
 * @功能描述 防重復提交
 * @date 2018-08-26
 */
@SpringBootApplication
public 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
  <K, V> RedisTemplate<K, V> getRedisTemplate() {
    RedisTemplate<K, V> redisTemplate = new RedisTemplate<K, V>();
    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<String, Integer> template;
 
  @Around("execution(* com.example..*Controller.*(..)) && @annotation(nrs)")
  public Object arround(ProceedingJoinPoint pjp, NoRepeatSubmit nrs) {
    ValueOperations<String, Integer> 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("驗證重復提交時出現(xiàn)未知異常!");
      return "{\"code\":-889,\"message\":\"驗證重復提交時出現(xiàn)未知異常!\"}";
    }
 
  }
 
}

測試類:

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
 */
@RestController
public class TestController {
  @RequestMapping("/test")
  @NoRepeatSubmit
  public String test() {
    return ("程序邏輯返回");
  }
 
}

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

最新評論