日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

解决com.alibaba.fastjson.JSONException: autoType is not support

發(fā)布時(shí)間:2025/3/21 55 豆豆
生活随笔 收集整理的這篇文章主要介紹了 解决com.alibaba.fastjson.JSONException: autoType is not support 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

轉(zhuǎn)載自?https://blog.csdn.net/cdyjy_litao/article/details/72458538

?

最近發(fā)現(xiàn)進(jìn)程運(yùn)行日志中出現(xiàn)很多下面的日志:

?

  • com.alibaba.fastjson.JSONException:?autoType is not support. com.jd.ac.domain.api.offline.UserInfo
  • at com.alibaba.fastjson.parser.ParserConfig.checkAutoType(ParserConfig.java:882)
  • at com.alibaba.fastjson.parser.DefaultJSONParser.parseObject(DefaultJSONParser.java:322)
  • at com.alibaba.fastjson.parser.DefaultJSONParser.parse(DefaultJSONParser.java:1327)
  • at com.alibaba.fastjson.parser.deserializer.JavaObjectDeserializer.deserialze(JavaObjectDeserializer.java:45)
  • at com.alibaba.fastjson.parser.deserializer.FastjsonASMDeserializer_16_Order.deserialze(Unknown Source)
  • at com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer.deserialze(JavaBeanDeserializer.java:184)
  • at com.alibaba.fastjson.parser.DefaultJSONParser.parseObject(DefaultJSONParser.java:368)
  • at com.alibaba.fastjson.parser.DefaultJSONParser.parse(DefaultJSONParser.java:1327)
  • at com.alibaba.fastjson.parser.DefaultJSONParser.parse(DefaultJSONParser.java:1293)
  • at com.alibaba.fastjson.JSON.parse(JSON.java:137)
  • at com.alibaba.fastjson.JSON.parse(JSON.java:128)

?

網(wǎng)上查了下相關(guān)的資料,有幾篇分析可以參考:

http://www.tinygroup.org/docs/3281429682083150397

https://github.com/alibaba/fastjson/wiki/enable_autotype

?

大體原因就是使用fastjson的時(shí)候:序列化時(shí)將class信息寫入,反解析的時(shí)候,fastjson默認(rèn)情況下會(huì)開啟autoType的檢查,相當(dāng)于一個(gè)白名單檢查吧,如果序列化信息中的類路徑不在autoType中,反解析就會(huì)報(bào)上面的com.alibaba.fastjson.JSONException:?autoType is not support的異常

