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

Redis密碼在springboot中自定義加解密實踐

 更新時間:2026年01月12日 09:30:56   作者:MartinYangHJ  
文章介紹了在Spring?Boot項目中自定義配置Redis密碼的加解密方法,通過在`application.yml`文件中配置信息,并在`RedisConfig`類中實現(xiàn)加解密邏輯

Redis密碼在springboot自定義加解密

1.application.yml文件配置信息

spring:
  # redis 配置
  redis:
    # 地址
    host: 192.168.1.xxx
    # 端口,默認為6379
    port: 6379
    # 數(shù)據(jù)庫索引
    database: 0
    # 密碼,DES加密后,有key值,此處只作為例子
    password: 1E903BC217660491
    # 連接超時時間
    timeout: 10s
    lettuce:
      pool:
        # 連接池中的最小空閑連接
        min-idle: 0
        # 連接池中的最大空閑連接
        max-idle: 8
        # 連接池的最大數(shù)據(jù)庫連接數(shù)
        max-active: 8
        # #連接池最大阻塞等待時間(使用負值表示沒有限制)
        max-wait: -1ms

2.RedisConfig中代碼

package com.framework.config;

import com.common.utils.sign.DESUtil;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.script.DefaultRedisScript;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.jsontype.impl.LaissezFaireSubTypeValidator;

/**
 * redis配置
 * 
 * @author elane
 */
@Configuration
@EnableCaching
public class RedisConfig extends CachingConfigurerSupport
{
    private final Environment environment;
    public RedisConfig(Environment environment){
        this.environment=environment;
    }

    @Bean
    public RedisConnectionFactory myLettuceConnectionFactory(){
        RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration(environment.getProperty("spring.redis.host"),Integer.parseInt(environment.getProperty("spring.redis.port")));
        redisStandaloneConfiguration.setDatabase(Integer.parseInt(environment.getProperty("spring.redis.database")));
        //獲取application.yml 中的密碼(密文)
        String password = environment.getProperty("spring.redis.password");
        //解密密碼并停駕到配置中
        String pwd=DESUtil.encrypt("111111");//此處用于生成加密后的密碼,配置在配置文件中
        redisStandaloneConfiguration.setPassword(DESUtil.decrypt(password));
        return new LettuceConnectionFactory(redisStandaloneConfiguration);
    }

    @Bean
    @SuppressWarnings(value = { "unchecked", "rawtypes" })
    public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory connectionFactory)
    {
        //connectionFactory獲取到的密碼就是解密后的密碼
        RedisTemplate<Object, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(connectionFactory);

        FastJson2JsonRedisSerializer serializer = new FastJson2JsonRedisSerializer(Object.class);

        ObjectMapper mapper = new ObjectMapper();
        mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        mapper.activateDefaultTyping(LaissezFaireSubTypeValidator.instance, ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY);
        serializer.setObjectMapper(mapper);

        // 使用StringRedisSerializer來序列化和反序列化redis的key值
        template.setKeySerializer(new StringRedisSerializer());
        template.setValueSerializer(serializer);

        // Hash的key也采用StringRedisSerializer的序列化方式
        template.setHashKeySerializer(new StringRedisSerializer());
        template.setHashValueSerializer(serializer);

        template.afterPropertiesSet();
        return template;
    }

    @Bean
    public DefaultRedisScript<Long> limitScript()
    {
        DefaultRedisScript<Long> redisScript = new DefaultRedisScript<>();
        redisScript.setScriptText(limitScriptText());
        redisScript.setResultType(Long.class);
        return redisScript;
    }

    /**
     * 限流腳本
     */
    private String limitScriptText()
    {
        return "local key = KEYS[1]\n" +
                "local count = tonumber(ARGV[1])\n" +
                "local time = tonumber(ARGV[2])\n" +
                "local current = redis.call('get', key);\n" +
                "if current and tonumber(current) > count then\n" +
                "    return tonumber(current);\n" +
                "end\n" +
                "current = redis.call('incr', key)\n" +
                "if tonumber(current) == 1 then\n" +
                "    redis.call('expire', key, time)\n" +
                "end\n" +
                "return tonumber(current);";
    }
}

