maven springboot工程 jar包使用ProGuard 混淆代码

1.ProGuard 混淆 只能增加反编译代码的阅读难度,不能根本保护代码安全

2.maven工程使用proguard-maven-plugin这个插件

3.若工程中包含大量第三方框架,混淆后会报错,所以选择性混淆私有的逻辑代码比较容易,框架代码不混淆

添加依赖:

<dependency>
				<groupId>net.sf.proguard</groupId>
				<artifactId>proguard-base</artifactId>
				<version>5.3.3</version>
			</dependency>

pom.xml例子:

<build>
		<finalName>${artifactId}</finalName>
		<plugins>
			<plugin>
				<groupId>com.github.wvengen</groupId>
				<artifactId>proguard-maven-plugin</artifactId>
				<executions>
					<execution>
						<phase>package</phase>
						<goals><goal>proguard</goal></goals>
					</execution>
				</executions>
				<configuration>
					<proguardVersion>5.3.3</proguardVersion>
					<injar>${project.build.finalName}.jar</injar>
					<outjar>${project.build.finalName}.jar</outjar>
					<obfuscate>true</obfuscate>
					<options>
						<option>-dontshrink</option>
						<option>-dontoptimize</option>
						<!-- This option will replace all strings in reflections method invocations with new class names.
                             For example, invokes Class.forName('className')-->
						<option>-adaptclassstrings</option>
						<!-- This option will save all original annotations and etc. Otherwise all we be removed from files.-->
						<option>-keepattributes Exceptions,InnerClasses,Signature,Deprecated,
							SourceFile,LineNumberTable,*Annotation*,EnclosingMethod</option>
						<!-- This option will save all original names in interfaces (without obfuscate).-->
						<option>-keepnames interface **</option>
						<!-- This option will save all original methods parameters in files defined in -keep sections,
                             otherwise all parameter names will be obfuscate.-->
						<option>-keepparameternames</option>
						<option>-keeppackagenames</option>
						<!-- This option will save all original class files (without obfuscate) but obfuscate all in domain package.-->
						<option>-keep class com.abcutil.abcutil.App{ *; }</option>
						<option>-keep class com.abcutil.abcutil.SpringUtil{ *; }</option>
						<option>-keep class com.abcutil.abcutil.config.** { *; }</option>
						<option>-keep class com.abcutil.abcutil.dao.** { *; }</option>
						<option>-keep class com.abcutil.abcutil.entities.** { *; }</option>
						<option>-keep class com.abcutil.abcutil.exception.** { *; }</option>
						<option>-keep class com.abcutil.abcutil.schedule.SchedulerTask</option>
						<option>-keep class com.abcutil.abcutil.service.MainService</option>
						<!-- This option will save all original interfaces files (without obfuscate) in all packages.-->
						<option>-keep interface * extends * { *; }</option>
						<!-- This option will save all original defined annotations in all class in all packages.-->
						<option>-keepclassmembers class * {
							@org.springframework.beans.factory.annotation.Autowired *;
							@org.springframework.beans.factory.annotation.Value *;
							@org.springframework.stereotype.Service *;
							@org.springframework.stereotype.Component *;
							@org.springframework.scheduling.annotation.Scheduled *;
							}
						</option>
					</options>
					<libs>
						<!-- Include main JAVA library required.-->
						<lib>${java.home}/lib/rt.jar</lib>
						<!-- Include crypto JAVA library if necessary.-->
						<lib>${java.home}/lib/jce.jar</lib>
					</libs>
				</configuration>
			</plugin>

			 <!--Maven assembly must be run after proguard obfuscation so it take already obfuscated files.-->
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<executions>
					<execution>
						<goals>
							<goal>repackage</goal>
						</goals>
						<configuration>
							<mainClass>com.abcutil.abcutil.App</mainClass>
						</configuration>
					</execution>
				</executions>
			</plugin>
		</plugins>
	</build>

ProGuard 是一款功能强大的 Java 字节码优化工具,它可以混淆、压缩和优化 Java 应用程序的字节码,从而减小应用体积、防止逆向工程和保护代码版权。当你混淆 jar 包时,ProGuard 会对 Java 类中的代码进行以下操作: 1. **符号擦除**(Renaming & Shuffling): ProGuard 将类名、字段名、方法名和参数名转换为无意义的名字,使得反编译后的代码难以理解。 2. **去除冗余代码**(Pruning dead code): 删除未被引用的方法和字段,减少不必要的库依赖。 3. **压缩代码**(Obfuscation): 对代码进行包装,使逻辑变得更难理解和分析。 4. **加密敏感信息**(Code shrinking): 可选地对敏感数据进行加密或混淆,增强安全性。 5. **防止逆向工程**(Optimizations for size & speed): 进行一些优化,如消除空指针检查等,提高应用程序运行速度和内存效率。 要使用 ProGuard 混淆 jar 包,你需要按照以下步骤操作: 1. 添加 ProGuard 插件到构建工具:如果是 Maven 或 Gradle 项目,可以在 build.gradle 或 pom.xml 文件中配置插件。 - Maven 示例: ```groovy plugins { id 'com.github.johnrengelman.proguard-maven-plugin' version '0.8' } ``` - Gradle 示例: ```gradle apply plugin: 'com.github.johnrengelman.proguard-gradle' proguard { // 配置选项 } ``` 2. 创建或编辑混淆规则文件(proguard-rules.pro): 在这里定义具体的混淆规则,比如保留哪些类和方法,以及要删除的无用资源等。 3. 执行混淆过程:运行构建命令(mvn clean install, gradle assemble),其中会调用 ProGuard 工具对打包前的 class 文件进行处理。 ```bash mvn proguard:proguard # 或者对于 Gradle: ./gradlew assembleWithProguard ``` 4. 查看生成的混淆后的 jar 包:确认混淆成功后,jar 包内的代码已经经过了混淆处理。
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值