亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Springboot利用Redis實現(xiàn)接口冪等性攔截

 更新時間:2022年06月23日 15:58:55   作者:小目標青年  
這篇文章主要為大家介紹了Springboot如何利用Redis實現(xiàn)接口冪等性攔截。本文將通過自定義注解+redis+攔截器+MD5?實現(xiàn),感興趣的可以了解一下

前言

近期一個老項目出現(xiàn)了接口冪等性 校驗問題,前端加了按鈕置灰,

依然被人拉著接口參數(shù)一頓輸出,還是重復調(diào)用了接口,小陳及時趕到現(xiàn)場,通過復制粘貼,完成了后端接口冪等性調(diào)用校驗。

以前寫過一篇關(guān)于接口簡單限流防止重復調(diào)用的,但是跟該篇還是不一樣的,該篇的角度是接口和參數(shù)整體一致才當做重復。

簡單限流:Springboot使用redis實現(xiàn)接口Api限流的實例

該篇內(nèi)容:

實現(xiàn)接口調(diào)用的冪等性校驗

方案 :自定義注解+redis+攔截器+MD5 實現(xiàn)

草圖,意會(用戶標識不是必要,看業(yè)務場景是針對個人還是只針對接口&參數(shù)):

話不多說,開始實戰(zhàn)。

PS: 前排提醒,如果你還不知道怎么springboot整合redis,可以先去看下redis使用系列的 一、二。

SpringBoot中對應2.0.x版本的Redis配置詳解

SpringBoot整合Redis之編寫RedisConfig

正文

自定義注解 怎么玩的 :      

①標記哪個接口需要進行冪等性攔截        

②每個接口可以要求冪等性范圍時間不一樣,舉例:可以2秒內(nèi),可以3秒內(nèi),時間自己傳        

③ 一旦觸發(fā)了,提示語可以不同 ,舉例:VIP的接口,普通用戶的接口,提示語不一樣(開玩笑)

效果:

實戰(zhàn)開始

核心三件套

注解、攔截器、攔截器配置

① RepeatDaMie.java

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
 
/**
 * @Author: JCccc
 * @Date: 2022-6-13 9:04
 * @Description:  自定義注解,防止重復提交
 */
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface RepeatDaMie {
 
    /**
     * 時間ms限制
     */
    public int second() default 1;
 
    /**
     * 提示消息
     */
    public String describe() default "重復提交了,兄弟";
    
}

②ApiRepeatInterceptor.java

import com.example.repeatdemo.annotation.RepeatDaMie;
import com.example.repeatdemo.util.ContextUtil;
import com.example.repeatdemo.util.Md5Encrypt;
import com.example.repeatdemo.util.RedisUtils;
import com.example.repeatdemo.wrapper.CustomHttpServletRequestWrapper;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Objects;
 
/**
 * @Author: JCccc
 * @Date: 2022-6-15 9:11
 * @Description: 接口冪等性校驗攔截器
 */
@Component
public class ApiRepeatInterceptor implements HandlerInterceptor {
 
    private final Logger log = LoggerFactory.getLogger(this.getClass());
 
