Redis密码在springboot自定义加解密
1.application.yml文件配置信息
spring:
# redis 配置
redis:
# 地址
host: 192.168.1.xxx
# 端口,默认为6379
port: 6379
# 数据库索引
database: 0
# 密码,DES加密后,有key值,此处只作为例子
password: 1E903BC217660491
# 连接超时时间
timeout: 10s
lettuce:
pool:
# 连接池中的最小空闲连接
min-idle: 0
# 连接池中的最大空闲连接
max-idle: 8
# 连接池的最大数据库连接数
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
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持IT俱乐部。
