Now there is a need to add a bean to the spring container during program operation. This bean is the configuration class of spring AOP. How to make the configuration take effect.
I know that the @Component annotation needs to be configured under normal circumstances, but I need to configure it during runtime instead of scanning the configuration at startup
At first, I wrote this
@Aspect
public class ParamAspect {
private static String result;
@Pointcut("execution(public * com.example.aop.*.*(String))")
public void doOperation() {...}
@Before("doOperation()")
public void before(JoinPoint joinPoint) throws Exception {...}
@AfterReturning(returning = "object", pointcut = "doOperation()")
public void doAfterReturning(Object object) {...}
}
registry the bean
DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) beanFactoryField.get(ctx);
BeanDefinitionRegistry beanDefReg = beanFactory;
BeanDefinitionBuilder beanDefBuilder = BeanDefinitionBuilder.genericBeanDefinition(ParamAspect.class);
BeanDefinition beanDef = beanDefBuilder.getBeanDefinition();
if (!beanDefReg.containsBeanDefinition("theBean")) {
beanDefReg.registerBeanDefinition("theBean", beanDef);
}
But I find it doesn't work Then I try the method provided online
public class Config {
private Advisor config(){
AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
pointcut.setExpression("execution(public * com.example.aop.*.*(String))");
return new DefaultPointcutAdvisor(pointcut, new MyMethodInterceptor());
}
}
public class MyMethodInterceptor implements MethodInterceptor {
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
...
}
}
and change class
BeanDefinitionBuilder beanDefBuilder = BeanDefinitionBuilder.genericBeanDefinition(Config.class);
It still doesn't work
I am engaged in Java security research and want to find a way to dynamically configure AOP to execute malicious code.So I want to find a way to make AOP effective after registering the beans configured by AOP.
If there is a deserialization vulnerability in the application, the hacker can trigger the function of registering AOP through a maliciously constructed object, and then execute malicious code.