关于springboot:SpringBoot自定义mavenplugin插件整合asm代码插桩

6次阅读

共计 3470 个字符,预计需要花费 9 分钟才能阅读完成。

背景

公司开发框架减少了 web 零碎 license 受权证书校验模块,履行一台机器一个受权证书,初步计划是减少拦截器针对全局申请进行拦挡校验,评估后认为校验形式繁多,应该减少重要工具类,业务 service 实现中每个办法的进行校验,因为波及代码量较大硬编码工作艰难,故抉择通过自定义 maven 插件在编译期间进行动静代码插桩操作

我的项目配置

新建 maven 我的项目设置打包形式

<packaging>maven-plugin</packaging>

减少依赖项

         <!-- 应用 doc 的形式 -->
        <dependency>
            <groupId>org.apache.maven</groupId>
            <artifactId>maven-plugin-api</artifactId>
            <version>3.5.2</version>
        </dependency>
        <!-- 应用注解的形式 -->
        <dependency>
            <groupId>org.apache.maven.plugin-tools</groupId>
            <artifactId>maven-plugin-annotations</artifactId>
            <version>3.5.2</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.maven</groupId>
            <artifactId>maven-project</artifactId>
            <version>2.2.1</version>
        </dependency>
        <dependency>
            <groupId>org.ow2.asm</groupId>
            <artifactId>asm</artifactId>
            <version>9.0</version>
        </dependency>

build 内容配置

     <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-plugin-plugin</artifactId>
                <version>3.5</version>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.6.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>

编译拦挡

创立编译操作类 FramePlugin,继承 AbstractMojo 并应用 Mojo 注解标注,output 参数是 class 文件编译后门路

@Mojo(name = "deepcompile", defaultPhase = LifecyclePhase.COMPILE)
public class FramePlugin extends AbstractMojo {@Parameter(name = "output", defaultValue = "${project.build.directory}")
    private File output;
    public void execute() throws MojoExecutionException {
        File f = ;
        if (!f.exists()) {f.mkdirs();
        }
        try {insertPile(f);
        } catch (Exception e) {
            exceptioncount++;
            e.printStackTrace();}
    }

ASM 插桩

新建 ClassVisitor 重写 visitMethod 办法来过滤拜访须要插桩的办法,须要排除自带的 init 办法

public class MethodCoverageClassVisitor extends ClassVisitor {public MethodCoverageClassVisitor(ClassVisitor classVisitor) {super(Opcodes.ASM9, classVisitor);
    }

    @Override
    public MethodVisitor visitMethod(int access, String name, String descriptor, String signature,
                                     String[] exceptions) {final MethodVisitor methodVisitor = super.visitMethod(access, name, descriptor, signature, exceptions);
        if (name.equals("<init>")) {return methodVisitor;}
        return new MethodCoverageMethodVisitor(Opcodes.ASM9, methodVisitor);
    }
}

新建 MethodVisitor 重写 visitCode 办法针对办法外部字节码进行自定义操作,这里是应用框架外部封装好的一个静态方法来校验 license 证书

public class MethodCoverageMethodVisitor extends MethodVisitor {public MethodCoverageMethodVisitor(int api, MethodVisitor methodVisitor) {super(api, methodVisitor);
    }

    @Override
    public void visitCode() {mv.visitFieldInsn(Opcodes.INVOKESTATIC, "com/xxxx/frame/common/utils/ComplieSDK", "checkLicense", "()V");
    }
}

最初在 execute 中进行文件递归查找调用,就是将曾经编译的 class 文件读取 / 自定义操作后保留

 private void insertPile(File root) throws IOException {if (root.isDirectory()) {for (File file : root.listFiles()) {insertPile(file);
            }
        }
        String className = root.getName().replace(".class", "");
        if (root.getName().endsWith(".class")) {
            //class 筛选
            boolean flag = false;
             // 自定义的 class 文件筛选条件代码
            if (flag) {System.out.println("【insertPile】:" + className);
                FileOutputStream fos = null;
                try {final byte[] instrumentBytes = doInsertPile(root);
                    fos = new FileOutputStream(root);
                    fos.write(instrumentBytes);
                    fos.flush();} catch (MojoExecutionException e) {System.out.println("【insertPile-exception】:" + className);
                    e.printStackTrace();} finally {if (fos != null) {fos.close();
                    }
                }
            }
        }
    }

我的项目应用

maven-plugin 我的项目执行 mvn install 装置到本地仓库

框架我的项目配置自定义 maven 插件进行打包,配置执行的申明周期为 complie(编译),这里 goal 自定义命令名称须要和 mojo 注解标注类中指定的 name 名称统一

          <plugin>
                <groupId>com.xxxxx</groupId>
                <artifactId>frame-maven-plugin</artifactId>
                <version>1.2.5</version>
                <executions>
                    <execution>
                        <goals>
                            <!-- 执行指标 -->
                            <goal>deepcompile</goal>
                        </goals>
                        <!-- 执行这个指标所在的生命周期 -->
                        <phase>compile</phase>
                    </execution>
                </executions>
            </plugin>
正文完
 0