Caused by: com.alibaba.fastjson.JSONException: autoType is not support

大体原因就是使用fastjson的时候:序列化时将class信息写入,反解析的时候,fastjson默认情况下会开启autoType的检查,相当于一个白名单检查吧,如果序列化信息中的类路径不在autoType中,反解析就会报上面的com.alibaba.fastjson.JSONException: autoType is not support的异常

打开autotype功能

1、JVM启动参数

   -Dfastjson.parser.autoTypeSupport=true

2.代码中程序控制

static {
        // 针对fastjson暴露的严重bug修改 https://github.com/alibaba/fastjson/wiki/enable_autotype
        String methodName = "addAccept";
        String args = "com.anzhi."; //置顶包,多个包用,分割
        ParserConfig parserConfig = ParserConfig.getGlobalInstance();
        boolean executed = false;
        try {
            for(Method m : ParserConfig.class.getDeclaredMethods()){
                if(m.getName().equals(methodName)){
                    m.invoke(parserConfig, args);
                    executed = true;
                }
            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (Throwable e){
            e.printStackTrace();
        }
        System.out.println("fastjson 漏洞修复,白名单添加结果: "+executed);
    }

猜你喜欢

转载自blog.csdn.net/qq_31459039/article/details/80264925
今日推荐