Dynamic agent for java learning

Dynamic agent for java learning

0x00 Preface

In the later study of vulnerability research, we must know several knowledge points. Reflection mechanism and dynamic proxy mechanism. As for reflection, I have already mentioned it before, so I won't go into more details here.

0x01 dynamic agent

Here is a definition of some dynamic agents.

([manual funny] in fact, the dynamic agent is a middleman who indirectly delivers things to customers, but before that, it must earn some price difference)

The main purpose of using dynamic agent in our development is to enhance the method without changing the target object method.

Here are a few more concepts:

1. 真实对象:被代理的对象

2. 代理对象:

3. 代理模式:代理对象代理真实对象,达到增强真实对象功能的目的

Let's write a piece of code that normally calls a method using a real object.

Computer interface code:

package com.test.web.proxy;

public interface computer {
    public String method(double money);
    String show();
}


Lenveo class code:

package com.test.web.proxy;

public class lenveo implements computer{
    @Override
    public String method(double money) {
        System.out.println("花了+"+money+"买的电脑");
        return "电脑";

    }

    @Override
    public String show() {
        System.out.println("展示电脑");

        return "展示电脑";
    }
}

Test class code:

package com.test.web.proxy;

public class test1 {
    public static void main(String[] args) {
        lenveo lenveo = new lenveo();
        String method = lenveo.method(8000);
        String show = lenveo.show();
        System.out.println(show);
        System.out.println(method);




    }
}

This is a normal piece of code. If we need to enhance it without changing other classes of code, we can use the dynamic agent.

Let's take a look at the implementation steps of dynamic agent to implement dynamic agent according to the steps.

package com.test.web.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class test {
    public static void main(String[] args) {
        lenveo lenveo = new lenveo();


                                            //获取传入类加载器                    //获取传入真实对象所有接口
        computer proxy = (computer) Proxy.newProxyInstance(lenveo.getClass().getClassLoader(),lenveo.getClass().getInterfaces(),new InvocationHandler() {
            @Override
            public Object invoke(Object proxy,Method method,Object[] args) throws Throwable {
                System.out.println("invoke方法执行了");
                Object obj = method.invoke(lenveo,args);

                return obj;
            }
        });
        String method = proxy.method(3000);
        System.out.println(method);

    }
}

Proxy here The newproxyinstance method passed in three parameters:

1.类的加载器:new出来的真实对象.getClass().getClassLoader()

2.接口数组:new出来的真实对象.getClass().getInterfaces()

3.处理器:new InvocationHandler() 

Invoke parameter:

1.proxy:代理对象

2.method:代理对象调用的方法,被封装成的对象

3.args:代理对象调用方法适合传递的实际参数

0x02 end

Well, after another article, the dynamic agent mechanism is still more important. Here are just some simple dynamic agents. Update the dynamic agent when you are free.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>