Files
springboot-template/src/main/java/com/aisi/template/config/RedisConfig.java
shenjianZ 40c85c3c1f feat: 实现完整的 RBAC 权限管理系统与基础设施增强
在初始认证基础上,新增完整的 RBAC 权限模型(角色、权限、菜单三级管理),
  集成审计日志、接口限流、登录失败锁定、Refresh Token 机制、Redis 分布式缓存与锁、
  RocketMQ 消息队列,并引入 Flyway 数据库版本管理,同时补充项目文档与使用示例
2026-04-10 10:58:22 +08:00

97 lines
3.5 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package com.aisi.template.config;
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;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
/**
* Redis 配置类
* 配置 Redis 序列化方式
*
* 主要功能:
* 1. RedisTemplate用于操作对象类型数据
* 2. StringRedisTemplate用于操作字符串类型数据
*
* 序列化配置:
* - Key使用 String 序列化
* - Value使用 JSON 序列化(支持类型信息)
* - HashKey使用 String 序列化
* - HashValue使用 JSON 序列化
*
* @author Claude
* @since 2024-04-09
*/
@Configuration
public class RedisConfig {
/**
* 配置 RedisTemplate用于操作对象
* 步骤:
* 1. 创建 RedisTemplate 对象
* 2. 配置 ObjectMapper支持类型信息
* 3. 配置 Key 序列化方式
* 4. 配置 Value 序列化方式
*
* @param factory Redis 连接工厂
* @param objectMapper Jackson ObjectMapper
* @return RedisTemplate 对象
*/
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory, ObjectMapper objectMapper) {
// 1. 创建 RedisTemplate 对象
RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setConnectionFactory(factory);
// 2. 配置 ObjectMapper支持类型信息用于反序列化时确定类型
ObjectMapper redisMapper = objectMapper.copy();
redisMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
redisMapper.activateDefaultTyping(
LaissezFaireSubTypeValidator.instance,
ObjectMapper.DefaultTyping.NON_FINAL,
JsonTypeInfo.As.PROPERTY
);
// 3. 配置序列化器
StringRedisSerializer stringSerializer = new StringRedisSerializer();
GenericJackson2JsonRedisSerializer jsonSerializer = new GenericJackson2JsonRedisSerializer(redisMapper);
// 3.1 Key 使用 String 序列化
template.setKeySerializer(stringSerializer);
// 3.2 HashKey 使用 String 序列化
template.setHashKeySerializer(stringSerializer);
// 3.3 Value 使用 JSON 序列化
template.setValueSerializer(jsonSerializer);
// 3.4 HashValue 使用 JSON 序列化
template.setHashValueSerializer(jsonSerializer);
// 4. 执行初始化
template.afterPropertiesSet();
return template;
}
/**
* 配置 StringRedisTemplate用于操作字符串
* 说明:
* - StringRedisTemplate 专门用于处理字符串类型数据
* - Key 和 Value 都使用 String 序列化
* - 适用于简单的键值对操作
*
* @param factory Redis 连接工厂
* @return StringRedisTemplate 对象
*/
@Bean
public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory factory) {
return new StringRedisTemplate(factory);
}
}