对数据库连接信息加解密,配置信息为密文设置在nacos配置中心,spring.prop.key为密钥,加解密使用
配置密文信息可有由密钥key依据加解密方法获得
/**
* redis配置
*/
@Configuration
@EnableCaching
@AutoConfigureBefore(RedisAutoConfiguration.class)
public class RedisConfig extends CachingConfigurerSupport {
@Value(value = "${spring.data.redis.host}")
private String host;
@Value(value = "${spring.data.redis.password}")
private String password;
@Value(value = "${spring.data.redis.port}")
private int port;
@Value(value = "${spring.data.redis.database}")
private int database;
@Value(value = "${spring.prop.key}")
private String key;
@Bean
public LettuceConnectionFactory lettuceConnectionFactory() {
RedisStandaloneConfiguration configuration = new RedisStandaloneConfiguration();
configuration.setHostName(SM4Utils.decryptCbc(StringUtils.stringToHexString(key), host));
configuration.setPort(26379);
configuration.setPassword(SM4Utils.decryptCbc(StringUtils.stringToHexString(key), password));
return new LettuceConnectionFactory(configuration);
}
@Bean
@SuppressWarnings(value = {"unchecked", "rawtypes"})
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setConnectionFactory(factory);
Jackson2JsonRedisSerializer<Object> valueSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
// 使用StringRedisSerializer来序列化和反序列化redis的key值
template.setKeySerializer(new StringRedisSerializer());
template.setValueSerializer(valueSerializer);
// Hash的key也采用StringRedisSerializer的序列化方式
template.setHashKeySerializer(new StringRedisSerializer());
template.setHashValueSerializer(valueSerializer);
template.afterPropertiesSet();
return template;
}
@Bean
public RedissonClient redissonClient() {
Config config = new Config();
config.setCodec(new JsonJacksonCodec());
SingleServerConfig singleServerConfig = config.useSingleServer();
String address = "redis://" + SM4Utils.decryptCbc(StringUtils.stringToHexString(key), host) + ":" + port;
singleServerConfig.setAddress(address);
singleServerConfig.setDatabase(database);
singleServerConfig.setPassword(SM4Utils.decryptCbc(StringUtils.stringToHexString(key), password));
singleServerConfig.setRetryInterval(10000);
singleServerConfig.setRetryAttempts(3);
return Redisson.create(config);
}
}