fastjson反序列化TemplatesImpl链

fastjson反序列化TemplatesImpl链

FastJson的用法

首先熟悉一下FastJson如何使用,FastJson是阿里巴巴开发的一个解析Json格式的第三方包。

序列化

FastJson在序列化A时会调用Aget方法获得数据。

另外toJSONString()方法在进行序列化时还可以指定一个可选的SerializerFeature.WriteClassName参数,指定了该参数后,在序列化时json数据中会写入一个 @type 选项。

json数据中的 @type 选项用于指定反序列化的类,也就是说所,当这段json数据被反序列化时,会按照 @type 选项中指定的类全名反序列化成java对象。

反序列化

fastjson提供了两个反序列化函数:parseObject()parse()

public class FastJsonDemo01 {
   
    public static void main(String[] args) {
   
        Student student = new Student("A",11);
        String jsonString = JSON.toJSONString(student, SerializerFeature.WriteClassName);
        System.out.println(jsonString);

        System.out.println("============1==============");
        JSONObject jsonObject = JSON.parseObject(jsonString);
        System.out.println("jsonObject --->" + jsonObject);
    }
}

/*
method: Student(String name, int age)
method: getAge()
method: getName()
{"@type":"fastJsonDemo.Student","age":11,"name":"A"}
============1==============
method: Student()
method: setAge(int age)
method: setName(String name)
method: getAge()
method: getName()
jsonObject --->{"name":"A","age":11}
*/

JSON.parseObject(jsonString)在反序列化过程中会调用对象的settergetter方法。

public class FastJsonDemo01 {
   
    public static void main(String[] args) {
   
        Student student = new Student("A",11);
        String jsonString = JSON.toJSONString(student, SerializerFeature.WriteClassName);
        System.out.println(jsonString);

        System.out.println("============2==============");
        Student jsonStudent = JSON.parseObject(jsonString,Student.class);
        System.out.println("jsonStudent --->" + jsonStudent);
    }
}

/*
============2==============
method: Student()
method: setAge(int age)
method: setName(String name)
jsonStudent --->fastJsonDemo.Student@497470ed
*/

parseObject(jsonString,Student.class)这种指定了反序列化类型的方式只会调用Studentset方法。

public class FastJsonDemo01 {
   
    public static void main(String[] args) {
   
        Student student = new Student("A",11);
        String jsonString = JSON.toJSONString(student, SerializerFeature.WriteClassName);
        System.out.println(jsonString);

        System.out.println("============3==============");
        Object obj = JSON.parse(jsonString);
        System.out.println("obj --->" + obj);
    }
}
/*
============3==============
method: Student()
method: setAge(int age)
method: setName(String name)
obj --->fastJsonDemo.Student@63c12fb0
*/

JSON.parse(jsonString)在反序列化过程中会调用对象的setter方法。

而当序列化对象中的某个set变成了private权限,那么这个field将不会被反序列化,除非加上Feature.SupportNonPublicField参数。

可以参考13-java安全——fastjson1.2.24反序列化TemplatesImpl利用链分析

TemplatesImpl利用链

首先来看com.sun.org.apache.xalan.internal.xsltc.trax.TemplatesImpl类,

/**
* This method generates an instance of the translet class that is
* wrapped inside this Template. The translet instance will later
* be wrapped inside a Transformer object.
*/
private Translet getTransletInstance()
        throws TransformerConfigurationException {
   
        try {
   
            if (_name == null) return null;

            if (_class == null) defineTransletClasses();

            // The translet needs to keep a reference to all its auxiliary
            // class to prevent the GC from collecting them
            AbstractTranslet translet = (AbstractTranslet) _class[_transletIndex]
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值