【Redis】配置序列化器

1. 配置FastJSON2

FastJSON2相比与FastJSON更安全,更推荐使用。

java 复制代码
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONReader;
import com.alibaba.fastjson2.JSONWriter;
import com.alibaba.fastjson2.filter.Filter;

import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.SerializationException;

import java.nio.charset.Charset;

/**
 * Redis使用FastJson序列化
 * 
 * @author ruoyi
 */
public class FastJson2JsonRedisSerializer<T> implements RedisSerializer<T>
{
    /**
     * 自动识别json对象白名单配置(仅允许解析的包名,范围越小越安全)
     */
    public static final String[] JSON_WHITELIST_STR = { "org.springframework", "im.zhaojun" };

    public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");

    static final Filter AUTO_TYPE_FILTER = JSONReader.autoTypeFilter(JSON_WHITELIST_STR);

    private Class<T> clazz;

    public FastJson2JsonRedisSerializer(Class<T> clazz)
    {
        super();
        this.clazz = clazz;
    }

    @Override
    public byte[] serialize(T t) throws SerializationException
    {
        if (t == null)
        {
            return new byte[0];
        }
        return JSON.toJSONString(t, JSONWriter.Feature.WriteClassName).getBytes(DEFAULT_CHARSET);
    }

    @Override
    public T deserialize(byte[] bytes) throws SerializationException
    {
        if (bytes == null || bytes.length <= 0)
        {
            return null;
        }
        String str = new String(bytes, DEFAULT_CHARSET);
        return (T)JSON.parseObject(str, clazz, AUTO_TYPE_FILTER);
    }
}

2. 配置Redis序列化器

java 复制代码
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.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
@Configuration
public class RedisConfig {
    @Bean
    public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
//        //1.创建
//        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
//        redisTemplate.setConnectionFactory(connectionFactory);
//        //json序列化工具
//        GenericJackson2JsonRedisSerializer serializer = new GenericJackson2JsonRedisSerializer();
//        //设置key的序列化
//        //StringRedisSerializer UTF_8 = new StringRedisSerializer(StandardCharsets.UTF_8);
//        redisTemplate.setKeySerializer(RedisSerializer.string());
//        redisTemplate.setHashKeySerializer(RedisSerializer.string());
//        //设置值的value
//        redisTemplate.setValueSerializer(serializer);
//        redisTemplate.setHashValueSerializer(serializer);
//        return redisTemplate;
        RedisTemplate<Object, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(connectionFactory);



        FastJson2JsonRedisSerializer serializer = new FastJson2JsonRedisSerializer(Object.class);


        // 使用StringRedisSerializer来序列化和反序列化redis的key值
        template.setKeySerializer(new StringRedisSerializer());
        template.setValueSerializer(serializer);

        // Hash的key也采用StringRedisSerializer的序列化方式
        template.setHashKeySerializer(new StringRedisSerializer());
        template.setHashValueSerializer(serializer);

        template.afterPropertiesSet();
        return template;
    }
}
相关推荐
Xy9108 分钟前
App Trace技术解析:传参安装、一键拉起与快速安装
数据库·程序员
朝九晚五ฺ13 分钟前
【MySQL基础】MySQL表操作全面指南:从创建到管理的深度解析
数据库·sql
whltaoin29 分钟前
Redis专题-实战篇一-基于Session和Redis实现登录业务
redis·缓存·springboot
AIGC_北苏38 分钟前
Ubuntu 安装 Mysql 数据库
数据库·mysql·ubuntu
15942315631 小时前
QT使用WxSQLite3打开加密数据库并查询
数据库·qt·sqlite
华清远见成都中心2 小时前
大语言模型(LLM)中的KV缓存压缩与动态稀疏注意力机制设计
人工智能·缓存·语言模型
程序员阿超的博客2 小时前
【数据篇】持久化核心:整合 JPA/MyBatis 实现优雅的数据库操作
数据库·mybatis
张哈大3 小时前
【 java 虚拟机知识 第一篇 】
java·开发语言·jvm·笔记·缓存
struggle20256 小时前
RushDB开源程序 是现代应用程序和 AI 的即时数据库。建立在 Neo4j 之上
数据库·typescript·neo4j