    private static final String POST="POST";
    private static final String GET="GET";
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        try {
            if (handler instanceof HandlerMethod) {
                HandlerMethod handlerMethod = (HandlerMethod) handler;
                // 獲取RepeatDaMie注解
                RepeatDaMie repeatDaMie = handlerMethod.getMethodAnnotation(RepeatDaMie.class);
                if (null==repeatDaMie) {
                    return true;
                }
                //限制的時間范圍
                int seconds = repeatDaMie.second();
                //這個用戶唯一標識,可以自己細微調(diào)整,是userId還是token還是sessionId還是不需要
                String userUniqueKey = request.getHeader("userUniqueKey");
                String method = request.getMethod();
                String apiParams = "";
                if (GET.equals(method)){
                    log.info("GET請求來了");
                    apiParams = new ObjectMapper().writeValueAsString(request.getParameterMap());
                }else if (POST.equals(method)){
                    log.info("POST請求來了");
                    CustomHttpServletRequestWrapper wrapper = (CustomHttpServletRequestWrapper) request;
                    apiParams = wrapper.getBody();
                }
               log.info("當前參數(shù)是:{}",apiParams);
                // 存儲key
                String keyRepeatDaMie = Md5Encrypt.md5(userUniqueKey+request.getServletPath()+apiParams) ;
                RedisUtils redisUtils = ContextUtil.getBean(RedisUtils.class);
                if (Objects.nonNull(redisUtils.get(keyRepeatDaMie))){
                   log.info("重復請求了,重復請求了,攔截了");
                   returnData(response,repeatDaMie.describe());
                   return false;
               }else {
                    redisUtils.setWithTime(keyRepeatDaMie, true,seconds);
               }
 
            }
            return true;
        } catch (Exception e) {
            log.warn("請求過于頻繁請稍后再試");
            e.printStackTrace();
        }
        return true;
    }
 
    public void returnData(HttpServletResponse response,String msg) throws IOException {
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json; charset=utf-8");
        ObjectMapper objectMapper = new ObjectMapper();
        //這里傳提示語可以改成自己項目的返回數(shù)據(jù)封裝的類
        response.getWriter().println(objectMapper.writeValueAsString(msg));
        return;
    }
 
}

③ WebConfig.java

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
 
/**
 * @Author: JCccc
 * @Date: 2022-6-15 9:24
 * @Description:
 */
@Configuration
public class WebConfig implements WebMvcConfigurer {
 
    
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new ApiRepeatInterceptor()).addPathPatterns("/**");
    }
}

工具類三件套

①ContextUtil.java

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
/**
 * @Author: JCccc
 * @Date: 2022-6-15 9:24
 * @Description:
 */
@Component
public final class ContextUtil implements ApplicationContextAware {
    protected static ApplicationContext applicationContext ;
    @Override
    public void setApplicationContext(ApplicationContext arg0) throws BeansException {
        if (applicationContext == null) {
            applicationContext = arg0;
        }
    }
    public static Object getBean(String name) {
        //name表示其他要注入的注解name名
        return applicationContext.getBean(name);
    }
    /**
     * 拿到ApplicationContext對象實例后就可以手動獲取Bean的注入實例對象
     */
    public static <T> T getBean(Class<T> clazz) {
        return applicationContext.getBean(clazz);
    }
}

②Md5Encrypt.java

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
 
/**
 * @Author: JCccc
 * @CreateTime: 2018-10-30
 * @Description:
 */
public class Md5Encrypt {
 
    private static final char[] DIGITS = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a',
            'b', 'c', 'd', 'e', 'f'};
 
    /**
     * 對字符串進行MD5加密
     *
     * @param text 明文
     * @return 密文
     */
    public static String md5(String text) {
        MessageDigest msgDigest = null;
 
        try {
            msgDigest = MessageDigest.getInstance("MD5");
        } catch (NoSuchAlgorithmException e) {
            throw new IllegalStateException("System doesn't support MD5 algorithm.");
        }
 
        try {
            // 注意該接口是按照指定編碼形式簽名
            msgDigest.update(text.getBytes("UTF-8"));
 
        } catch (UnsupportedEncodingException e) {
 
            throw new IllegalStateException("System doesn't support your  EncodingException.");
 
        }
 
        byte[] bytes = msgDigest.digest();
 
        String md5Str = new String(encodeHex(bytes));
 
        return md5Str;
    }
 
    private static char[] encodeHex(byte[] data) {
 
        int l = data.length;
 
        char[] out = new char[l << 1];
        // two characters form the hex value.
        for (int i = 0, j = 0; i < l; i++) {
            out[j++] = DIGITS[(0xF0 & data[i]) >>> 4];
            out[j++] = DIGITS[0x0F & data[i]];
        }
 
        return out;
    }
}

③RedisUtils.java

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.*;
import org.springframework.stereotype.Component;
import java.io.Serializable;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
 
 
@Component
public class RedisUtils {
 
    @Autowired
    private RedisTemplate redisTemplate;
 
