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: -1ms2.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);";
}
}总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。
发表评论