public Class<?> checkAutoType(String typeName, Class<?> expectClass) {
/* 805 */ if (typeName == null) {
/* 806 */ return null;
/* ? ? */ }
/* ? ? */
/* 809 */ String className = typeName.replace('$', '.');
/* ? ? */
/* 811 */ if ((this.autoTypeSupport) || (expectClass != null)) {
/* 812 */ for (int i = 0; i < this.acceptList.length; ++i) {
/* 813 */ String accept = this.acceptList[i];
/* 814 */ if (className.startsWith(accept)) {
/* 815 */ return TypeUtils.loadClass(typeName, this.defaultClassLoader);
/* ? ? */ }
/* ? ? */ }
/* ? ? */
/* 819 */ for (int i = 0; i < this.denyList.length; ++i) {
/* 820 */ String deny = this.denyList[i];
/* 821 */ if (className.startsWith(deny)) {
/* 822 */ throw new JSONException("autoType is not support. " + typeName);
/* ? ? */ }
/* ? ? */ }
/* ? ? */ }
/* ? ? */
/* 827 */ Class clazz = TypeUtils.getClassFromMapping(typeName);
/* 828 */ if (clazz == null) {
/* 829 */ clazz = this.deserializers.findClass(typeName);
/* ? ? */ }
/* ? ? */
/* 832 */ if (clazz != null) {
/* 833 */ if ((expectClass != null) && (!(expectClass.isAssignableFrom(clazz)))) {
/* 834 */ throw new JSONException("type not match. " + typeName + " -> " + expectClass.getName());
/* ? ? */ }
/* ? ? */
/* 837 */ return clazz;
/* ? ? */ }
/* ? ? */
/* 840 */ if (!(this.autoTypeSupport)) {
/* 841 */ for (int i = 0; i < this.denyList.length; ++i) {
/* 842 */ String deny = this.denyList[i];
/* 843 */ if (className.startsWith(deny)) {
/* 844 */ throw new JSONException("autoType is not support. " + typeName);
/* ? ? */ }
/* ? ? */ }
/* 847 */ for (int i = 0; i < this.acceptList.length; ++i) {
/* 848 */ String accept = this.acceptList[i];
/* 849 */ if (className.startsWith(accept)) {
/* 850 */ clazz = TypeUtils.loadClass(typeName, this.defaultClassLoader);
/* ? ? */
/* 852 */ if ((expectClass != null) && (expectClass.isAssignableFrom(clazz))) {
/* 853 */ throw new JSONException(
"type not match. " + typeName + " -> " + expectClass.getName());
/* ? ? */ }
/* 855 */ return clazz;
/* ? ? */ }
/* ? ? */ }
/* ? ? */ }
/* ? ? */
/* 860 */ if ((this.autoTypeSupport) || (expectClass != null)) {
/* 861 */ clazz = TypeUtils.loadClass(typeName, this.defaultClassLoader);
/* ? ? */ }
/* ? ? */
/* 864 */ if (clazz != null)
/* ? ? */ {
/* 866 */ if ((ClassLoader.class.isAssignableFrom(clazz)) ||
/* 867 */ (DataSource.class/* 867 */ .isAssignableFrom(clazz)))
/* ? ? */ {
/* 869 */ throw new JSONException("autoType is not support. " + typeName);
/* ? ? */ }
/* ? ? */
/* 872 */ if (expectClass != null) {
/* 873 */ if (expectClass.isAssignableFrom(clazz)) {
/* 874 */ return clazz;
/* ? ? */ }
/* 876 */ throw new JSONException("type not match. " + typeName + " -> " + expectClass.getName());
/* ? ? */ }
/* ? ? */
/* ? ? */ }
/* ? ? */
/* 881 */ if (!(this.autoTypeSupport)) {
/* 882 */?throw new JSONException("autoType is not support. " + typeName);
/* ? ? */ }
/* ? ? */
/* 885 */ return clazz;
/* ? ? */ }

?

參考 https://github.com/alibaba/fastjson/wiki/enable_autotype ?講解了3種方式添加autoType的白名單:

?

一、添加autotype白名單

?

添加白名單有三種方式,三選一,如下:

1. 在代碼中配置

ParserConfig.getGlobalInstance().addAccept("com.taobao.pac.client.sdk.dataobject.");

如果有多個(gè)包名前綴,分多次addAccept

2. 加上JVM啟動(dòng)參數(shù)

-Dfastjson.parser.autoTypeAccept=com.taobao.pac.client.sdk.dataobject.,com.cainiao.

如果有多個(gè)包名前綴,用逗號(hào)隔開

3. 通過fastjson.properties文件配置。

在1.2.25/1.2.26版本支持通過類路徑的fastjson.properties文件來配置,配置方式如下:

fastjson.parser.autoTypeAccept=com.taobao.pac.client.sdk.dataobject.,com.cainiao. // 如果有多個(gè)包名前綴,用逗號(hào)隔開

二、打開autotype功能

如果通過配置白名單解決不了問題,可以選擇繼續(xù)打開autotype功能,fastjson在新版本中內(nèi)置了多重防護(hù),但是還是可能會(huì)存在一定風(fēng)險(xiǎn)。兩種方法打開autotype,二選一,如下:

1、JVM啟動(dòng)參數(shù)

-Dfastjson.parser.autoTypeSupport=true

2、代碼中設(shè)置

ParserConfig.getGlobalInstance().setAutoTypeSupport(true);

如果有使用非全局ParserConfig則用另外調(diào)用setAutoTypeSupport(true);

總結(jié)

以上是生活随笔為你收集整理的解决com.alibaba.fastjson.JSONException: autoType is not support的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。