Javassist字节码检测在运行时为Springboot配置类创建NoClassDefFoundError

问题描述

我试图在构建期间使用带有gradle build的javassist修改SpringBoot配置java类。我的意图是基于构建配置删除SpringBoot应用程序的依赖项jar(春季安全性)。为此,我首先使用javassist在构建期间从配置类中删除了在配置类中使用spring安全性Authentication类的方法

Classpool pool = Classpool.getDefault();
CtClass cc = pool.get("com.xxx.xxx.SpringDataConfig");
cc.removeMethod(cc.getDeclaredMethod("auditor"));
cc.writeFile("./build/classes/java/main");

和gradle任务为

task applyCodeChange(type: JavaExec) {
    group = "Execution"
    description = "Code config"
    classpath = sourceSets.main.runtimeClasspath
    main = "com.xxx.xxx.CodeConfig"
  }
it.tasks.jar.dependsOn it.tasks.applyCodeChange

在执行特定项目的构建任务之后。最终的jar将具有不带该方法修改后的类文件(使用Java反编译器进行了验证)。但是,当我尝试运行该jar时,我面临该方法使用的依赖项的NoClassDefFoundError。我也可以在反编译的类文件中看到该依赖项的Import语句。

下面是我尝试删除方法

 public AuditorAware<String> auditor() {
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    return () -> Optional.ofNullable(authentication.getName());
  }

我总能看到

导入org.springframework.security.core.Authentication

在反编译类中,但不是org.springframework.security.core.context.SecurityContextHolder。我还确认该类中没有其他方法在该文件中使用Authentication类,并且在该应用程序运行时始终将java.lang.NoClassDefFoundError用于org / springframework / security / core / Authentication。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)