基于Allatori7.6对Java代码实现代码混淆
第一步:下载Allatori7.6
第二步:解压Allatori-7.6-Demo.zip
解压下来就是这个样子,然后你可以用两种方式实现代码混淆
方式一:就是在你项目中打包的时候进行混洗代码
第一步:Allatori-7.6-Demo下的lib下面的jar包复制到你要打包的项目中去
第二步:配置在你项目下src/main/resources新建一个allatori.xml这个文件
<config>
<!--原编译的jar文件,混淆后输出的jar文件-->
<input>
<jar in="test-boot-module-system-1.0.0.jar" out="test-boot-module-system-1.0.0-obfuscated.jar"/>
</input>
<!-- 不替换类名、方法名、属性名 -->
<keep-names>
<class access="protected+">
<field access="private+"/>
<method access="protected+"/>
</class>
</keep-names>
<property name="local-variables-naming" value="keep-parameters"/>
<!-- 排除(不做代码混淆)-->
<ignore-classes>
<!-- 排除springboot依赖文件(springboot构建的项目需要排除,否则业务程序会报错) -->
<class template="class *springframework*"/>
<class template="class *apache*"/>
<class template="class *yaml*"/>
<class template="class *baomidou*"/>
<class template="class *alibaba*"/>
<class template="class *jeecg*"/>
<class template="class *codehaus*"/>
<class template="class *servlet*"/>
<class template="class *mail*"/>
<class template="class *tio*"/>
<class template="class *quartz*"/>
<class template="class *misc*"/>
<!-- <class template="class **"/> -->
<!-- 排除如下两个包下的类 -->
<class template="class com.csp.*.entity.*"/>
<class template="class com.csp.*.mapper.xml.*"/>
<class template="class org.jeecg.JeecgApplication.*"/>
<class template="class org.jeecg.modules.*"/>
<class template="class org.jeecg.config.*"/>
<class template="class cn.im.vim.*"/>
</ignore-classes>
<!-- <keep-names>-->
<!-- 防止部分类、方法、变量找不到名称而报错 -->
<!-- 所有方法名称不变,parameters="keep"表示方法参数名也不变 -->
<!-- <method template="*(**)" parameters="keep"/>-->
<!-- </keep-names>-->
<!-- 随机命名混淆字符-->
<!-- <property name="random-seed" value="abcdef ghnljk svi"/>-->
<property name="log-file" value="log.xml"/>
</config>
第三步:在你的项目中的pom.xml里面进行插件配置
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<!-- resouces拷贝文件插件 -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
<executions>
<!-- 执行这个插件的时候执行申明的所有phase -->
<execution>
<id>copy-and-filter-allatori-config</id>
<phase>package</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<!-- 这个地方需要注意拷贝的文件位置需要是target目录,target目录是最终打jar包存放的位置 -->
<outputDirectory>${basedir}/target</outputDirectory>
<resources>
<resource>
<!-- 这个地方的文件目录需要注意,网上很多目录都是${basedir}/allatori这个,所以才会需要手动拷贝allatori.xml文件到target目录下,有了这个mvn会自动帮我们拷贝了 -->
<directory>src/main/resources</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>
<!-- 指定引用的allatori的jar包位置,这里把jar包放在了根目录下的lib目录里 -->
<argument>${basedir}/lib/allatori.jar</argument>
<!-- 指定代码混淆时的配置文件,因为是混淆指定的是jar包,jar包位置在target下,所以我们的allatori.xml也需要拷贝到该目录下 -->
<argument>${basedir}/target/allatori.xml</argument>
</arguments>
</configuration>
</plugin>
</plugins>
</build>
第四步:就是使用maven插件进行打包,我这边使用第一种方式没有成功。
这里可以使用插件打包:->clean->install就可以打包成功,我看了很多博客上有些是这样打包
还有些是这样的在根目录下执行命令:mvn package -DskipTests
反正我是使用第一种方式打包的时候混淆代码没有成功。
第五步:如果出现这个样子基本上是成功了
第六步:如果你出现了这种情况那可能没有混淆成功
还报警告Some classes weren’t found. It may result in weaker obfuscation. Add needed jars to the ‘classpath’ element of the configuration file.
说明可能没有成功我这边推荐你使用第二种方式
方式二:就是在你打包好的jar包在进行混淆
第一步:就是在解压下的Allatori-7.6-Demo双击进入到
第二步:进入到这个路劲下
第三步:将你打包好的文件复制到Allatori-7.6-Demo\tutorial\step01\files
第四步:就是更改这个config.xml
<config>
<!--原编译的jar文件,混淆后输出的jar文件-->
<input>
<jar in="csp-boot-module-system-1.0.0.jar" out="csp-boot-module-system-1.0.0-obfuscated.jar"/>
</input>
<!-- 不替换类名、方法名、属性名 -->
<keep-names>
<class access="protected+">
<field access="private+"/>
<method access="protected+"/>
</class>
</keep-names>
<!-- <classpath basedir="library-jars">-->
<!-- <!– Adding library.jar to the classpath –>-->
<!-- <jar name="library.jar"/>-->
<!-- <!– Adding all jars in the lib directory to the classpath –>-->
<!-- <jar name="lib/*.jar"/>-->
<!-- <!– Adding all jars in the lib2 directory and its subdirectories to the classpath –>-->
<!-- <jar name="lib2/**/*.jar"/>-->
<!-- </classpath>-->
<!-- 接口形参名保持不变 -->
<property name="local-variables-naming" value="keep-parameters"/>
<!-- 排除(不做代码混淆)-->
<ignore-classes>
<!-- 排除springboot依赖文件(springboot构建的项目需要排除,否则业务程序会报错) -->
<class template="class *springframework*"/>
<class template="class *apache*"/>
<class template="class *yaml*"/>
<class template="class *baomidou*"/>
<class template="class *alibaba*"/>
<class template="class *jeecg*"/>
<class template="class *codehaus*"/>
<class template="class *servlet*"/>
<class template="class *mail*"/>
<class template="class *tio*"/>
<class template="class *quartz*"/>
<class template="class *misc*"/>
<!-- <class template="class **"/> -->
<!-- 排除如下两个包下的类 -->
<class template="class com.csp.*.entity.*"/>
<class template="class com.csp.*.mapper.xml.*"/>
<class template="class org.jeecg.JeecgApplication.*"/>
<class template="class org.jeecg.modules.*"/>
<class template="class org.jeecg.config.*"/>
<class template="class cn.im.vim.*"/>
<!-- <class template="class com.baomidou.mybatisplus.extension.service.impl.IService.*"/>-->
<!-- <class template="class com.baomidou.mybatisplus.extension.service.impl.ServiceImpl.*"/>-->
<!-- <class template="class com.baomidou.mybatisplus.core.mapper.BaseMapper.*"/>-->
<!-- <class template="class org.jeecg.common.system.base.controller.JeecgController.*"/>-->
<!-- <class template="class org.springframework.beans.factory.DisposableBean.*"/>-->
<!-- <class template="class org.springframework.context.ApplicationContextAware.*"/>-->
<!-- <class template="class javax.mail.Authenticator.*"/>-->
<!-- <class template="class org.jeecg.common.handler.IFillRuleHandler.*"/>-->
<!-- <class template="class org.apache.shiro.web.filter.authc.BasicHttpAuthenticationFilter.*"/>-->
<!-- <class template="class org.springframework.web.servlet.HandlerInterceptor.*"/>-->
<!-- <class template="class org.jeecg.common.system.base.entity.JeecgEntity.*"/>-->
<!-- <class template="class org.jeecgframework.poi.excel.imports.base.ImportFileServiceI.*"/>-->
<!-- <class template="class org.jeecg.common.system.base.service.JeecgService.*"/>-->
<!-- <class template="class org.jeecgframework.dict.service.AutoPoiDictServiceI.*"/>-->
<!-- <class template="class org.springframework.boot.CommandLineRunner.*"/>-->
<!-- <class template="class org.tio.websocket.server.WsServerAioListener.*"/>-->
<!-- <class template="class org.quartz.Job.*"/>-->
<!-- <class template="class org.springframework.boot.actuate.health.HealthIndicator.*"/>-->
<!-- <class template="class org.jeecg.common.system.base.service.impl.JeecgServiceImpl.*"/>-->
<!-- <class template="class org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory.*"/>-->
<!-- <class template="class org.apache.shiro.realm.AuthorizingRealm.*"/>-->
<!-- <class template="class org.apache.mina.core.service.IoHandlerAdapter.*"/>-->
<!-- <class template="class org.apache.shiro.authc.AuthenticationToken.*"/>-->
<!-- <class template="class org.jeecg.common.system.api.ISysBaseAPI.*"/>-->
<!-- <class template="class org.apache.shiro.web.filter.AccessControlFilter.*"/>-->
<!-- <class template="class org.springframework.web.servlet.config.annotation.WebMvcConfigurer.*"/>-->
<!-- <class template="class org.tio.websocket.server.handler.IWsMsgHandler.*"/>-->
<!-- <class template="class org.tio.core.stat.IpStatListener.*"/>-->
<!-- <class template="class org.springframework.cache.annotation.CachingConfigurerSupport.*"/>-->
<!-- <class template="class org.apache.ibatis.plugin.Interceptor.*"/>-->
</ignore-classes>
<!-- <keep-names>-->
<!-- 防止部分类、方法、变量找不到名称而报错 -->
<!-- 所有方法名称不变,parameters="keep"表示方法参数名也不变 -->
<!-- <method template="*(**)" parameters="keep"/>-->
<!-- </keep-names>-->
<!-- 随机命名混淆字符-->
<!-- <property name="random-seed" value="abcdef ghnljk svi"/>-->
<!--这个是你项目打包好后的一个lib包你要引入进来不然会引起找不到类然后导致代码混淆较弱-->
<classpath>
<jar name="lib/*.jar"/>
</classpath>
<property name="log-file" value="log.xml"/>
</config>
第四步:配置好后,Allatori-7.6-Demo\tutorial\step02\files双击运行RunAllatori.bat文件然后他会生成一个你那边配置输出的一个obfuscated.jar文件