1.首先到官网下载混淆所需要的jar包:allatori.jar

官网地址:http://www.allatori.com

2.在项目的pom文件中添加混淆的插件

<build>
        <plugins>
            <!--混淆配置开始-->
            <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>${settings.localRepository}\com\cs\allatori\allatori\1.0\allatori.jar</argument>
                        <argument>${basedir}/src/main/resources/allatori.xml</argument>
                    </arguments>
                </configuration>
            </plugin>
            <!--混淆配置结束-->
        </plugins>
    </build>

3.如果混淆工具包是放在项目目录中,就在项目的根目录下新建“lib”目录并放入混淆工具包

springboot代码混淆技术 springboot 混淆_maven


4.在项目的resource目录下添加allatori.xml,(也可以放在lib目录下,只要pom文件引用的allatori.xml路径写对即可)并编写以下内容:

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <input>
        <!-- in:要混淆的jar包目录,out:为混淆后输出的jar包目录 -->
        <jar in="../../../target/plat-iop-daas-core-3.1.0-SNAPSHOT.jar" out="../../../../../jar/plat-iop-daas-core-3.1.0-SNAPSHOT.jar"/>
    </input>
    <keep-names>
        <!-- 由于微服务会有模块依赖的调用,使用类名和方法名不混淆 -->
        <class access="protected+">
            <!--<field access="protected+"/>-->
            <method access="protected+"/>
        </class>
    </keep-names>
    <!-- 混淆映射日志文件 -->
    <property name="log-file" value="log.xml"/>
    <!-- 启用字符串加密(类型和版本) -->
    <property name="string-encryption" value="enable"/>
    <property name="string-encryption-type" value="fast"/>
    <property name="string-encryption-version" value="v4"/>
    <!-- 包名保持不变 -->
    <property name="packages-naming" value="keep"/>
    <!--<property name="classes-naming" value="unique"/>
    <property name="classes-naming" value="123"/>-->
    <!--<property name="methods-naming" value="unique"/>
    <property name="methods-naming" value="abc"/>-->
    <property name="fields-naming" value="123"/>
    <!-- 接口形参名保持不变 -->
    <property name="local-variables-naming" value="keep-parameters"/>

    <!-- 排除不需要混淆的类(出错的类) -->
    <ignore-classes>
        <!-- 排除启动类 -->
        <!--<class template="class com.iop.PlatIopAuthApplication"/>-->
        <!--排除springboot依赖文件(springboot构建的项目需要排除,否则业务程序会报错)-->
        <class template="class org.springframework.boot.*"/>
        <!-- 排除不需要混淆的模块配置类、实体类、结果类…… -->
        <class template="class com.iop.daas.config.*"/>
        <class template="class com.iop.daas.aspect.*"/>
        <class template="class com.iop.daas.dto.*"/>
    </ignore-classes>
</config>

5.混淆后jar包输出的目录问题:如果对jar包名字有要求(不能变)就可以在该项目的工作空间中单独新建一个“jar”目录,然后把所有模块混淆后的jar包都输出到这个目录;如果对jar包名字有要求的话可以把混淆后的jar包输出到对应模块的target目录下,但名字不能与之前jar名字一致

6.clean一下项目再打包,在对应的输出目录中即可出现混淆后的jar包。

7.如果打包过程中出现下面情况

springboot代码混淆技术 springboot 混淆_jar_02


没有报error错误,即打包成功!

8.如果是需要把混淆的jar包作为依赖放入要运行的jar包或者war包,可以使用bandzip压缩软件先打开要运行的jar包或者war包,然后把混淆后的jar包添加到依赖的lib目录中,这里要注意添加的压缩级别是:0-仅储存

9.如果在打包过程中或运行过程中出现错误(类找不到……),就要到allatori.xml配置文件中的标签中添加相应报错文件的排除