SpringBoot自定義maven-plugin插件整合asm代碼插樁
背景
公司開發(fā)框架增加了web系統(tǒng)license授權(quán)證書校驗(yàn)?zāi)K,實(shí)行一臺(tái)機(jī)器一個(gè)授權(quán)證書,初步方案是增加攔截器針對(duì)全局請(qǐng)求進(jìn)行攔截校驗(yàn),評(píng)估后認(rèn)為校驗(yàn)方式單一,應(yīng)該增加重要工具類,業(yè)務(wù)service實(shí)現(xiàn)中每個(gè)方法的進(jìn)行校驗(yàn),因?yàn)樯婕按a量較大硬編碼工作困難,故選擇通過自定義maven插件在編譯期間進(jìn)行動(dòng)態(tài)代碼插樁操作
項(xiàng)目配置
新建maven項(xiàng)目設(shè)置打包方式
<packaging>maven-plugin</packaging>
增加依賴項(xiàng)
?? ??? ? <!--使用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內(nèi)容配置
?? ? <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>
編譯攔截
創(chuàng)建編譯操作類FramePlugin,繼承AbstractMojo并使用Mojo注解標(biāo)注,output參數(shù)是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方法針對(duì)方法內(nèi)部字節(jié)碼進(jìn)行自定義操作,這里是使用框架內(nèi)部封裝好的一個(gè)靜態(tài)方法來校驗(yàn)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中進(jìn)行文件遞歸查找調(diào)用,就是將已經(jīng)編譯的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();
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }
? ? }項(xiàng)目使用
maven-plugin項(xiàng)目執(zhí)行mvn install安裝到本地倉庫
框架項(xiàng)目配置自定義maven插件進(jìn)行打包,配置執(zhí)行的聲明周期為complie(編譯),這里goal自定義命令名稱需要和mojo注解標(biāo)注類中指定的name名稱一致
??? ??? ? <plugin> ? ? ? ? ? ? ? ? <groupId>com.xxxxx</groupId> ? ? ? ? ? ? ? ? <artifactId>frame-maven-plugin</artifactId> ? ? ? ? ? ? ? ? <version>1.2.5</version> ? ? ? ? ? ? ? ? <executions> ? ? ? ? ? ? ? ? ? ? <execution> ? ? ? ? ? ? ? ? ? ? ? ? <goals> ? ? ? ? ? ? ? ? ? ? ? ? ? ? <!-- 執(zhí)行目標(biāo) --> ? ? ? ? ? ? ? ? ? ? ? ? ? ? <goal>deepcompile</goal> ? ? ? ? ? ? ? ? ? ? ? ? </goals> ? ? ? ? ? ? ? ? ? ? ? ? <!-- 執(zhí)行這個(gè)目標(biāo)所在的生命周期 --> ? ? ? ? ? ? ? ? ? ? ? ? <phase>compile</phase> ? ? ? ? ? ? ? ? ? ? </execution> ? ? ? ? ? ? ? ? </executions> ? ? ? ? ? ? </plugin>
到此這篇關(guān)于SpringBoot自定義maven-plugin插件整合asm代碼插樁的文章就介紹到這了,更多相關(guān)maven-plugin asm代碼插樁內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring整合redis(jedis)實(shí)現(xiàn)Session共享的過程
這篇文章主要介紹了Spring整合redis(jedis)實(shí)現(xiàn)Session共享,需要的朋友可以參考下2018-06-06
java實(shí)現(xiàn)計(jì)算器加法小程序(圖形化界面)
這篇文章主要介紹了Java實(shí)現(xiàn)圖形化界面的計(jì)算器加法小程序,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-05-05
Spring?MVC?請(qǐng)求映射路徑的配置實(shí)現(xiàn)前后端交互
在Spring?MVC中,請(qǐng)求映射路徑是指與特定的請(qǐng)求處理方法關(guān)聯(lián)的URL路徑,這篇文章主要介紹了Spring?MVC?請(qǐng)求映射路徑的配置,實(shí)現(xiàn)前后端交互,需要的朋友可以參考下2023-09-09
深入淺析Spring Security5中默認(rèn)密碼編碼器
這篇文章主要介紹了Spring Security5中默認(rèn)密碼編碼器,非常不錯(cuò),具有一定的參考借鑒價(jià)值 ,需要的朋友可以參考下2019-05-05
在Android系統(tǒng)中使用WebViewClient處理跳轉(zhuǎn)URL的方法
這篇文章主要介紹了在Android系統(tǒng)中使用WebViewClient處理跳轉(zhuǎn)URL的方法,實(shí)現(xiàn)代碼為Java語言編寫,是需要的朋友可以參考下2015-07-07
淺談Java數(shù)據(jù)結(jié)構(gòu)之稀疏數(shù)組知識(shí)總結(jié)
今天帶大家了解一下Java稀疏數(shù)組的相關(guān)知識(shí),文中有非常詳細(xì)的介紹及代碼示例,對(duì)正在學(xué)習(xí)java的小伙伴們有很好地幫助,需要的朋友可以參考下2021-05-05
Java ArrayList的基本概念和作用及動(dòng)態(tài)數(shù)組的機(jī)制與性能
在Java中,ArrayList是一個(gè)實(shí)現(xiàn)了List接口的動(dòng)態(tài)數(shù)組,它可以根據(jù)需要自動(dòng)增加大小,因此可以存儲(chǔ)任意數(shù)量的元素,這篇文章主要介紹了探秘Java ArrayList的基本概念和作用及動(dòng)態(tài)數(shù)組的機(jī)制與性能,需要的朋友可以參考下2023-12-12
Spring Boot實(shí)戰(zhàn)之?dāng)?shù)據(jù)庫操作的示例代碼
本篇文章主要介紹了Spring Boot實(shí)戰(zhàn)之?dāng)?shù)據(jù)庫操作的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-01-01
快速學(xué)會(huì)Dubbo的配置環(huán)境及相關(guān)配置
本文主要講解Dubbo的環(huán)境與配置,文中運(yùn)用大量代碼和圖片講解的非常詳細(xì),需要學(xué)習(xí)或用到相關(guān)知識(shí)的小伙伴可以參考這篇文章2021-09-09

