Redis密码在springboot中自定义加解密实践
Redis密码在springboot自定义加解密1.application.yml文件配置信息1234567891011121314151617181920212223spring:# redis 配置redis:# 地址host:192.168.1.xxx# 端口默认为6379port:6379# 数据库索引database:0# 密码DES加密后有key值此处只作为例子password:1E903BC217660491# 连接超时时间timeout:10slettuce:pool:# 连接池中的最小空闲连接min-idle:0# 连接池中的最大空闲连接max-idle:8# 连接池的最大数据库连接数max-active:8# #连接池最大阻塞等待时间使用负值表示没有限制max-wait:-1ms2.RedisConfig中代码123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101packagecom.framework.config;importcom.common.utils.sign.DESUtil;importorg.springframework.cache.annotation.CachingConfigurerSupport;importorg.springframework.cache.annotation.EnableCaching;importorg.springframework.context.annotation.Bean;importorg.springframework.context.annotation.Configuration;importorg.springframework.core.env.Environment;importorg.springframework.data.redis.connection.RedisConnectionFactory;importorg.springframework.data.redis.connection.RedisStandaloneConfiguration;importorg.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;importorg.springframework.data.redis.core.RedisTemplate;importorg.springframework.data.redis.core.script.DefaultRedisScript;importorg.springframework.data.redis.serializer.StringRedisSerializer;importcom.fasterxml.jackson.annotation.JsonAutoDetect;importcom.fasterxml.jackson.annotation.JsonTypeInfo;importcom.fasterxml.jackson.annotation.PropertyAccessor;importcom.fasterxml.jackson.databind.ObjectMapper;importcom.fasterxml.jackson.databind.jsontype.impl.LaissezFaireSubTypeValidator;/*** redis配置** author elane*/ConfigurationEnableCachingpublicclassRedisConfigextendsCachingConfigurerSupport{privatefinalEnvironment environment;publicRedisConfig(Environment environment){this.environmentenvironment;}BeanpublicRedisConnectionFactory myLettuceConnectionFactory(){RedisStandaloneConfiguration redisStandaloneConfiguration newRedisStandaloneConfiguration(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 pwdDESUtil.encrypt(111111);//此处用于生成加密后的密码配置在配置文件中redisStandaloneConfiguration.setPassword(DESUtil.decrypt(password));returnnewLettuceConnectionFactory(redisStandaloneConfiguration);}BeanSuppressWarnings(value {unchecked,rawtypes})publicRedisTemplateObject, Object redisTemplate(RedisConnectionFactory connectionFactory){//connectionFactory获取到的密码就是解密后的密码RedisTemplateObject, Object template newRedisTemplate();template.setConnectionFactory(connectionFactory);FastJson2JsonRedisSerializer serializer newFastJson2JsonRedisSerializer(Object.class);ObjectMapper mapper newObjectMapper();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(newStringRedisSerializer());template.setValueSerializer(serializer);// Hash的key也采用StringRedisSerializer的序列化方式template.setHashKeySerializer(newStringRedisSerializer());template.setHashValueSerializer(serializer);template.afterPropertiesSet();returntemplate;}BeanpublicDefaultRedisScriptLong limitScript(){DefaultRedisScriptLong redisScript newDefaultRedisScript();redisScript.setScriptText(limitScriptText());redisScript.setResultType(Long.class);returnredisScript;}/*** 限流脚本*/privateString limitScriptText(){returnlocal key KEYS[1]\nlocal count tonumber(ARGV[1])\nlocal time tonumber(ARGV[2])\nlocal current redis.call(get, key);\nif current and tonumber(current) count then\n return tonumber(current);\nend\ncurrent redis.call(incr, key)\nif tonumber(current) 1 then\n redis.call(expire, key, time)\nend\nreturn tonumber(current);;}}总结以上为个人经验希望能给大家一个参考