当前位置: 首页 > news >正文

Redis密码在springboot中自定义加解密实践

Redis密码在springboot自定义加解密

1.application.yml文件配置信息

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

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中代码

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

packagecom.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

*/

@Configuration

@EnableCaching

publicclassRedisConfigextendsCachingConfigurerSupport

{

privatefinalEnvironment environment;

publicRedisConfig(Environment environment){

this.environment=environment;

}

@Bean

publicRedisConnectionFactory 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 pwd=DESUtil.encrypt("111111");//此处用于生成加密后的密码,配置在配置文件中

redisStandaloneConfiguration.setPassword(DESUtil.decrypt(password));

returnnewLettuceConnectionFactory(redisStandaloneConfiguration);

}

@Bean

@SuppressWarnings(value = {"unchecked","rawtypes"})

publicRedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory connectionFactory)

{

//connectionFactory获取到的密码就是解密后的密码

RedisTemplate<Object, 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;

}

@Bean

publicDefaultRedisScript<Long> limitScript()

{

DefaultRedisScript<Long> redisScript =newDefaultRedisScript<>();

redisScript.setScriptText(limitScriptText());

redisScript.setResultType(Long.class);

returnredisScript;

}

/**

* 限流脚本

*/

privateString 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);";

}

}

总结

以上为个人经验,希望能给大家一个参考

http://www.jsqmd.com/news/657305/

相关文章:

  • 千问3.5-9B辅助网络协议分析与安全漏洞描述生成
  • 热轧钢带缺陷数据集,称为Xsteel表面缺陷数据集(X-SDD),其中包含七种典型的热轧带钢缺陷类型,共有1360个缺陷图像。与常用的NEU表面缺陷数据库(NEU-CLS)的六种缺陷类型相比,X-SD
  • SITS2026闭门研讨会纪要(内部流出):3家头部金融科技公司如何用定制化AI代码搜索工具规避CVE-2026-XXXX类漏洞——附可部署的RAG策略模板
  • AI Coding Agents 的“生产级技能包”
  • 研究:约35%新网站由AI生成或辅助,让互联网“虚假快乐”且降低意识形态多样性
  • React Hooks原理剖析
  • 3步打造专属游戏库:Playnite界面布局与色彩定制指南
  • 全新智能识别技术加持,高效提取视频关键信息更清晰省事还好整理
  • 告别安卓模拟器:在Windows上直接安装APK的三大场景化解决方案
  • 2026奇点大会AI测试生成技术白皮书核心泄露(仅限首批读者速领)
  • 5分钟学会PlantUML编辑器:免费在线UML绘图终极指南
  • C++11 新特性 万能函数容器之std::function
  • 解锁Ryzen处理器的隐藏性能:SMUDebugTool完全指南
  • 3个技巧让百度网盘下载速度翻倍:直链解析工具实战指南
  • BI国产替代进入深水区:为什么企业真正重估的,不只是 Power BI 和 Tableau 的替代成本,而是数据分析能力的组织渗透率
  • 云端 LLM 思考,端侧 VLM 执行:探讨 侠客工坊Android 自动化测试与业务流转的终局
  • 【GitHub项目推荐--Octogent:给 Claude Code 装上“章鱼触手”的多智能体编排层】⭐
  • Redis实现分布式限流的几种方法
  • LLM-Graph-Builder:基于大语言模型的智能知识图谱构建解决方案
  • 博弈论算法精讲:从公平组合游戏到SG函数实战(ACM/OI选手必备)
  • 交直流混合微电网架构:拓扑优化与功率交互设计
  • 2026年3月SMT精密激光钢网供应商推荐分析,精密激光切割加工/SMT纳米阶梯钢网,SMT精密激光钢网源头厂家推荐分析 - 品牌推荐师
  • SITS2026智能生成能力雷达图(11维评估):从TypeScript泛型推导到Spring Boot事务链路补全,谁真正读懂了你的代码语义?
  • Adobe-GenP 3.0:解密Adobe全家桶通用补丁的技术实现与应用指南
  • 康耐视VisionPro:从“固定”到“灵活”,工业标定的实战进阶指南
  • 谷歌调整“水手计划”团队,浏览器智能体遇冷,新模型效率提升 50 倍!
  • 蓝桥杯单片机备赛避坑指南:从第九届省赛代码里学到的3个调试技巧与1个常见误区
  • MinerU 系列教程 第十一课:表格识别 - 有线与无线的双引擎
  • 如何为Windows安卓子系统打造完整的Android体验:MagiskOnWSALocal终极指南
  • EC开发tips