java混淆代码

1.添加maven依赖

<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>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>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>allatori/lib/allatori.jar</argument>
	    <argument>${basedir}/target/allatori.xml</argument>
	  </arguments>
	</configuration>
</plugin>

2.添加配置文件与依赖包

其中allatori.xml

<config>
    <input>
        <jar in="原始包.jar"  out="原始包-obfuscated.jar"/>
    </input>

    <keep-names>
        <class access="protected+">
            <field access="private+"/>
            <method access="protected+"/>
        </class>
        <!-- 所有方法名保持不变,参数也不变 -->
        <method template="*(**)" parameters="keep"/>
    </keep-names>

    <property name="log-file" value="log.xml"/>
    <!--
    <property name="string-encryption" value="enable" apply2class="class com.linkingthing"/>
    -->
    <!-- 接口形参名保持不变 -->
    <!--
    <property name="local-variables-naming" value="keep-parameters"/>
    -->
    <ignore-classes>
        <!-- 配置启动类不被混淆  保证springBoot可以正常启动 -->
        <class template="class *Application*"/>
        <class template="class *springframework*"/>        
        <class template="class com.xx.domain.*"/>        
        <class template="class org.apache.*"/>
        <class template="class *mybatisplus.*"/>
        <!--防止mybatis出现没有@Param取不到参数的情况-->
        <class template="class *mapper.*"/>

    </ignore-classes>
    <!--这个是你项目打包好后的一个lib包你要引入进来不然会引起找不到类然后导致代码混淆较弱-->
    <classpath>
        <jar name="lib/*.jar"/>
    </classpath>
</config>
jar下载地址: https://allatori.com/downloads/Allatori-8.5-Demo.zip


3.操作步骤

mvn clean
mvn dependency:copy-dependencies -DoutputDirectory=target/lib
mvn  package  -X -Dmaven.test.skip=true
mv target/原始.jar target/原始-bak.jar
mv target/原始-obfuscated.jar target/原始.jar

#验证可用性
java -jar  target/原始.jar
调用mybatis的方法


4.优化

第3步是需要手工操作的,怎么实现自动化呢?主要是jar名需要带上maven里配置的版本号,并且最好能一句maven package就能完成,那么把这些命令弄成maven配置文件吧。


4.1配置maven在package阶段生成依赖的lib文件夹

<plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-dependency-plugin</artifactId>
	<executions>
	  <execution>
	    <id>copy-dependencies</id>
	    <phase>package</phase>
	    <goals>
	      <goal>copy-dependencies</goal>
	    </goals>
	    <configuration>
	      <type>jar</type>
	      <includeTypes>jar</includeTypes>
	      <outputDirectory>
	        ${project.build.directory}/lib
	      </outputDirectory>
	    </configuration>
	  </execution>
	</executions>
</plugin>

4.2 配置改jar文件名。因为allatori.xml里面不能配置带变量的jar包名,所以这里把jar包名改成固定的。

<plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-antrun-plugin</artifactId>
	<executions>
	  <execution>
	    <id>copy1</id>
	    <phase>package</phase>
	    <configuration>
	      <target>
	        <move file="target/${project.build.finalName}.jar" tofile="target/webinfo.jar"></move>
	      </target>
	    </configuration>
	    <goals>
	      <goal>run</goal>
	    </goals>
	  </execution>
	</executions>
</plugin>

4.3 修改上文中的exec-maven-plugin让之前的一个步骤变为2两步骤,第2个步骤是将jar包变改回来,改成带版本号的。

pom.xml,所有的依赖:

 <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <executions>
        <execution>
            <id>copy-dependencies</id>
            <phase>package</phase>
            <goals>
                <goal>copy-dependencies</goal>
            </goals>
            <configuration>
                <type>jar</type>
                <includeTypes>jar</includeTypes>
                <outputDirectory>
                    ${project.build.directory}/lib
                </outputDirectory>
            </configuration>
        </execution>
    </executions>
</plugin>
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-antrun-plugin</artifactId>
    <executions>
        <execution>
            <id>copy1</id>
            <phase>package</phase>
            <configuration>
                <target>
                    <move file="target/${project.build.finalName}.jar" tofile="target/webinfo.jar"></move>
                </target>
            </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>allatori</directory>
                        <includes>
                            <include>allatori.xml</include>
                        </includes>
                        <filtering>true</filtering>
                    </resource>
                </resources>
            </configuration>
        </execution>
    </executions>
</plugin>


文/程忠 浏览次数:0次   2023-05-04 20:08:27

相关阅读


评论:
点击刷新

↓ 广告开始-头部带绿为生活 ↓
↑ 广告结束-尾部支持多点击 ↑