    /**
     * 寫入String型 [ 鍵,值]
     *
     * @param key
     * @param value
     * @return
     */
 
    public boolean set(final String key, Object value) {
        boolean result = false;
        try {
            ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
            operations.set(key, value);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
 
    }
 
 
 
    /**
     * 寫入String型,順便帶有過期時間 [ 鍵,值]
     *
     * @param key
     * @param value
     * @return
     */
 
    public boolean setWithTime(final String key, Object value,int seconds) {
        boolean result = false;
        try {
 
            ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
            operations.set(key, value,seconds, TimeUnit.SECONDS);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
 
    }
 
 
 
    /**
     * 批量刪除對應的value
     *
     * @param keys
     */
 
    public void remove(final String... keys) {
        for (String key : keys) {
            remove(key);
        }
    }
 
    /**
     * 批量刪除key
     *
     * @param pattern
     */
 
    public void removePattern(final String pattern) {
        Set<Serializable> keys = redisTemplate.keys(pattern);
        if (keys.size() > 0)
            redisTemplate.delete(keys);
    }
 
    /**
     * 刪除對應的value
     *
     * @param key
     */
 
    public void remove(final String key) {
        if (exists(key)) {
            redisTemplate.delete(key);
        }
    }
 
 
    /**
     * 判斷緩存中是否有對應的value
     *
     * @param key
     * @return
     */
 
    public boolean exists(final String key) {
        return redisTemplate.hasKey(key);
    }
 
 
    /**
     * 讀取緩存
     *
     * @param key
     * @return
     */
 
    public Object get(final String key) {
        Object result = null;
        ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
        result = operations.get(key);
        return result;
    }
 
 
    /**
     * 哈希 添加
     * hash 一個鍵值(key->value)對集合
     *
     * @param key
     * @param hashKey
     * @param value
     */
 
    public void hmSet(String key, Object hashKey, Object value) {
 
        HashOperations<String, Object, Object> hash = redisTemplate.opsForHash();
 
        hash.put(key, hashKey, value);
 
    }
 
 
    /**
     * Hash獲取數(shù)據(jù)
     *
     * @param key
     * @param hashKey
     * @return
     */
 
    public Object hmGet(String key, Object hashKey) {
        HashOperations<String, Object, Object> hash = redisTemplate.opsForHash();
        return hash.get(key, hashKey);
 
    }
 
 
    /**
     * 列表添加
     * list:lpush key value1
     *
     * @param k
     * @param v
     */
 
    public void lPush(String k, Object v) {
        ListOperations<String, Object> list = redisTemplate.opsForList();
        list.rightPush(k, v);
    }
 
 
    /**
     * 列表List獲取
     * lrange: key 0 10 (讀取的個數(shù) 從0開始 讀取到下標為10 的數(shù)據(jù))
     *
     * @param k
     * @param l
     * @param l1
     * @return
     */
 
    public List<Object> lRange(String k, long l, long l1) {
        ListOperations<String, Object> list = redisTemplate.opsForList();
        return list.range(k, l, l1);
    }
 
 
    /**
     * Set集合添加
     *
     * @param key
     * @param value
     */
 
    public void add(String key, Object value) {
        SetOperations<String, Object> set = redisTemplate.opsForSet();
        set.add(key, value);
    }
 
 
    /**
     * Set 集合獲取
     *
     * @param key
     * @return
     */
 
    public Set<Object> setMembers(String key) {
 
        SetOperations<String, Object> set = redisTemplate.opsForSet();
 
        return set.members(key);
 
    }
 
 
    /**
     * Sorted set :有序集合添加
     *
     * @param key
     * @param value
     * @param scoure
     */
 
    public void zAdd(String key, Object value, double scoure) {
        ZSetOperations<String, Object> zset = redisTemplate.opsForZSet();
        zset.add(key, value, scoure);
    }
 
 
    /**
     * Sorted set:有序集合獲取
     *
     * @param key
     * @param scoure
     * @param scoure1
     * @return
     */
 
    public Set<Object> rangeByScore(String key, double scoure, double scoure1) {
 
        ZSetOperations<String, Object> zset = redisTemplate.opsForZSet();
 
        return zset.rangeByScore(key, scoure, scoure1);
 
    }
 
 
    /**
     * 根據(jù)key獲取Set中的所有值
     *
     * @param key 鍵
     * @return
     */
 
    public Set<Integer> sGet(String key) {
        try {
            return redisTemplate.opsForSet().members(key);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
 
 
    /**
     * 根據(jù)value從一個set中查詢,是否存在
     *
     * @param key   鍵
     * @param value 值
     * @return true 存在 false不存在
     */
 
    public boolean sHasKey(String key, Object value) {
        try {
            return redisTemplate.opsForSet().isMember(key, value);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
 
}

REDIS配置類

RedisConfig.java

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import static org.springframework.data.redis.cache.RedisCacheConfiguration.defaultCacheConfig;
 
/**
 * @Author: JCccc
 * @CreateTime: 2018-09-11
 * @Description:
 */
@Configuration
@EnableCaching
public class RedisConfig {
    @Bean
    public CacheManager cacheManager(RedisConnectionFactory connectionFactory) {
        RedisCacheConfiguration cacheConfiguration =
                defaultCacheConfig()
                        .disableCachingNullValues()
                        .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new Jackson2JsonRedisSerializer(Object.class)));
        return RedisCacheManager.builder(connectionFactory).cacheDefaults(cacheConfiguration).build();
    }
 
 
    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(factory);
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper om = new ObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(om);
        //序列化設置 ,這樣為了存儲操作對象時正常顯示的數(shù)據(jù),也能正常存儲和獲取
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
        return redisTemplate;
    }
    @Bean
    public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory factory) {
        StringRedisTemplate stringRedisTemplate = new StringRedisTemplate();
        stringRedisTemplate.setConnectionFactory(factory);
        return stringRedisTemplate;
    }
 
}

最后寫測試接口,看看效果(一個POST,一個GET):

故意把時間放大,1000秒內(nèi)重復調(diào)用,符合我們攔截規(guī)則的都會被攔截。

TestController.java

import com.example.repeatdemo.dto.PayOrderApply;
import com.example.repeatdemo.annotation.RepeatDaMie;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
 
/**
 * @Author: JCccc
 * @Date: 2022-6-05 9:44
 * @Description:
 */
@RestController
public class TestController {
    private final Logger log = LoggerFactory.getLogger(this.getClass());
 
    @RepeatDaMie(second = 1000,describe = "尊敬的客戶,您慢點")
    @PostMapping(value = "/doPost")
    @ResponseBody
    public void test(@RequestBody PayOrderApply payOrderApply) {
        log.info("Controller POST請求:"+payOrderApply.toString());
    }
 
    @RepeatDaMie(second = 1000,describe = "大哥,你冷靜點")
    @GetMapping(value = "/doGet")
    @ResponseBody
    public void doGet( PayOrderApply payOrderApply) {
        log.info("Controller GET請求:"+payOrderApply.toString());
    }
 
}

PayOrderApply.java

/**
 * @Author: JCccc
 * @Date: 2022-6-12 9:46
 * @Description:
 */
public class PayOrderApply {
 
    private String sn;
    private Long amount;
    private String proCode;
 
    public String getSn() {
        return sn;
    }
 
    public void setSn(String sn) {
        this.sn = sn;
    }
 
    public Long getAmount() {
        return amount;
    }
 
    public void setAmount(Long amount) {
        this.amount = amount;
    }
 
    public String getProCode() {
        return proCode;
    }
 
    public void setProCode(String proCode) {
        this.proCode = proCode;
    }
 
    @Override
    public String toString() {
        return "PayOrderApply{" +
                "sn='" + sn + '\'' +
                ", amount=" + amount +
                ", proCode='" + proCode + '\'' +
                '}';
    }
}

redis生成了值:

到此這篇關(guān)于Springboot利用Redis實現(xiàn)接口冪等性攔截的文章就介紹到這了,更多相關(guān)Springboot接口冪等性攔截內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 基于Java Springboot + Vue + MyBatis實現(xiàn)音樂播放系統(tǒng)

    基于Java Springboot + Vue + MyBatis實現(xiàn)音樂播放系統(tǒng)

    這篇文章主要介紹了一個完整的音樂播放系統(tǒng)是基于Java Springboot + Vue + MyBatis編寫的,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-08-08
  • springboot2.6.4集成swagger3.0遇到的坑及解決方法

    springboot2.6.4集成swagger3.0遇到的坑及解決方法

    這篇文章主要介紹了springboot2.6.4如何集成swagger3.0,在集成的過程中遇到很多問題,本文給大家分享四種問題及相應的解決方案,需要的朋友可以參考下
    2022-03-03
  • Java結(jié)構(gòu)型設計模式之裝飾模式詳解

    Java結(jié)構(gòu)型設計模式之裝飾模式詳解

    裝飾模式(Decorator Pattern)允許向一個現(xiàn)有的對象添加新的功能,同時又不改變其結(jié)構(gòu)。這種類型的設計模式屬于結(jié)構(gòu)型模式,它是作為現(xiàn)有類的一個包裝。這種模式創(chuàng)建了一個裝飾類,用來包裝原有的類,并在保持類方法簽名完整性的前提下,提供了額外的功能
    2023-03-03
  • Java數(shù)據(jù)庫存儲數(shù)組的方法小結(jié)

    Java數(shù)據(jù)庫存儲數(shù)組的方法小結(jié)

    在現(xiàn)代軟件開發(fā)中,數(shù)組是常用的數(shù)據(jù)結(jié)構(gòu)之一,然而,在關(guān)系數(shù)據(jù)庫中直接存儲數(shù)組并不是一個簡單的任務,本文將詳細介紹幾種在Java中將數(shù)組存儲到數(shù)據(jù)庫的方法,包括使用JPA、JSON、XML、以及關(guān)系型數(shù)據(jù)庫的數(shù)組類型等,需要的朋友可以參考下
    2024-09-09
  • spring mvc 讀取xml文件數(shù)據(jù)庫配置參數(shù)的方法

    spring mvc 讀取xml文件數(shù)據(jù)庫配置參數(shù)的方法

    下面小編就為大家?guī)硪黄猻pring mvc 讀取xml文件數(shù)據(jù)庫配置參數(shù)的方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-10-10
  • 解決Spring中@Value注解取值為null問題

