基于ProGuard-Maven-Plugin的自定义代码混淆插件

本文介绍了如何在Maven项目中使用基于ProGuard的自定义代码混淆插件,该插件支持混淆jar和war包。作者分享了修改后的项目地址,并详细讲解了插件的使用方法以及ProGuard的配置参数,旨在保护敏感代码,防止反编译。

介绍

大家可能都会碰到一些代码比较敏感的项目场景,这个时候代码被反编译看到就不好了,这个时候就需要代码混淆插件来对代码进行混淆了。

基于Maven的项目一般会去考虑使用proguard-maven-plugin,但是这个插件仅支持打Jar包不支持打War包。

于是我用空闲时间在proguard-maven-plugin的基础上修改了里面的一部分逻辑,可以在项目构建过的时候把代码混淆,支持打成jar包和war包。 

现在贴出来给大家看看。

项目地址

https://github.com/lovethegirl/code-hidding-plugin


修改部分

War包压缩解压的工具类

package com.github.wvengen.maven.proguard;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Iterator;

import org.apache.commons.compress.archivers.ArchiveException;
import org.apache.commons.compress.archivers.ArchiveInputStream;
import org.apache.commons.compress.archivers.ArchiveOutputStream;
import org.apache.commons.compress.archivers.ArchiveStreamFactory;
import org.apache.commons.compress.archivers.jar.JarArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.utils.IOUtils;
import org.apache.commons.io.FileUtils;

/**
 * 处理WAR文件工具类。可压缩或解压缩WAR文件。
 * 
 * @author Xiong Shuhong(shelltea@gmail.com)
 */
