Exception in thread "main" com.alibaba.fastjson.JSONException: autoType is not support. com.accord.f

将类反序列化的时候,报错:

Exception in thread "main" com.alibaba.fastjson.JSONException: autoType is not support. com.accord.fastjson.User
	at com.alibaba.fastjson.parser.ParserConfig.checkAutoType(ParserConfig.java:1026)
	at com.alibaba.fastjson.parser.DefaultJSONParser.parseObject(DefaultJSONParser.java:316)
	at com.alibaba.fastjson.parser.DefaultJSONParser.parse(DefaultJSONParser.java:1356)
	at com.alibaba.fastjson.parser.DefaultJSONParser.parse(DefaultJSONParser.java:1322)
	at com.alibaba.fastjson.JSON.parse(JSON.java:152)
	at com.alibaba.fastjson.JSON.parse(JSON.java:162)
	at com.alibaba.fastjson.JSON.parse(JSON.java:131)
	at com.accord.fastjson.FastJson.main(FastJson.java:111)

百度一下方法:

网上查了下相关的资料,有几篇分析可以参考:

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

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

大体原因就是使用fastjson的时候:序列化时将class信息写入,反解析的时候,fastjson默认情况下会开启autoType的检查,相当于一个白名单检查吧,如果序列化信息中的类路径不在autoType中,反解析就会报上面的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."); 

如果有多个包名前缀,分多次addAccept

2. 加上JVM启动参数

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

如果有多个包名前缀,用逗号隔开

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

在1.2.25/1.2.26版本支持通过类路径的fastjson.properties文件来配置,配置方式如下:

fastjson.parser.autoTypeAccept=com.taobao.pac.client.sdk.dataobject.,com.cainiao. // 如果有多个包名前缀,用逗号隔开

二、打开autotype功能

如果通过配置白名单解决不了问题,可以选择继续打开autotype功能,fastjson在新版本中内置了多重防护,但是还是可能会存在一定风险。两种方法打开autotype,二选一,如下:

1、JVM启动参数

-Dfastjson.parser.autoTypeSupport=true

2、代码中设置

ParserConfig.getGlobalInstance().setAutoTypeSupport(true); 

如果有使用非全局ParserConfig则用另外调用setAutoTypeSupport(true);


我用的方法:加上这句:

ParserConfig.getGlobalInstance().setAutoTypeSupport(true);

就解决问题了!



`com.alibaba.fastjson.JSONException: autoType is not support.` 这个异常通常发生在使用Fastjson序列化或反序列化JSON数据时,如果开启了`autoTypeSupport`(自动类型支持)功能,而Fastjson版本较高,可能会触发这个异常。从Fastjson 1.2.38版本开始,默认禁用了`autoTypeSupport`功能,主要是出于安全考虑,因为`autoType`可能会导致安全漏洞。 `com.kjlink.framework.tyq.dto.TyqComOrderResponse` 看起来像是你项目中的一个类。当尝试反序列化JSON字符串为这个类的对象时,如果JSON字符串中包含了类的全路径信息,Fastjson默认会进行类型检查,防止潜在的安全风险。 如果你确认使用这个类是安全的,并且确实需要反序列化操作,你可以通过以下方法解决这个问题: 1. 设置`fastjson.parser.autoTypeSupport`为`true`,但这不推荐,因为它会降低安全性。在使用时,可以这样做: ```java JSONParser parser = new JSONParser(); parser.getConfig().setAutoTypeSupport(true); ``` 2. 如果使用的是Fastjson 1.2.47或更高版本,可以使用`JSON.parseObject(String text, Class<T> clazz, Feature... features)`方法,并指定`Feature.AllowAutoType`: ```java JSON.parseObject(jsonString, TyqComOrderResponse.class, Feature.AllowAutoType); ``` 3. 如果你的JSON字符串中明确指定了类名,并且类的全路径信息是安全的,你可以使用`ParserConfig.getGlobalInstance().addAccept("你的包名.")`来添加信任的包路径: ```java ParserConfig.getGlobalInstance().addAccept("com.kjlink.framework.tyq.dto."); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值