国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

解決com.alibaba.fastjson.JSONException:autoType is not support問題,Redis FastJson

這篇具有很好參考價(jià)值的文章主要介紹了解決com.alibaba.fastjson.JSONException:autoType is not support問題,Redis FastJson。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

前言

最近在配置redis序列化問題的時(shí)候,使用fastjson來進(jìn)行序列化,報(bào)異常:

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

等等問題

本次出現(xiàn)問題是在fastjson反序列化springsecurity的UserDetails時(shí)出現(xiàn)的問題,報(bào)錯(cuò)代碼如下

解決com.alibaba.fastjson.JSONException:autoType is not support問題,Redis FastJson

環(huán)境

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

提示問題出現(xiàn)在ObjectReaderProvider.java:734行,這里autoTypeSupport默認(rèn)是true

解決com.alibaba.fastjson.JSONException:autoType is not support問題,Redis FastJson

大致意思是,類型沒有在白名單范圍之內(nèi),找了很多個(gè)博主的帖子,都沒有可行的解決方案,可能是我用的版本比較高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;
    }
}

現(xiàn)在只需要給fastjson添加白名單就行了

嘗試1(高版本不支持)

通過修改全局對(duì)象的方式進(jìn)行白名單設(shè)置

解決com.alibaba.fastjson.JSONException:autoType is not support問題,Redis FastJson

嘗試2(高版本不支持)

配置jvm啟動(dòng)參數(shù),上面都不支持,這個(gè)應(yīng)該也不支持

嘗試3(成功)

查看GenericFastJsonRedisSerializer源碼,發(fā)現(xiàn)里面有兩個(gè)構(gòu)造函數(shù),一個(gè)無參(我現(xiàn)在用的),另一個(gè)是contextFilter我也不知道是干嘛的,但是發(fā)現(xiàn)參數(shù)名竟然是acceptNames,理解意思大概是支持的名稱?

解決com.alibaba.fastjson.JSONException:autoType is not support問題,Redis FastJson

于是我就嘗試了有參構(gòu)造序列化

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

重啟服務(wù)后發(fā)現(xiàn)成功了!

總結(jié)

在redis序列化配置中使用帶參數(shù)的GenericFastJsonRedisSerializer,然后添加不支持的類名即可

  1. 添加不支持的類名

String[] acceptNames = {"org.springframework.security.core.authority.SimpleGrantedAuthority"};
  1. 使用帶參序列化對(duì)象文章來源地址http://www.zghlxwxcb.cn/news/detail-469046.html

GenericFastJsonRedisSerializer serializer = new GenericFastJsonRedisSerializer(acceptNames);

