最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Spring Boot如何防止重復(fù)提交

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

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

思考邏輯:

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

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

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

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

Application.java

package com;
 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
 
/**
 * @author www.spring.tsh
 * @功能描述 防重復(fù)提交
 * @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) // 運(yùn)行時(shí)有效
/**
 * @功能描述 防止重復(fù)提交標(biāo)記注解
 * @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) {// 如果緩存中有這個(gè)url視為重復(fù)提交
        Object o = pjp.proceed();
        opsForValue.set(key, 0, 2, TimeUnit.SECONDS);
        return o;
      } else {
        logger.error("重復(fù)提交");
        return null;
      }
    } catch (Throwable e) {
      e.printStackTrace();
      logger.error("驗(yàn)證重復(fù)提交時(shí)出現(xiàn)未知異常!");
      return "{\"code\":-889,\"message\":\"驗(yàn)證重復(fù)提交時(shí)出現(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)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java實(shí)時(shí)獲取基金收益項(xiàng)目源碼分享

    Java實(shí)時(shí)獲取基金收益項(xiàng)目源碼分享

    這篇文章主要介紹了Java實(shí)時(shí)獲取基金收益項(xiàng)目源碼分享,主要包括JAVA爬取天天基金網(wǎng)數(shù)據(jù)使用實(shí)例、應(yīng)用技巧、基本知識(shí)點(diǎn)總結(jié)和需要注意事項(xiàng),需要的朋友可以參考下
    2021-03-03
  • SpringBoot項(xiàng)目中訪問HTML頁面的實(shí)現(xiàn)示例

    SpringBoot項(xiàng)目中訪問HTML頁面的實(shí)現(xiàn)示例

    本文主要介紹了SpringBoot項(xiàng)目中訪問HTML頁面的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-08-08
  • Java訪問者模式實(shí)現(xiàn)優(yōu)雅的對象結(jié)構(gòu)處理

    Java訪問者模式實(shí)現(xiàn)優(yōu)雅的對象結(jié)構(gòu)處理

    Java訪問者模式是一種行為型設(shè)計(jì)模式,它通過將數(shù)據(jù)結(jié)構(gòu)和數(shù)據(jù)操作分離,實(shí)現(xiàn)對復(fù)雜對象結(jié)構(gòu)的處理。它將數(shù)據(jù)結(jié)構(gòu)中的每個(gè)元素都轉(zhuǎn)換為訪問者能夠識(shí)別的形式,從而使得數(shù)據(jù)操作可以在不影響數(shù)據(jù)結(jié)構(gòu)的前提下進(jìn)行擴(kuò)展和變化
    2023-04-04
  • 詳解Spring Cloud Zuul 服務(wù)網(wǎng)關(guān)

    詳解Spring Cloud Zuul 服務(wù)網(wǎng)關(guān)

    本篇文章主要介紹了詳解Spring Cloud Zuul 服務(wù)網(wǎng)關(guān),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-12-12
  • Java網(wǎng)絡(luò)編程實(shí)例——簡單模擬在線聊天

    Java網(wǎng)絡(luò)編程實(shí)例——簡單模擬在線聊天

    學(xué)了java網(wǎng)絡(luò),也是該做個(gè)小案例來鞏固一下了。本次案例將使用UDP和多線程模擬即時(shí)聊天,簡單練練手。
    2021-05-05
  • 詳解Java5、Java6、Java7的新特性

    詳解Java5、Java6、Java7的新特性

    本編文章詳細(xì)介紹了Java5、Java6、Java7的新特性,需要的朋友可以參考下
    2017-04-04
  • Java和C語言分別實(shí)現(xiàn)水仙花數(shù)及拓展代碼

    Java和C語言分別實(shí)現(xiàn)水仙花數(shù)及拓展代碼

    這篇文章主要介紹了分別用Java和C語言實(shí)現(xiàn)水仙花數(shù),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-11-11
  • 詳細(xì)分析Java并發(fā)集合LinkedBlockingQueue的用法

    詳細(xì)分析Java并發(fā)集合LinkedBlockingQueue的用法

    這篇文章主要介紹了詳細(xì)分析Java并發(fā)集合LinkedBlockingQueue的用法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-04-04
  • java中關(guān)于return返回值的用法詳解

    java中關(guān)于return返回值的用法詳解

    在本篇文章里小編給大家整理的是一篇關(guān)于java中關(guān)于return返回值的用法詳解內(nèi)容,有興趣的朋友們可以學(xué)習(xí)參考下。
    2020-12-12
  • springboot 集成pgsql+mybatis plus的詳細(xì)步驟

    springboot 集成pgsql+mybatis plus的詳細(xì)步驟

    集成 Spring Boot、PostgreSQL 和 MyBatis Plus 的步驟與 MyBatis 類似,只不過在 MyBatis Plus 中提供了更多的便利功能,如自動(dòng)生成 SQL、分頁查詢、Wrapper 查詢等,下面分步驟給大家介紹springboot 集成pgsql+mybatis plus的過程,感興趣的朋友一起看看吧
    2023-12-12

最新評論

定陶县| 永仁县| 东港市| 卢龙县| 葫芦岛市| 贺州市| 太原市| 莱芜市| 石家庄市| 讷河市| 莱州市| 东城区| 繁昌县| 华池县| 辽中县| 富顺县| 鄢陵县| 东源县| 陆丰市| 洛阳市| 望都县| 深州市| 南宁市| 泰和县| 松原市| 南投市| 甘孜县| 安丘市| 壤塘县| 颍上县| 无为县| 和平县| 荔波县| 定远县| 武汉市| 高尔夫| 芜湖市| 武功县| 济宁市| 崇仁县| 桃源县|