1.介绍
Allatori是一个Java 混淆器,它属于第二代混淆器,因此它能够全方位地保护你的知识产权。 Allatori具有以下几种保护方式:命名混淆,流混淆,调试信息混淆,字符串混淆,以及水印技术。对于教育和非商业项目来说这个混淆器是免费的。支持war和jar文件格式,并且允许对需要混淆代码的应用程序添加有效日期。 有项目需要对代码进行保护,比较初级的方案就是对代码进行混淆,打包之后的文件进行反编译后,就可以看到效果。此外,使用Allatori打的包体积也会小一点。
2.下载并引入项目
使用的allatori版本是8.3的demo版本 进入官网allatori.com/ 下载jar包,解压获得如下两个jar包
在根目录新建目录lib,将上述jar包放入
3.进行混淆配置
在lib里新建文件allatori.xml,此文件用于配置allatori混淆情况。
在 ignore-classes标签 中配置忽略的包或者类,这些文件将不被混淆。
通常需要配置dao、entity、controller、config等文件;
使用了@Cacheable注解的需要将类配置在里面,防止缓存到redis里的数据被混淆
<config>
<input>
<!-- in表示输出的原始jar包,out表示输出的混淆后的jar包,后者名称可自定义,也可以是war -->
<jar in="unified-code-1.0-SNAPSHOT.jar" out="unified-code-1.0-SNAPSHOT-obfuscated.jar"/>
</input>
<keep-names>
<class access="protected+">
<field access="protected+"/>
<method access="protected+"/>
</class>
<!-- 视图层的方法参数名称不做混淆,否则传参会对应不上,不怕麻烦的也可以加@RequestParam指定入参名称 -->
<class template="class *Controller">
<method template="private+ *(**)" parameters="keep"/>
</class>
</keep-names>
<classpath>
<jar name="D:/work/2022/unifiedcode/unified-common/target/unified-common-1.0-SNAPSHOT.jar"/>
</classpath>
<property name="log-file" value="log.xml"/>
<!-- 忽略的包或类,这些文件将不被混淆 -->
<ignore-classes>
<class template="class *springframework*" />
<class template="class *shardingjdbc*" />
<class template="class *jni*" />
<class template="class *alibaba*"/>
<class template="class *persistence*"/>
<class template="class *mybatisplus*"/>
<class template="class *Interceptor*"/>
<!-- 排除业务类 -->
<class template="class com.bjdx.unified.*.handler.*" />
<class template="class com.bjdx.unified.*.config.*" />
<class template="class com.bjdx.unified.*.dao.*" />
<class template="class com.bjdx.unified.*.entity.*" />
<class template="class com.bjdx.unified.*.data.*" />
<class template="class com.bjdx.unified.*.vo.*" />
<class template="class com.bjdx.unified.*.interceptor.*" />
<class template="class com.bjdx.unified.*.feign.*" />
<class template="class com.bjdx.unified.code.service.impl.CodeRuleItemTakeServiceImpl" />
<class template="class com.bjdx.unified.code.service.impl.CodeRuleItemDicServiceImpl" />
<class template="class com.bjdx.unified.code.service.impl.CodeRuleItemTimeformatServiceImpl" />
<class template="class com.bjdx.unified.code.service.impl.CodeRuleItemTypeServiceImpl" />
<class template="class com.bjdx.unified.code.service.impl.CodeRuleServiceImpl" />
</ignore-classes>
<!-- 到期时间(到期后无法启动jar) 格式:yyyy/mm/dd-->
<!--<expiry date="2021/04/03" string="SERVICE EXPIRED!"/>-->
<!-- 随机命名混淆字符,默认用当前时间,每次打包混淆的类名、变量名都不一样,如果做了配置那么两次打包内容就一样-->
<!--<property name="random-seed" value="abcdef ghnljk svi"/>-->
</config>
4.pom文件引入依赖
<!-- allatori插件-->
<plugin>
<!-- resouces拷贝文件插件 -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
<executions>
<!-- 执行这个插件的时候执行申明的所有phase -->
<execution>
<id>copy-and-filter-allatori-config</id>
<phase>package</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<!-- 这个地方需要注意拷贝的文件位置需要是target目录,target目录是最终打jar包存放的位置 -->
<outputDirectory>${basedir}/target</outputDirectory>
<resources>
<resource>
<!-- 这个地方的文件目录需要注意,网上很多目录都是${basedir}/allatori这个,所以才会需要手动拷贝allatori.xml文件到target目录下,有了这个mvn会自动帮我们拷贝了 -->
<directory>lib</directory>
<includes>
<!-- 配置文件文件名 -->
<include>allatori.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<!-- 代码混淆打包插件 -->
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.0.0</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>
<!-- 指定引用的allatori的jar包位置,这里把jar包放在了根目录下的lib目录里 -->
<argument>lib/allatori.jar</argument>
<!-- 指定代码混淆时的配置文件,因为是混淆指定的是jar包,jar包位置在target下,所以我们的allatori.xml也需要拷贝到该目录下 -->
<argument>${basedir}/target/allatori.xml</argument>
</arguments>
</configuration>
</plugin>
<!-- allatori插件-->
5.打包
通过maven打包后获得两个jar包
Unified-code-1.0-SNAPSHOT-obfuscated.jar即为混淆后的jar包
注:打包时可能会出现如下问题
目前本人的解决方法是多次打包,最终将上述所缺的jar包打入。
暂未找到更好的方法,如果有人找到更好的方法请告知
6.通过jd-gui查看效果
进行反编译后代码: