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

这篇具有很好参考价值的文章主要介绍了解决com.alibaba.fastjson.JSONException:autoType is not support问题,Redis FastJson。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

前言

最近在配置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时出现的问题,报错代码如下

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

环境

java 17

springboot 3.0.4

fastjson 2.0.22

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>2.0.22</version>
        </dependency>

寻找问题根源

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

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

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

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

这下面是我的Redis配置

import com.alibaba.fastjson.support.spring.GenericFastJsonRedisSerializer;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
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.StringRedisSerializer;

/**
 * @description 缓存配置类
 * @author ouyang
 * @version 1.0
 * @date 2023-03-14 8:25
 */
@Configuration
@EnableCaching
public class RedisConfig extends CachingConfigurerSupport {

    @Bean
    @SuppressWarnings(value = { "unchecked", "rawtypes" })
    public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
        RedisTemplate<Object, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(connectionFactory);
        GenericFastJsonRedisSerializer serializer = new GenericFastJsonRedisSerializer();
        // 使用GenericFastJsonRedisSerializer来序列化和反序列化redis的key值
        template.setKeySerializer(new StringRedisSerializer());
        template.setValueSerializer(serializer);
        // Hash的key也采用StringRedisSerializer的序列化方式
        template.setHashKeySerializer(new StringRedisSerializer());
        template.setHashValueSerializer(serializer);

        template.afterPropertiesSet();
        return template;
    }
}

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

尝试1(高版本不支持)

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

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

尝试2(高版本不支持)

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

尝试3(成功)

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

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

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

import com.alibaba.fastjson.support.spring.GenericFastJsonRedisSerializer;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
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.StringRedisSerializer;

/**
 * @description 缓存配置类
 * @author ouyang
 * @version 1.0
 * @date 2023-03-14 8:25
 */
@Configuration
@EnableCaching
public class RedisConfig extends CachingConfigurerSupport {

    @Bean
    @SuppressWarnings(value = { "unchecked", "rawtypes" })
    public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
        RedisTemplate<Object, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(connectionFactory);

        // 解决autoType is not support.xxxx.xx的问题
        String[] acceptNames = {"org.springframework.security.core.authority.SimpleGrantedAuthority"};
        GenericFastJsonRedisSerializer serializer = new GenericFastJsonRedisSerializer(acceptNames);
        
        // 使用GenericFastJsonRedisSerializer来序列化和反序列化redis的key值
        template.setKeySerializer(new StringRedisSerializer());
        template.setValueSerializer(serializer);
        // Hash的key也采用StringRedisSerializer的序列化方式
        template.setHashKeySerializer(new StringRedisSerializer());
        template.setHashValueSerializer(serializer);

        template.afterPropertiesSet();
        return template;
    }
}
解决com.alibaba.fastjson.JSONException:autoType is not support问题,Redis FastJson

重启服务后发现成功了!

总结

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

  1. 添加不支持的类名

String[] acceptNames = {"org.springframework.security.core.authority.SimpleGrantedAuthority"};
  1. 使用带参序列化对象文章来源地址https://www.toymoban.com/news/detail-469046.html

GenericFastJsonRedisSerializer serializer = new GenericFastJsonRedisSerializer(acceptNames);

到了这里,关于解决com.alibaba.fastjson.JSONException:autoType is not support问题,Redis FastJson的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Error:(3, 28) java: 程序包com.alibaba.fastjson不存在

    情景:查看WebRoot——WEB-INF——lib已存在json包 json-lib-2.4-jdk15.jar commons-beanutils-1.8.0.jar commons-logging-1.1.1.jar commons-collections-3.2.1.jar commons-lang-2.5.jar ezmorph-1.0.6.jar 报错 : Error:(3, 28) java: 程序包com.alibaba.fastjson不存在 解决:添加json包 1、点击File——Project Structure  2 、选择Module——

    2024年02月03日
    浏览(33)
  • Fastjson JSONException: illegal identifier : \pos 2, line 1, column 3问题解决

    com.alibaba.fastjson.JSONException: illegal identifier : pos 2, line 1, column 3 问题分析: 1、使用了JSONArray.parseArray(String text, ClassT clazz)方法时,text字符串内部存在转义字符,导致反序列化报错。 解决办法:先去以下网站将字符串去转义。   JSON在线 | JSON解析格式化—SO JSON在线工具 SO JSO

    2023年04月19日
    浏览(54)
  • 【Java】“com.alibaba.fastjson.JSONObject cannot be cast to“报错问题

    报错如下: 通过 debug 断点可以看到,这里拿到虽然是 List,但是里面的对象还是一个 JSONObject,并不是需要的 DTO 类,所有导致了后面的报错。 查到问题根源,只要把这里的对象转化为 DTO 类就行了,就可以避免报错。 增加代码: 我的json \\\"[{},{}]\\\" 已经存为字符串所以改写这样

    2024年02月13日
    浏览(42)
  • git 报错 protocol ‘https‘ is not supported解决

    报错原因:选择不了其他分支代码,甚至都看不到其他分支,我这边解决了两次报错,情况如下: 第一种报错: idea中刷新分支报错如下: Fetch Failed protocol \\\'\\\'https\\\' is not supported 话不多说,直接上 解决方案:  1:可以直接在idea命令窗中执行:git remote set-url origin 你的url 2.然后

    2024年02月06日
    浏览(48)
  • java.util.LinkedHashMap cannot be cast to com.alibaba.fastjson.JSONObject

    接收postman 发送的请求,请求参数是 JSONObject 格式,需要获取其中的info对象 public void xxxxxx(@RequestBody JSONObject map) { // 先将info转成json格式的字符串,再转为JSON对象 JSONObject info = JSON.parseObject(JSON.toJSONString(map.get(“info”))); }

    2024年02月14日
    浏览(24)
  • ClassCastException: com.alibaba.fastjson.JSONObject cannot be cast to 接口json数据转换异常。

    之前在使用fastjson进行接口传输数据时,碰到接收端数据转换异常问题,难了我好久。 例如:我们需要将json转换成实例集合 通常对JSON对象的转换操作是使用 强转 导致一些朋友对实例对象的操作也是进行强转 如下 这样的话我们在对 list 操作时就会报类似如下错误 所以之后

    2024年02月10日
    浏览(34)
  • fastjson2 打开 AutoType

    FASTJSON支持AutoType功能,这个功能在序列化的JSON字符串中带上类型信息,在反序列化时,不需要传入类型,实现自动类型识别。 必须显式打开才能使用。和fastjson 1.x不一样,fastjson 1.x为了兼容有一个白名单,在fastjson 2中,没有任何白名单,也不包括任何Exception类的白名单,必

    2024年02月10日
    浏览(30)
  • FastJson中AutoType反序列化漏洞

    Fastjson1.2.80 反序列化漏洞情报,攻击者可以利用该漏洞攻击远程服务器, 可能会造成任意命令执行。在Fastjson=1.2.83的版本中,通过新的Gadgets链绕过autoType开关,在autoType关闭的情况下仍然可能可以绕过黑白名单防御机制,实现了反序列化漏洞利用的远程代码执行效果,同时,此

    2024年02月07日
    浏览(38)
  • Cloning into ‘XXXX‘... fatal: protocol ‘?https‘ is not supporte 报错解决方法

    git bash 中出现信息如下信息: Cloning into \\\'XXXX\\\'... fatal: protocol \\\'?https\\\' is not supporte  经过百度搜索: 可能存在问题一:git clone 使用的时候不支持https,可能需要换成SSH方式 你可以通过命令git remote set-url origin 你仓库的SSH地址,去除SSH认证。 但是感觉不太实际。 然后继续看下一篇

    2023年04月12日
    浏览(36)
  • fastjson漏洞修复:开启safeMode来禁用autoType

    Fastjson官方再次披露严重漏洞,包括rocketmq、jeecg-boot等近15%的github开源项目受影响 2022年5月23日,fastjson 官方发布安全通报,fastjson = 1.2.80 存在反序列化任意代码执行漏洞,在特定条件下可绕过默认autoType关闭限制,可能会导致远程服务器被攻击。 漏洞信息如下: 漏洞评级:

    2024年02月04日
    浏览(30)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包