在工作中,我们写的代码需要开源和需要给团队中的人使用,为了防止代码被轻松反编译,可以通过混淆工具,降低代码的可读性。
前言
本文主要讲解allatori在Java maven工程中的使用方法。
一、Allatori介绍
官方地址: http://www.allatori.com
-
Allatori 是第二代 Java 混淆器,可为您的知识产权提供全方位的保护。
-
尽管大多数第二代混淆器都提供了有价值的保护,但在 Allatori 中开发了许多附加功能,以使逆向工程代码几乎是不可能的。
-
Allatori 不只是混淆,它还最小化了应用程序的大小,并提高了速度。Allatori,作为每个现代 Java 混淆器,具有完整的水印功能,允许软件获得适当的许可。
二、使用步骤
1.引入jar和配置文件
从官网上下载最新的jar,在工程中创建文件夹lib和allatori。
2.配置allatori文件
allatori.xml内容如下:
<config>
<input>
<!--in表示需要混淆的jar包, out表示混淆后的jar包, 名称相同时会覆盖原来的jar包-->
<!-- <jar in="test.jar" out="obf-test.jar"/>-->
<jar in="common-util.jar" out="allatori_common-util.jar"/>
</input>
<keep-names>
<class template="class * instanceof java.io.Serializable"/>
<!--设置混淆级别,protected+ 表示访问权限在protected(包含)之上的名称不会被混淆-->
<class access="protected+">
<field access="protected+"/>
<method access="protected+"/>
</class>
</keep-names>
<!--用于排除某些不需要混淆的类-->
<ignore-classes>
<!-- 以com.code1开头的类不进行混淆 -->
<!-- <class template="class regex:^(?!com.code1).*"/>-->
<!-- <class template="class org.springframework.*"/>-->
</ignore-classes>
<!--名称混淆方式-->
<!-- <property name="classes-naming" value="iii"/>-->
<!-- <property name="methods-naming" value="iii"/>-->
<property name="fields-naming" value="abc"/>
<property name="line-numbers" value="keep"/>
<property name="log-file" value="log.xml"/>
<!-- 用于为混淆的应用程序设置类路径-->
<!--<classpath basedir="../lib">-->
<!-- Adding library.jar to the classpath -->
<!-- Adding all jars in the lib directory to the classpath -->
<!--<jar name="*.jar"/>-->
<!-- Adding all jars in the lib2 directory and its subdirectories to the classpath -->
<!--</classpath>-->
</config>
标签说明文档:https://allatori.com/doc.html
3.配置pom.xml
<build>
<finalName>common-util</finalName>
<plugins>
<!-- Allatori plugin start -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
<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.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>
<argument>${basedir}/lib/allatori.jar</argument>
<argument>${basedir}/target/allatori.xml</argument>
</arguments>
</configuration>
</plugin>
</plugins>
</build>