背景
公司开发框架减少了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>