1. 程式人生 > >Caused by: com.alibaba.fastjson.JSONException: autoType is not support

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);
    }