總結(jié)

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Redis通過scan查找不過期的 key(方法詳解)

    Redis通過scan查找不過期的 key(方法詳解)

    SCAN 命令是一個基于游標的迭代器,每次被調(diào)用之后, 都會向用戶返回一個新的游標, 用戶在下次迭代時需要使用這個新游標作為 SCAN 命令的游標參數(shù), 以此來延續(xù)之前的迭代過程,對Redis scan 查找 key相關(guān)知識感興趣的朋友一起看看吧
    2021-08-08
  • redis?哨兵集群搭建的實現(xiàn)

    redis?哨兵集群搭建的實現(xiàn)

    本文主要介紹了redis?哨兵集群搭建的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-08-08
  • 簡介Redis中的showlog功能

    簡介Redis中的showlog功能

    這篇文章主要介紹了簡介Redis中的showlog功能,作者同時對比了DEL命令的性能,需要的朋友可以參考下
    2015-06-06
  • Redis刪除緩存失敗的原因和解決方案

    Redis刪除緩存失敗的原因和解決方案

    本文討論了數(shù)據(jù)庫操作成功但緩存刪除失敗時,線上環(huán)境下的應(yīng)對策略,提出了一種更穩(wěn)定的方法,包括主流程寫庫后再刪除緩存、刪除失敗進入重試隊列、超過重試上限進入死信隊列并觸發(fā)告警和補償?shù)却胧?需要的朋友可以參考下
    2026-04-04
  • Redis全量同步和增量同步原理

    Redis全量同步和增量同步原理

    主從第一次同步是全量同步:也就是說,當你主從節(jié)點連接建立后,需要執(zhí)行一次全量同步,但如果slave重啟后同步,此時slave重啟后,slave節(jié)點和master節(jié)點的數(shù)據(jù)之間有落后,因此需要進行增量同步,感興趣的同學(xué)可以參考閱讀
    2023-04-04
  • Redis中的過期策略和淘汰策略使用詳解

    Redis中的過期策略和淘汰策略使用詳解

    這篇文章主要介紹了Redis中的過期策略和淘汰策略使用方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2025-06-06
  • Redis集群水平擴展、集群中添加以及刪除節(jié)點的操作

    Redis集群水平擴展、集群中添加以及刪除節(jié)點的操作

    這篇文章主要介紹了Redis集群水平擴展、集群中添加以及刪除節(jié)點的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-03-03
  • Redis?搭建主從集群的操作指南

    Redis?搭建主從集群的操作指南

    單節(jié)點的?Redis?并發(fā)能力有限,要進一步提高?Redis?的并發(fā)能力,就需要搭建主從集群,實現(xiàn)讀寫分離,這篇文章主要給大家介紹了Redis搭建主從集群的操作指南,需要的朋友可以參考下
    2023-08-08
  • 使用Redis實現(xiàn)JWT令牌主動失效機制

    使用Redis實現(xiàn)JWT令牌主動失效機制

    JWT是一種輕量級的身份驗證和授權(quán)機制,它是一種 JSON 格式的數(shù)據(jù)串,通常用于客戶端和服務(wù)端之間的單點登錄(Single Sign-On, SSO)場景,本文給大家介紹了如何使用Redis來實現(xiàn)JWT令牌主動失效機制,需要的朋友可以參考下
    2024-08-08
  • Redis 在真實世界的 5 個用法

    Redis 在真實世界的 5 個用法

    Redis是一個開源的使用ANSI C語言編寫、支持網(wǎng)絡(luò)、可基于內(nèi)存亦可持久化的日志型、Key-Value數(shù)據(jù)庫,并提供多種語言的API這篇文章主要介紹了Redis 在真實世界的 5 個用法,需要的朋友可以參考下
    2018-03-03

最新評論

阜南县| 绿春县| 洞口县| 望奎县| 平江县| 延川县| 丰顺县| 望奎县| 南岸区| 海南省| 铁岭市| 镇康县| 礼泉县| 鄂州市| 房山区| 阿图什市| 泰州市| 合肥市| 延津县| 芮城县| 永川市| 达孜县| 措勤县| 法库县| 武平县| 平果县| 林芝县| 民权县| 灯塔市| 蓬安县| 德兴市| 九台市| 乐亭县| 武隆县| 古丈县| 梅河口市| 永川市| 瑞安市| 南涧| 隆德县| 巴林左旗|