How to Secure Your SpringBoot Application from Decompilation with ClassFinal
This guide explains how to protect a SpringBoot-based e‑commerce project from source leakage by using code obfuscation or the ClassFinal Maven plugin, detailing configuration, startup options, decompilation results, and machine‑binding to ensure the packaged jar runs only on authorized servers.
Scenario
A project needs to be deployed on a third‑party server without exposing source code, requiring the production startup package to be secured against decompilation tools.
Solution
First option: code obfuscation using proguard-maven-plugin . This works for single‑module projects but becomes complex in multi‑module setups.
Second option: code encryption using classfinal-maven-plugin . This approach is simpler, encrypts source files, configuration files, and dependencies, and supports machine‑binding so the encrypted project runs only on specified machines.
Project Operation
Add the following plugins to the
pom.xmlof the SpringBoot startup module, placing the ClassFinal plugin after the
spring-boot-maven-plugin:
<code><build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<!-- Encryption plugin -->
<groupId>net.roseboy</groupId>
<artifactId>classfinal-maven-plugin</artifactId>
<version>1.2.1</version>
<configuration>
<password>#</password> <!-- No password needed at startup -->
<excludes>org.spring</excludes>
<packages>${groupId}</packages>
<cfgfiles>application.yml,application-dev.yml</cfgfiles>
<libjars>hutool-all.jar</libjars>
<code>xxxx</code> <!-- Machine code for binding -->
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>classFinal</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build></code>Startup Methods
No‑Password Startup
java -javaagent:xxx-encrypted.jar -jar xxx-encrypted.jar
Password‑Protected Startup
java -javaagent:xxx-encrypted.jar='-pwd=密码' -jar xxx-encrypted.jar
Decompilation Effect
After encryption, method bodies are cleared while retaining parameters and annotations, allowing Swagger documentation to work. Decompilation only reveals method signatures and annotations, not the actual implementation. Decryption occurs entirely in memory during startup, leaving no decrypted files on disk.
YAML configuration files appear blank after encryption.
Machine‑Binding Startup
Download
classfinal-fatjar-1.2.1.jarand generate a machine code:
java -jar classfinal-fatjar-1.2.1.jar -CInsert the generated machine code into the
<code>element of the plugin configuration; the packaged project will then run only on that specific machine.
macrozheng
Dedicated to Java tech sharing and dissecting top open-source projects. Topics include Spring Boot, Spring Cloud, Docker, Kubernetes and more. Author’s GitHub project “mall” has 50K+ stars.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.