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

Spring Boot如何防止重復提交

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

場景:同一個用戶在2秒內對同一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 ("程序邏輯返回");
  }
 
}

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

相關文章

  • Java實時獲取基金收益項目源碼分享

    Java實時獲取基金收益項目源碼分享

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

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

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

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

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

    詳解Spring Cloud Zuul 服務網關

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

    Java網絡編程實例——簡單模擬在線聊天

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

    詳解Java5、Java6、Java7的新特性

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

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

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

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

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

    java中關于return返回值的用法詳解

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

    springboot 集成pgsql+mybatis plus的詳細步驟

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

最新評論

五家渠市| 兰考县| 山东省| 漯河市| 古交市| 江西省| 阜新市| 蕉岭县| 阜康市| 内黄县| 红原县| 万全县| 高碑店市| 徐水县| 南陵县| 库尔勒市| 海城市| 库伦旗| 广宗县| 如东县| 图们市| 虎林市| 建德市| 汪清县| 遂溪县| 广饶县| 定远县| 平遥县| 武乡县| 清涧县| 麻城市| 辽宁省| 孟津县| 呈贡县| 吉水县| 运城市| 绩溪县| 顺平县| 含山县| 广丰县| 新乡县|