稷然如此

  • 首页
  • 文章分类
    • AI
    • Android
    • Java
    • Shell
    • Vue
    • C#
    • Python
    • 数据库
    • 组件
    • 其他
    • Game
  • 常用命令
    • Docker
    • Git
    • Linux
  • 操作系统
    • CentOS
    • Ubuntu
    • Windows
    • Kylin
  • 工具
    • IntelliJ IDEA
    • Visual Studio Code
稷然如此
不积跬步,无以至千里
  1. 首页
  2. 文章分类
  3. Java
  4. 正文

Spring Boot 代码混淆

2023年7月11日 1368点热度 0人点赞

1.下载

Allatori 混淆包
点击选项卡 DOWNLOAD 即可,下载下来的是含有 demo 工程的案例包,有两个目录  lib(混淆用的 jar 包)、tutorial(demo)

2.新建 lib 目录

在需要混淆代码的工程根目录下新建目录 lib(随意,啥名都可以),然后将下载下来的 lib 目录下两个文件(allatori.jar、allatori-annotations.jar)复制进去

3.新建规则配置

在 lib 目录下新建 allatori.xml 文件(名字也可以随意,后面项目 pom.xml 里可以修改),xml 内容:
<config>
    <!-- Maven properties could be used in Allatori configuration file. -->
    <input>
        <!-- in 是需要混淆的 jar 包,out 是混淆后输出的 jar 包,可以先 package 下查看 target 目录 jar 包名 -->
        <jar in="akim-cloud-framework-1.2.0-SNAPSHOT.jar" out="akim-cloud-framework.jar"/>
    </input>
    <!-- 指定混淆 -->
    <keep-names>
        <!-- 对字段、方法进行混淆 -->
        <class access="protected+">
            <field access="protected+"/>
            <method access="protected+"/>
        </class>
        <!-- 控制器保持参数名不参与混淆 -->
        <class template="class *Controller">
            <method template="private+ *(**)" parameters="keep"/>
        </class>
    </keep-names>
    <!-- 不需要被混淆的类 -->
    <ignore-classes>
        <!-- springframework 框架、jni 框架、alibaba 框架,一般项目基本上都是围绕这三套框架构建的 -->
        <class template="class *springframework*" />
        <class template="class *jni*" />
        <class template="class *alibaba*"/>
        <!-- 以下是项目引用到的三方组件库 -->
        <class template="class *lombok*"/>
        <class template="class *xingyuv*"/>
        <class template="class *fasterxml*"/>
        <class template="class *hutool*"/>
        <class template="class *google*"/>
        <class template="class org.apache.*"/>
        <class template="class feign.*"/>
        <class template="class io.minio.*"/>
        <class template="class *lionsoul*"/>
        <class template="class *swagger*"/>
        <class template="class *aspectj*"/>
        <class template="class *micrometer*"/>
        <class template="class *opentracing*"/>
        <class template="class *redisson*"/>
        <class template="class com.baomidou.*"/>
        <class template="class org.mybatis.*"/>
        <class template="class *yulichang*"/>
        <class template="class *jsqlparser*"/>
        <class template="class *aopalliance*"/>
        <class template="class *aliyuncs*"/>
        <class template="class *tencentcloudapi*"/>
        <class template="class *xkcoding*"/>
        <class template="class *zhyd*"/>
        <class template="class *springdoc*"/>
        <!-- 以下是项目本身组件 -->
        <!-- 如果混淆了 BaseDO 会导致 mybatis 执行时带进去的基类字段变成 a、b、c 这种 -->
        <class template="class com.akim.cloud.framework.common.model.dbo.BaseDO"/>
        <!-- 如果混淆了 CommonResult、PageResult 等会导致接口输出内容变成 a、b、c 这种 -->
        <class template="class com.akim.cloud.framework.common.pojo.*"/>
        <!-- 如果混淆了枚举会导致调用时都是 a、b、c 这种 -->
        <class template="class com.akim.cloud.framework.common.enums.*"/>
        <!-- 所有的 config 注入类均不需要混淆 -->
        <class template="class com.akim.cloud.framework.*.config.*"/>
        <class template="class com.akim.cloud.framework.logger.operatelog.config.*"/>
        <class template="class com.akim.cloud.framework.web.common.*.config.*"/>
        <!-- aop 切面混淆会导致加载不了注解 -->
        <class template="class com.akim.cloud.framework.logger.operatelog.core.aop.OperateLogAspect"/>
        <class template="class com.akim.cloud.framework.security.core.aop.PreAuthenticatedAspect"/>
    </ignore-classes>
    <property name="log-file" value="log.xml"/>

    <!-- 到期时间(到期后无法启动jar) 格式:yyyy/mm/dd-->
    <!--<expiry date="2025/12/31" string="SERVICE EXPIRED!"/>-->
    <!-- 随机命名混淆字符,默认用当前时间,每次打包混淆的类名、变量名都不一样,如果做了配置那么两次打包内容就一样-->
    <!--<property name="random-seed" value="abcdef ghnljk svi"/>-->
</config>

4.打包插件

项目根 pom.xml 文件新增或者替换原来打包插件内容,内容如下:
<build>
        <plugins>
            <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}/lib</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}/lib/allatori.xml</argument>
                    </arguments>
                </configuration>
            </plugin>
        </plugins>
    </build>

5.开始混淆

先 package 生成原始需要混淆的 jar 包,这个过程最后会报错,但不影响生成原始 jar 包,然后把这个原始 jar 包复制到新建的 lib 目录下,再次执行 package 打包,最后就会在 lib 目录下生成混淆后的 jar 包 akim-cloud-framework.jar。

6.注意事项

混淆后的代码需要多测试,大概率会埋坑的,比如:
1.输出的基类、请求参数类等,如果没有做好忽略混淆配置,就会出现比如项目中的基类 BaseDO 的 create_user_id、create_time、update_user_id、update_time等字段被混淆,导致继承了该基类的实体类在执行 sql 时代入的字段名为混淆过后的。CommonResult 输出的 code、msg、data 字段也是混淆过后的,不符合输出预期。PageResult 同 CommonResult 一个道理。PageParmas 会导致前端请求入参也匹配不上。
有时间再多研究研究,比如:
1.为什么打包插件不直接找到 target 目录的原始 jar 包进行混淆,还需要复制到 lib 目录下,配置了也没用。
2.工具包里的 allatori-annotations.jar 这个是干什么用的,是不是就是为了解决 AOP 切面注解无法加载的问题。
标签: Spring Cloud Yudao Cloud
最后更新:2025年9月23日

Akim

犇 骉 Java、C#、Python、Go、Android、MiniProgram、Bootstrap、Vue2

点赞
< 上一篇
下一篇 >
文章目录
  • 1.下载
  • 2.新建 lib 目录
  • 3.新建规则配置
  • 4.打包插件
  • 5.开始混淆
  • 6.注意事项

Copyright © 2025 aianran.com All Rights Reserved.

免责申明 | 隐私政策 | 服务条款 | 关于我们

黔ICP备2023008200号-1

贵公网安备 52010202003594号