到了這里,關(guān)于解決com.alibaba.fastjson.JSONException:autoType is not support問題,Redis FastJson的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • Error:(3, 28) java: 程序包c(diǎn)om.alibaba.fastjson不存在

    Error:(3, 28) java: 程序包c(diǎn)om.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 報(bào)錯(cuò) : Error:(3, 28) java: 程序包c(diǎn)om.alibaba.fastjson不存在 解決:添加json包 1、點(diǎn)擊File——Project Structure ?2 、選擇Module——

    2024年02月03日
    瀏覽(25)
  • Fastjson JSONException: illegal identifier : \pos 2, line 1, column 3問題解決

    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)方法時(shí),text字符串內(nèi)部存在轉(zhuǎn)義字符,導(dǎo)致反序列化報(bào)錯(cuò)。 解決辦法:先去以下網(wǎng)站將字符串去轉(zhuǎn)義。 ? JSON在線 | JSON解析格式化—SO JSON在線工具 SO JSO

    2023年04月19日
    瀏覽(20)
  • 【Java】“com.alibaba.fastjson.JSONObject cannot be cast to“報(bào)錯(cuò)問題

    【Java】“com.alibaba.fastjson.JSONObject cannot be cast to“報(bào)錯(cuò)問題

    報(bào)錯(cuò)如下: 通過 debug 斷點(diǎn)可以看到,這里拿到雖然是 List,但是里面的對(duì)象還是一個(gè) JSONObject,并不是需要的 DTO 類,所有導(dǎo)致了后面的報(bào)錯(cuò)。 查到問題根源,只要把這里的對(duì)象轉(zhuǎn)化為 DTO 類就行了,就可以避免報(bào)錯(cuò)。 增加代碼: 我的json \\\"[{},{}]\\\" 已經(jīng)存為字符串所以改寫這樣

    2024年02月13日
    瀏覽(26)
  • git 報(bào)錯(cuò) protocol ‘https‘ is not supported解決

    git 報(bào)錯(cuò) protocol ‘https‘ is not supported解決

    報(bào)錯(cuò)原因:選擇不了其他分支代碼,甚至都看不到其他分支,我這邊解決了兩次報(bào)錯(cuò),情況如下: 第一種報(bào)錯(cuò): idea中刷新分支報(bào)錯(cuò)如下: Fetch Failed protocol \\\'\\\'https\\\' is not supported 話不多說,直接上 解決方案: ?1:可以直接在idea命令窗中執(zhí)行:git remote set-url origin 你的url 2.然后

    2024年02月06日
    瀏覽(19)
  • java.util.LinkedHashMap cannot be cast to com.alibaba.fastjson.JSONObject

    接收postman 發(fā)送的請(qǐng)求,請(qǐng)求參數(shù)是 JSONObject 格式,需要獲取其中的info對(duì)象 public void xxxxxx(@RequestBody JSONObject map) { // 先將info轉(zhuǎn)成json格式的字符串,再轉(zhuǎn)為JSON對(duì)象 JSONObject info = JSON.parseObject(JSON.toJSONString(map.get(“info”))); }

    2024年02月14日
    瀏覽(14)
  • ClassCastException: com.alibaba.fastjson.JSONObject cannot be cast to 接口json數(shù)據(jù)轉(zhuǎn)換異常。

    之前在使用fastjson進(jìn)行接口傳輸數(shù)據(jù)時(shí),碰到接收端數(shù)據(jù)轉(zhuǎn)換異常問題,難了我好久。 例如:我們需要將json轉(zhuǎn)換成實(shí)例集合 通常對(duì)JSON對(duì)象的轉(zhuǎn)換操作是使用 強(qiáng)轉(zhuǎn) 導(dǎo)致一些朋友對(duì)實(shí)例對(duì)象的操作也是進(jìn)行強(qiáng)轉(zhuǎn) 如下 這樣的話我們?cè)趯?duì) list 操作時(shí)就會(huì)報(bào)類似如下錯(cuò)誤 所以之后

    2024年02月10日
    瀏覽(23)
  • fastjson2 打開 AutoType

    FASTJSON支持AutoType功能,這個(gè)功能在序列化的JSON字符串中帶上類型信息,在反序列化時(shí),不需要傳入類型,實(shí)現(xiàn)自動(dòng)類型識(shí)別。 必須顯式打開才能使用。和fastjson 1.x不一樣,fastjson 1.x為了兼容有一個(gè)白名單,在fastjson 2中,沒有任何白名單,也不包括任何Exception類的白名單,必

    2024年02月10日
    瀏覽(17)
  • FastJson中AutoType反序列化漏洞

    FastJson中AutoType反序列化漏洞

    Fastjson1.2.80 反序列化漏洞情報(bào),攻擊者可以利用該漏洞攻擊遠(yuǎn)程服務(wù)器, 可能會(huì)造成任意命令執(zhí)行。在Fastjson=1.2.83的版本中,通過新的Gadgets鏈繞過autoType開關(guān),在autoType關(guān)閉的情況下仍然可能可以繞過黑白名單防御機(jī)制,實(shí)現(xiàn)了反序列化漏洞利用的遠(yuǎn)程代碼執(zhí)行效果,同時(shí),此

    2024年02月07日
    瀏覽(18)
  • Cloning into ‘XXXX‘... fatal: protocol ‘?https‘ is not supporte 報(bào)錯(cuò)解決方法

    git bash 中出現(xiàn)信息如下信息: Cloning into \\\'XXXX\\\'... fatal: protocol \\\'?https\\\' is not supporte? 經(jīng)過百度搜索: 可能存在問題一:git clone 使用的時(shí)候不支持https,可能需要換成SSH方式 你可以通過命令git remote set-url origin 你倉庫的SSH地址,去除SSH認(rèn)證。 但是感覺不太實(shí)際。 然后繼續(xù)看下一篇

    2023年04月12日
    瀏覽(30)
  • fastjson漏洞修復(fù):開啟safeMode來禁用autoType

    fastjson漏洞修復(fù):開啟safeMode來禁用autoType

    Fastjson官方再次披露嚴(yán)重漏洞,包括rocketmq、jeecg-boot等近15%的github開源項(xiàng)目受影響 2022年5月23日,fastjson 官方發(fā)布安全通報(bào),fastjson = 1.2.80 存在反序列化任意代碼執(zhí)行漏洞,在特定條件下可繞過默認(rèn)autoType關(guān)閉限制,可能會(huì)導(dǎo)致遠(yuǎn)程服務(wù)器被攻擊。 漏洞信息如下: 漏洞評(píng)級(jí):

    2024年02月04日
    瀏覽(15)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包