Introduce how to encrypt spring boot project
After a spring program is encrypted by Jar2Exe, 2 features will fail:
Any kind of
Program could not automatically find and instantiate a
@Mapperbean. It could not find mapper xml file according to a wildcard such as
Any kind of
@Valuewill not work.
Because of the incompatibility of
encryption, we choose to encrypt part of the program, and keep the
spring part un-encrypted:
@Serviceclass: we split a service class into a
parent classand a
parent classis the real service class with business code to be encrypted. It has no annotations.
child classextends the
parent class, with
@Serviceto be auto-scaned, with
@Autowiredto be injected to by spring.
@Mapperclass: they are interfaces, we need not encrypt them.
mapper/xxx.xml: there are sql statements, if we want to encrypt, we need to declare them one by one explicitly.
@Controllerclass: usually we need not encrypt. If we want, do like
/templates/, these files are referred by their names, encyption compatible, encrypt if we want.
/application.properties, this file is used by its name, encryption compatible.
In order to be partially encrypted, we split a spring-boot project into 3 projects, please refer to the demo project:
demo-webis the original spring-boot project, keep un-encrypted.
@Serviceclasses are in it.
demo-coreis to be encrypted.
@Serviceare in this project. This project is a dependency of
@Mapperclasses, keep un-encrypted. In order to let
@Mapperclasses accessible in
demo-coreduring developing, as a dependency of
Encrypt using Jar2Exe
mvn build at the parent directory, then we will get the result
First, please download the most recent Jar2Exe
v2.2.4or above for v2.2,
v2.5.2or above for v2.5.
demo-web-0.0.1-SNAPSHOT.jar, we will get:
demo-web-0.0.1-SNAPSHOT ├── BOOT-INF │ ├── classes │ │ └── ……… │ └── lib │ ├── ……… │ ├── demo-core-0.0.1-SNAPSHOT.jar │ ├── demo-share-0.0.1-SNAPSHOT.jar │ └── ……… ├── META-INF │ ├── MANIFEST.MF │ └── ……… └── org └── springframework └── boot └── loader └── ………
because Jar2Exe has provided a ClassLoader, so we only need those files under
/BOOT-INF/and ignore other files.
Pack the contents under
/BOOT-INF/classes/into a zip or jar file:
cd BOOT-INF/classes/ jar cvf ../classes.jar *
At the step 1, choose
/BOOT-INF/lib/demo-core-....jaras the main jar.
At the step 3, input
com.regexlab.j2e.demo.DemoWebApplicationas the main class, ignore the warning when we click
At the step 4, we check the
Encrypt and hide.
At the step 5, we add
BOOT-INF/lib/*files as dependencies. Keep
demo-share-....jarNOT protected. As to other jar files, we need NOT to enable
Protectbecause it will slow down the startup of program if we choose more jars to be protected.
Demo Spring Boot Project