    解決Spring中@Value注解取值為null問題

    近期應用中因業(yè)務迭代需要接入 user 客戶端,接入后總是啟動失敗,報注冊 user bean 依賴的配置屬性為 null,所以接下來小編就和大家一起排查分析這個問題,感興趣的小伙伴跟著小編一起來看看吧
    2023-08-08
  • idea中定時及多數(shù)據(jù)源配置方法

    idea中定時及多數(shù)據(jù)源配置方法

    因項目要求,需要定時從達夢數(shù)據(jù)庫中取數(shù)據(jù),并插入或更新到ORACLE數(shù)據(jù)庫中,這篇文章主要介紹了idea中定時及多數(shù)據(jù)源配置方法,需要的朋友可以參考下
    2023-12-12
  • CCF考試試題之門禁系統(tǒng)java解題代碼

    CCF考試試題之門禁系統(tǒng)java解題代碼

    這篇文章主要為大家詳細介紹了CCF考試試題之門禁系統(tǒng)java解題代碼,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-01-01
  • Springboot集成graylog及配置過程解析

    Springboot集成graylog及配置過程解析

    這篇文章主要介紹了Springboot集成graylog及配置過程解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-12-12
  • 如何在 Java 中利用 redis 實現(xiàn) LBS 服務

    如何在 Java 中利用 redis 實現(xiàn) LBS 服務

    基于位置的服務,是指通過電信移動運營商的無線電通訊網(wǎng)絡或外部定位方式,獲取移動終端用戶的位置信息,在GIS平臺的支持下,為用戶提供相應服務的一種增值業(yè)務。下面我們來一起學習一下吧
    2019-06-06

最新評論