解决com.alibaba.fastjson.JSONException:autoType is not support问题,Redis FastJson

朴灿烈づ我的快乐病毒、 2024-03-25 19:23 147阅读 0赞

前言

最近在配置redis序列化问题的时候,使用fastjson来进行序列化,报异常:

com.alibaba.fastjson.JSONException: autoType is not support. org.springframework.security.core.authority.SimpleGrantedAuthority

com.alibaba.fastjson2.JSONException: autoType is not support. org.springframework.security.core.authority.SimpleGrantedAuthority

等等问题

本次出现问题是在fastjson反序列化springsecurity的UserDetails时出现的问题,报错代码如下

0e305d4337f09d85ce090588c0a101d9.png

环境

java 17

springboot 3.0.4

fastjson 2.0.22

  1. <dependency>
  2. <groupId>com.alibaba</groupId>
  3. <artifactId>fastjson</artifactId>
  4. <version>2.0.22</version>
  5. </dependency>

寻找问题根源

b8c8b4c8f3ea00a2d2f2d8cff9eddfd4.png

提示问题出现在ObjectReaderProvider.java:734行,这里autoTypeSupport默认是true

eaa7226648f954eae30257c1c835b199.png

大致意思是,类型没有在白名单范围之内,找了很多个博主的帖子,都没有可行的解决方案,可能是我用的版本比较高fastjson 2.0.22吧,其他博主的帖子都是fastjson1.x的。

这下面是我的Redis配置

  1. import com.alibaba.fastjson.support.spring.GenericFastJsonRedisSerializer;
  2. import org.springframework.cache.annotation.CachingConfigurerSupport;
  3. import org.springframework.cache.annotation.EnableCaching;
  4. import org.springframework.context.annotation.Bean;
  5. import org.springframework.context.annotation.Configuration;
  6. import org.springframework.data.redis.connection.RedisConnectionFactory;
  7. import org.springframework.data.redis.core.RedisTemplate;
  8. import org.springframework.data.redis.serializer.StringRedisSerializer;
  9. /**
  10. * @description 缓存配置类
  11. * @author ouyang
  12. * @version 1.0
  13. * @date 2023-03-14 8:25
  14. */
  15. @Configuration
  16. @EnableCaching
  17. public class RedisConfig extends CachingConfigurerSupport {
  18. @Bean
  19. @SuppressWarnings(value = { "unchecked", "rawtypes" })
  20. public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
  21. RedisTemplate<Object, Object> template = new RedisTemplate<>();
  22. template.setConnectionFactory(connectionFactory);
  23. GenericFastJsonRedisSerializer serializer = new GenericFastJsonRedisSerializer();
  24. // 使用GenericFastJsonRedisSerializer来序列化和反序列化redis的key值
  25. template.setKeySerializer(new StringRedisSerializer());
  26. template.setValueSerializer(serializer);
  27. // Hash的key也采用StringRedisSerializer的序列化方式
  28. template.setHashKeySerializer(new StringRedisSerializer());
  29. template.setHashValueSerializer(serializer);
  30. template.afterPropertiesSet();
  31. return template;
  32. }
  33. }

现在只需要给fastjson添加白名单就行了

尝试1(高版本不支持)

通过修改全局对象的方式进行白名单设置

c41e1b6bb83009c979e8337853d30afd.png

尝试2(高版本不支持)

配置jvm启动参数,上面都不支持,这个应该也不支持

尝试3(成功)

查看GenericFastJsonRedisSerializer源码,发现里面有两个构造函数,一个无参(我现在用的),另一个是contextFilter我也不知道是干嘛的,但是发现参数名竟然是acceptNames,理解意思大概是支持的名称?

4a56c43e692d8c8c9feef49e40d652e6.png

于是我就尝试了有参构造序列化

  1. import com.alibaba.fastjson.support.spring.GenericFastJsonRedisSerializer;
  2. import org.springframework.cache.annotation.CachingConfigurerSupport;
  3. import org.springframework.cache.annotation.EnableCaching;
  4. import org.springframework.context.annotation.Bean;
  5. import org.springframework.context.annotation.Configuration;
  6. import org.springframework.data.redis.connection.RedisConnectionFactory;
  7. import org.springframework.data.redis.core.RedisTemplate;
  8. import org.springframework.data.redis.serializer.StringRedisSerializer;
  9. /**
  10. * @description 缓存配置类
  11. * @author ouyang
  12. * @version 1.0
  13. * @date 2023-03-14 8:25
  14. */
  15. @Configuration
  16. @EnableCaching
  17. public class RedisConfig extends CachingConfigurerSupport {
  18. @Bean
  19. @SuppressWarnings(value = { "unchecked", "rawtypes" })
  20. public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
  21. RedisTemplate<Object, Object> template = new RedisTemplate<>();
  22. template.setConnectionFactory(connectionFactory);
  23. // 解决autoType is not support.xxxx.xx的问题
  24. String[] acceptNames = {"org.springframework.security.core.authority.SimpleGrantedAuthority"};
  25. GenericFastJsonRedisSerializer serializer = new GenericFastJsonRedisSerializer(acceptNames);
  26. // 使用GenericFastJsonRedisSerializer来序列化和反序列化redis的key值
  27. template.setKeySerializer(new StringRedisSerializer());
  28. template.setValueSerializer(serializer);
  29. // Hash的key也采用StringRedisSerializer的序列化方式
  30. template.setHashKeySerializer(new StringRedisSerializer());
  31. template.setHashValueSerializer(serializer);
  32. template.afterPropertiesSet();
  33. return template;
  34. }
  35. }

2ff27699c141a49516882ba698a1e7e1.png

重启服务后发现成功了!

总结

在redis序列化配置中使用带参数的GenericFastJsonRedisSerializer,然后添加不支持的类名即可

  1. 添加不支持的类名

    String[] acceptNames = {“org.springframework.security.core.authority.SimpleGrantedAuthority”};

  2. 使用带参序列化对象

    GenericFastJsonRedisSerializer serializer = new GenericFastJsonRedisSerializer(acceptNames);

发表评论

表情:
评论列表 (有 0 条评论,147人围观)

还没有评论,来说两句吧...

相关阅读