微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决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 举报,一经查实,本站将立刻删除。