public class WarUtils {
    public static void unzip(String warPath, String unzipPath) {
        File warFile = new File(warPath);
        try {
            BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(
                warFile));
            ArchiveInputStream in = new ArchiveStreamFactory().createArchiveInputStream(
                ArchiveStreamFactory.JAR, bufferedInputStream);

            JarArchiveEntry entry = null;
            while ((entry = (JarArchiveEntry) in.getNextEntry()) != null) {
                File file = new File(unzipPath, entry.getName());
                if (file.exists()) {
                    file.delete();
                }
                if (entry.isDirectory()) {
                    file.mkdir();
                } else {
                    OutputStream out = FileUtils.openOutputStream(file);
                    IOUtils.copy(in, out);
                    out.close();
                }
            }
            in.close();
        
淘宝花钱买的最新版!需要的拿去! This asset obfuscates your code to make it harder for bad guys to reverse engineer your projects. Specifically designed for Unity, it seamlessly links in with its build process. The top priority of this package is to work straight out of the box with no extra steps required. While other obfuscators can stop a game from working, Beebyte's obfuscator looks for specific Unity related code that must be protected. The contents of your source files are unchanged, the obfuscation targets the compiled assembly. Features: - Supports IL2CPP - Supports Assembly Definition Files (Unity 2017.3+) - Removes Namespaces without any conflicts - Recognises Unity related code that must not be changed - Renames Classes (including MonoBehaviours) - Renames Methods - Renames Parameters - Renames Fields - Renames Properties - Renames Events - String literal obfuscation - Adds fake methods - Easy and extensive customisation using the Unity inspector window - Consistent name translations are possible across multiple builds and developers - Semantically secure cryptographic naming convention for renamed members The asset works for both Unity Free and Unity Pro version 4.2.0 onwards (including Unity 5 & 2017 & 2018). Build targets include Standalone, Android, iOS, WebGL, UWP. Other platforms are not guaranteed or supported but may become supported at a future date. IL2CPP builds are much harder to reverse engineer but strings and member information (class, method names etc) are visible in the global-metadata.dat file. Obfuscation will apply to this file adding further security. Why not complement your security with the Anti-Cheat Toolkit - a great third party asset. For more information about the Obfuscator, please see the FAQ
<think>好的,我现在需要帮助用户在Java 17和Spring Boot 3.2.12项目中配置并使用ProGuard-Maven-Plugin实现自定义代码混淆。首先,我需要回忆一下ProGuard-Maven-Plugin的基本用法以及它在Java项目中的配置步骤。用户提到的是Java 17和Spring Boot 3.2.12,所以需要注意兼容性问题。 首先,ProGuard是一个用于代码优化、混淆和预校验的工具,通常用于减少应用大小和增加反编译难度。Maven插件需要正确配置在pom.xml文件中。但用户可能遇到版本兼容性问题,特别是Java 17的新特性,比如模块化系统或者新的语言特性是否会影响ProGuard的处理。 然后,查看用户提供的引用内容,引用[1]提到了配置ProGuard-Maven-Plugin的基本配置器,而引用[2]讨论了ClassFinal插件的配置,这可能对用户有参考价值,但用户明确希望使用ProGuard。需要注意,用户可能之前考虑过其他混淆工具,但最终选择了ProGuard,因此需要确保提供的解决方案与他们的需求相符。 接下来,我需要确定如何在Maven的pom.xml中添加ProGuard插件。通常,这涉及到在<build><plugins>部分添加插件配置。用户可能需要指定依赖项、混淆规则、保留的类等。此外,Java 17可能要求特定的ProGuard版本或配置参数,比如处理模块化或新的API。 另外,Spring Boot项目通常生成可执行的JAR,这可能会影响混淆步骤,因为主类需要在MANIFEST.MF中被正确识别。混淆后,需要确保Spring Boot的启动加载机制仍然有效,特别是如果类名或方法名被更改的话。可能需要配置ProGuard保留特定的注解或类,比如@SpringBootApplication或main方法。 可能还需要处理依赖库的问题,比如Spring Boot的库是否需要被排除在混淆之外,或者如何处理第三方库的引用。此外,混淆过程中可能会遇到常见的错误,比如缺少类定义或反射调用的问题,需要配置ProGuard规则来保留这些部分。 最后,生成相关问题时,应该围绕ProGuard的配置、优化、常见问题以及与其他工具的比较展开。例如,如何测试混淆后的应用,如何处理运行时错误,或者与ClassFinal等其他工具的区别。 总结步骤可能包括:添加插件依赖,配置插件执行阶段,定义混淆选项,处理Java 17的兼容性,保留Spring Boot必要的类和资源,以及构建和测试混淆后的JAR。需要确保所有步骤都适用于最新的Spring Boot版本和Java 17环境,避免遗漏关键配置导致构建失败或运行时错误。</think>在Java 17和Spring Boot 3.2.12项目中配置ProGuard-Maven-Plugin实现代码混淆,需按照以下步骤操作: ### 一、基础配置 1. **添加插件到pom.xml** ```xml <build> <plugins> <plugin> <groupId>com.github.wvengen</groupId> <artifactId>proguard-maven-plugin</artifactId> <version>2.6.1</version> <executions> <execution> <phase>package</phase> <goals><goal>proguard</goal></goals> </execution> </executions> <configuration> <obfuscate>true</obfuscate> <injar>${project.build.finalName}.jar</injar> <outjar>${project.build.finalName}-obfuscated.jar</outjar> <!-- Java 17兼容性配置 --> <options> <option>--release 17</option> <option>-ignorewarnings</option> </options> </configuration> </plugin> </plugins> </build> ``` 2. **处理依赖项** 添加ProGuard依赖以支持最新Java特性: ```xml <dependencies> <dependency> <groupId>net.sf.proguard</groupId> <artifactId>proguard-base</artifactId> <version>7.3.2</version> <scope>runtime</scope> </dependency> </dependencies> ``` ### 二、Spring Boot专用配置 1. **保留启动类和方法** 在`<configuration>`中添加保留规则: ```xml <options> <option>-keep public class your.package.MainApplication { public static void main(java.lang.String[]); }</option> <option>-keepattributes Exceptions,InnerClasses,Signature,Deprecated</option> </options> ``` 2. **排除Spring相关类** 避免破坏Spring的依赖注入和注解: ```xml <options> <option>-keep @org.springframework.stereotype.Component class * { *; }</option> <option>-keep @org.springframework.context.annotation.Configuration class *</option> </options> ``` ### 三、执行与验证 1. **构建命令** ```bash mvn clean package -DskipTests ``` 2. **验证混淆结果** 使用JD-GUI工具检查生成的`*-obfuscated.jar`,类名和方法名应变为无意义字符串,但Spring Boot启动逻辑保持完整[^1]。 ### 四、常见问题处理 - **反射调用异常**:在配置中增加`-keepclasseswithmembers`规则保留反射涉及的类 - **Lombok兼容问题**:添加`-keep class lombok.** { *; }` - **模块化冲突**:启用`--add-opens`参数支持JPMS模块系统
评论 10
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值