java 代碼加密混淆之Allatori
全是干貨,僅供參考,不喜勿噴,有問題歡迎交流!若能幫助您之萬一,節(jié)省您工作中一點點時間,吾心甚慰。
關鍵字:java、springboot、idea、maven、allatori。
一、下載混淆組件
1、 下載最新版,官網https://allatori.com/
2、 本實例,使用包Allatori-9.5-Demo.zip解壓后目錄:
lib下混淆使用的jar組件,tutorial下是一些例子。

二、集成到idea maven環(huán)境
1、將lib文件夾拷貝到idea項目目錄(或者公共目錄)下,確保項目(模塊)找到。
2、在項目(模塊)根目錄(src平級)添加allatori.xml文件,可在tutorial例子中找到一個allatori.xml添加到項目中。
3、配置allatori.xml文件,此文件對哪些類混淆,怎么混淆做配置;本文章一個樣例allatori.xml文件(見代碼實例)。
官網幫助文檔:https://allatori.com/doc.html
關鍵配置說明:
Input 節(jié)點配置要混淆的jar文件,in為輸入jar文件,out為加密混淆后輸出jar文件(支持pom.xml中變量寫法)。
配置說明:對于protected 及以上的訪問級別的類、方法、屬性保持名稱不變。
!com.xxx.xxx.* 非項目包下的類不混淆。
其中
對于一個springboot項目此配置基本夠用,其他配置,保持默認即可。

4、maven配置pom.xml
其他不變,在build>plugins節(jié)點內maven打包節(jié)點plugin后添加兩個plugin節(jié)點
注意:若項目使用了${project.build.finalName}參數,在pom.xml中要配置。
若不是springboot項目,maven打包build節(jié)點可不配置,但

上圖說明:將allatori.xml文件從根目錄拷貝到target目錄。

上圖說明:
5、執(zhí)行打包混淆,mvn clean package。輸出文件為混淆后的jar包,在target目錄下。例子中混淆后的包為project-xxx-xxx-obfuscated.jar,原始包project-xxx-xxx.jar
6、若有更多要求,如特殊打包環(huán)境啟用混淆,可使用profiles方式,如添加一種prod環(huán)境,在打包時使用參數 prod,執(zhí)行命令maven clean package -Pprod;不帶有prod默認打包模式,不混淆。
若項目引用了其他加密混淆的項目,使用dependencies重新引用。

三、其他
1、擴展,上述打包如果子項目過多,可將加密混淆后的jar包,統一輸出到一個指定目錄。
2、不使用maven加密混淆,可參考下載包中的例子,直接執(zhí)行命令打包混淆。
四、代碼實例
以下為完整版代碼,若要求比較簡單請參考上面圖片中代碼。
1、allatori.xml 代碼實例
<config>
<!-- Maven properties could be used in Allatori configuration file. -->
<input>
<jar in="${obf.in.name}" out="${obf.out.name}"/>
</input>
<keep-names>
<class access="protected+">
<field access="protected+"/>
<method access="protected+"/>
</class>
<class template="class !com.xxx.xxx.*"/>
<class template="class com.xxx.xxx.controller.*">
<method access="protected+" parameters="keep"/>
</class>
</keep-names>
<ignore-classes>
<class template="class com.xxx.xxx.datascope.*.**"/>
</ignore-classes>
<!-- 水印 -->
<watermark key="secure-key-to-extract-watermark" value="@Copyright (c) by 2025-2099 Co.All Rights Reserved"/>
<!-- 隨機字符加密 -->
<property name="random-seed" value="任意字符串"/>
<!-- 字符串加密 -->
<property name="string-encryption" value="maximum"/>
<!-- fast,strong-->
<property name="string-encryption-type" value="fast"/>
<property name="string-encryption-version" value="v4"/>
<!-- 成員重新排序 -->
<property name="member-reorder" value="random"/>
<!-- <property name="string-encryption-ignored-strings" value="patterns.txt"/>-->
<!-- 廣泛流混淆 -->
<!-- Control flow obfuscation -->
<property name="control-flow-obfuscation" value="enable"/>
<property name="extensive-flow-obfuscation" value="normal"/>
<expiry date="2099/01/01" string="EXPIRED!"/>
<property name="log-file" value="log.xml"/>
</config>
2、pom.xml 代碼實例。
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>com.xxx</groupId>
<artifactId>xxx-modules</artifactId>
<version>0.0.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>xxx-modules-topic</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- 各種引用 -->
</dependencies>
<build>
<finalName>${project.artifactId}</finalName>
</build>
<profiles>
<profile>
<id>default</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version>
<configuration>
<includeSystemScope>true</includeSystemScope>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
<!--若本項目引用了其他混淆的項目,使用此配置,加密打包 maven clean package -Pprod-->
<profile>
<id>prod</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<properties>
<obf.dir>
obfuscated
</obf.dir>
<obf.in.name-pre>
${project.artifactId}-temp
</obf.in.name-pre>
<obf.in.name>
${obf.in.name-pre}.jar
</obf.in.name>
<obf.out.name>
${obf.dir}/${project.artifactId}-${project.version}.jar
</obf.out.name>
</properties>
<!--重新加載混淆的依賴-->
<dependencies>
<dependency>
<groupId>com.xxx</groupId>
<artifactId>xxx-xxxxx-xxx</artifactId>
<version>${project.version}</version>
<scope>system</scope>
<systemPath>
${basedir}/../xxx-common/xxx-common-module_1/target/${obf.dir}/xxx-common-module_1-${project.version}.jar
</systemPath>
</dependency>
</dependencies>
<build>
<finalName>${obf.in.name-pre}</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version>
<configuration>
<includeSystemScope>true</includeSystemScope>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- Copying Allatori configuration file to 'target' directory.
The destination file will be filtered (Maven properties used in configuration file will be resolved). -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.3</version>
<executions>
<execution>
<id>run-create-obf-dir</id>
<phase>package</phase>
<configuration>
<tasks>
<delete dir="${basedir}/target/${obf.dir}"/>
<mkdir dir="${basedir}/target/${obf.dir}"/>
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
<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}
</directory>
<includes>
<include>allatori.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</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>
<argument>${basedir}/../../lib/allatori.jar</argument>
<argument>${basedir}/target/allatori.xml</argument>
</arguments>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>

浙公網安備 33010602011771號