一、说明
1、众所周知,jar可以通过工具进行反编译
2、若自己实现的算法想要保护起来,又不影响jar的功能调用,可尝试本文描述
二、解决方案
1、直奔工具: Allatori代码混淆
2、常用maven构造项目,举例就用maven了;其他的可参考官网示例,通俗易懂
PS:下载好示例
3、引入需要的jar
<dependency>
<groupId>allatori</groupId>
<artifactId>allatori</artifactId>
<version>1.0.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/resources/libs/allatori.jar</systemPath>
</dependency>
<dependency>
<groupId>allatori</groupId>
<artifactId>allatori-annotations</artifactId>
<version>1.0.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/resources/libs/allatori-annotations-1.0.0.jar</systemPath>
</dependency>
4、引入pom.xml引入插件
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
<configuration>
<encoding>UTF-8</encoding>
<includeSystemScope>true</includeSystemScope>
</configuration>
<executions>
<execution>
<id>copy-and-filter-allatori-config</id>
<phase>package</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/target</outputDirectory>
<resources>
<resource>
<directory>${basedir}/allatori</directory>
<includes>
<include>allatori.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<!-- Running Allatori -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<executions>
<execution>
<id>run-allatori</id>
<phase>package</phase>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
<configuration>
<executable>java</executable>
<arguments>
<argument>-Xms128m</argument>
<argument>-Xmx512m</argument>
<argument>-jar</argument>
<!-- Copy allatori.jar to 'allatori' directory to use the commented line -->
<!--<argument>${basedir}/../../../../lib/allatori.jar</argument>-->
<!-- <argument>${basedir}/allatori/allatori.jar</argument> -->
<argument>${project.basedir}/src/main/resources/libs/allatori.jar</argument>
<argument>${project.basedir}/target/allatori.xml</argument>
</arguments>
</configuration>
</plugin>
</plugins>
5、复制配置文件(从示例:Allatori-7.7-Demo\tutorial\step12-maven\files\mouse-gestures)
6、maven打包即可
三、第二种方案
1、方案说明
1)此方案依赖新版的allatori,8.3版本及以上版本
2)管网下载
https://allatori.com/
3)大致步骤: 正常maven打包 -> 配置allatori -> 编写脚本 -> 执行脚本
2、操作步骤
1)解压allatori
2)复制config到lib
3)编辑config.xml
<config> <input> <!--放到项目里面,用相对路径--> <!--项目跟路径新建: allatori目录,将jar跟xml配置都放目录下--> <jar in="../admin/target/hg-admin.jar" out="../admin/target/obf-hg-admin.jar"/> </input> <keep-names> <!--指明要混淆的包--> <!--SpringBoot启动类,需要排除混淆,不然java -jar会失败--> <class access="protected+"> <field access="protected+"/> <method access="protected+"/> </class> <!-- 防止部分类、方法、变量找不到名称而报错 --> <!-- 所有方法名称不变,parameters="keep"表示方法参数名也不变 --> <method template="*(**)" parameters="keep"/> <!-- com.a.b.c中的类以及其子包中的类的名称不变 --> <class template="class com.hg.admin.*"/> </keep-names> <property name="log-file" value="log.xml"/> </config>
说明1: 如果有其他三方依赖,都需要排除掉
说明2: 需要根据需求执行混淆的包
说明3: 忽略某些包
<ignore-classes> <!-- 排除springboot依赖文件(springboot构建的项目需要排除,否则业务程序会报错) --> <!--<class template="class **" />--> <class template="class org**"/> <class template="class com.querydsl**"/> <class template="class cn.**"/> <class template="class *springframework*"/> <class template="class *apache*"/> <class template="class *yaml*"/> <class template="class *baomidou*"/> <class template="class *alibaba*"/> <class template="class *jeecg*"/> <class template="class *codehaus*"/> <class template="class *servlet*"/> <class template="class *mail*"/> <class template="class *tio*"/> <class template="class *quartz*"/> <class template="class *misc*"/> <!-- <class template="class **"/> --> <!-- 排除如下两个包下的类 --> <class template="class com.csp.*.entity.*"/> <class template="class com.csp.*.mapper.xml.*"/> <class template="class org.jeecg.JeecgApplication.*"/> <class template="class org.jeecg.modules.*"/> <class template="class org.jeecg.config.*"/> <class template="class cn.im.vim.*"/> </ignore-classes>
说明1: 与<keep-names>同级
4)编写脚本
%1、正常maven打包(mvn clean package% %2、打开terminal% %3、进入到allatori目录% %4、执行decodeJar.cmd% %需要在allatori路径执行% java -jar allatori.jar config.xml
~~