SpringBoot结合ProGuard实现代码混淆(最新版)

前言

研究ProGuard也花了两天时间,其实最主要的时间花在前面proguard读取jar包的时候相关jar冲突的问题,但是总的来说不用拆分SpringBoot项目并且实现代码混淆已经很舒服了。

ProGuard集成

 1.maven的配置

具体配置如下:

<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>6.2.2</proguardVersion>
					<injar>${project.build.finalName}.jar</injar>
					<outjar>${project.build.finalName}.jar</outjar>
					<!--<proguardInclude>${project.basedir}/proguard.cfg</proguardInclude>-->
					<obfuscate>true</obfuscate>
					<options>
						<!-- 不做收缩(删除注释、未被引用代码)-->
						<option>-dontshrink</option>
						<!-- 不做优化(变更代码实现逻辑)-->
						<option>-dontoptimize</option>
						<!--保持目录结构,否则spring的自动注入无法使用-->
						<!--<option>-keepdirectories</option>-->
						<option>-keepattributes Exceptions,InnerClasses,Signature,Deprecated,
							SourceFile,LineNumberTable, *Annotation*,EnclosingMethod
						</option>
						<option>-adaptclassstrings</option>
						<option>
							<!-- 保护程序入口 -->
							 -keep class com.jingchen.ccny.CmepApplication { *; }
						</option>
						<option>-keepnames interface ** { *; }</option>
						<!-- 固定几个类不能混淆-->
						<option>-keepnames class com.jingchen.ccny.base.BaseService { *; }</option>
						<option>-keep class com.jingchen.ccny.common.cache.ConvertorNewCache { *; }</option>
						<option>-keep class com.jingchen.ccny.base.ControllerContext { *; }</option>
						<option>-keep class * extends com.jingchen.ccny.base.BaseService</option>
						<option>-keep class * implements com.jingchen.ccny.common.service.CallBackGuiService</option>
						<option>-keep class * implements com.jingchen.ccny.common.service.CallBackUDService</option>
						<option>-keep class com.jingchen.ccny.util.SpringUtil</option>
						<!--<option>-keep interface * extends * { *; }</option>-->
						<!-- 此选项将在所有包的所有类中保存所有原始定义的注释.-->
						<option>
							 -keep 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>
						<lib>${java.home}/lib/jce.jar</lib>
					</libs>
				</configuration>
				<dependencies>
					<dependency>
						<groupId>net.sf.proguard</groupId>
						<artifactId>proguard-base</artifactId>
						<version>6.2.2</version>
					</dependency>
				</dependencies>
			</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.jingchen.ccny.CcnyApplication</mainClass>
						</configuration>
					</execution>
				</executions>
			</plugin>
		</plugins>
	</build>

这里里面级联引用的jar很多,建议配置了私服的人先把镜像地址配置成maven中央仓库地址,这样先下下来相关依赖的包,然后再上传到你们的私服上去。据我研究中知道的jar就有:

在这里插入图片描述

注意:不连外网配置maven中央仓库的话,少了jar你们会很头疼的。而且不止net.sf.proguard相关包,还包括了com.guardsquare.proguard-base 和 com.guardsquare.proguard-core 相关的jar,所以真的连外网下包很重要!!!!

SpringBoot结合ProGuard实现代码混淆(最新版)

扫一扫手机访问