spring-session-data-redis出现\xac\xed\x00\x05sr\x00\x0e乱码问题

本文档介绍了在使用Spring Session Data Redis时遇到的乱码问题及其原因。问题源于默认的JdkSerializationRedisSerializer导致的存储格式不符合预期。为了解决这个问题,文章提供了两种解决方案:一是采用FastJsonRedisSerializer并配置FastJsonConfig以实现UTF-8编码;二是使用GenericFastJsonRedisSerializer,特别是在结合Spring Boot Security时,能有效避免SpelEvaluationException错误。通过修改序列化器,不仅解决了乱码问题,还优化了内存占用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

引入依赖

<dependency>
   <groupId>org.springframework.session</groupId>
    <artifactId>spring-session-data-redis</artifactId>
</dependency>

问题描述

如下面 箭头所示,对于spring-session-data-redis存储了session的数据时,会出现乱码。
这些乱码不应该出现,看起来不优雅,并且这些乱码占用空间,存储较多数据的时候,会占用大量内存。
在这里插入图片描述

问题出现原因

spring-session-data-redis默认使用原生的JdkSerializationRedisSerializer。
翻看源码RedisIndexedSessionRepository查看,这就是出现乱码的原因,存储格式不符合。
在这里插入图片描述

问题解决方案

查看官方文档:官方文档
在这里插入图片描述

问题解决代码

使用FastJsonRedisSerializer

@Bean
public RedisSerializer<Object> springSessionDefaultRedisSerializer() {

    // 使用 FastJsonRedisSerializer 来序列化和反序列化redis 的 value的值
    FastJsonRedisSerializer<Object> serializer = new FastJsonRedisSerializer<>(Object.class);
    ParserConfig.getGlobalInstance().addAccept("com.muzz");
    FastJsonConfig fastJsonConfig = new FastJsonConfig();
    fastJsonConfig.setCharset(StandardCharsets.UTF_8);
    serializer.setFastJsonConfig(fastJsonConfig);
    return serializer;
}

除了使用上面的序列化器之前,官方还推荐GenericFastJsonRedisSerializer序列化

@Bean
    public RedisSerializer<Object> springSessionDefaultRedisSerializer() {

        // 使用 FastJsonRedisSerializer 来序列化和反序列化 redis 的 value的值
//        FastJsonRedisSerializer<Object> serializer = new FastJsonRedisSerializer<>(Object.class);
//        ParserConfig.getGlobalInstance().addAccept("com.muzz.");
//        ParserConfig.getGlobalInstance().addAccept("org.springframework.");
//        ParserConfig.getGlobalInstance().addAccept("org.springframework.security.core.context.");
//        FastJsonConfig fastJsonConfig = new FastJsonConfig();
//        fastJsonConfig.setCharset(StandardCharsets.UTF_8);
//        serializer.setFastJsonConfig(fastJsonConfig);
//        return serializer;
        return new GenericFastJsonRedisSerializer();
    }

之所以使用这个之外,是因为我在使用springboot security的时候,发现了一个错误。

org.springframework.expression.spel.SpelEvaluationException: EL1008E: Property or field 'authentication' cannot be found on object of type 'com.alibaba.fastjson.JSONObject' - maybe not public or not valid?
	at org.springframework.expression.spel.ast.PropertyOrFieldReference.readProperty(PropertyOrFieldReference.java:217) ~[spring-expression-5.3.1.jar:5.3.1]
	at org.springframework.expression.spel.ast.PropertyOrFieldReference.getValueInternal(PropertyOrFieldReference.java:104) ~[spring-expression-5.3.1.jar:5.3.1]
	at org.springframework.expression.spel.ast.PropertyOrFieldReference.getValueInternal(PropertyOrFieldReference.java:91) ~[spring-expression-5.3.1.jar:5.3.1]
	at org.springframework.expression.spel.ast.CompoundExpression.getValueRef(CompoundExpression.java:55) ~[spring-expression-5.3.1.jar:5.3.1]
	....

在使用了 GenericFastJsonRedisSerializer这个序列化器之后,就可以解决上述问题。

效果显示

对比上面的问题显示,可以发现乱码解决了
在这里插入图片描述

总结

多看文档,文档藏金子。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值