概要
加密主体是xjar,可以添加xjar-maven-plugin插件方便打包使用,xjar插件为开源插件,可自行在github上下载源码使用。
xjar开源地址:https://github.com/core-lib/xjar;
xjar-maven-plugin开源地址:https://github.com/core-lib/xjar-maven-plugin。
细节
- 在需要加密的项目中引入
xjar-maven-plugin
插件
<!-- 添加 XJar Maven 插件 -->
<plugin>
<groupId>com.github.core-lib</groupId>
<artifactId>xjar-maven-plugin</artifactId>
<version>4.0.3</version>
<executions>
<execution>
<goals>
<goal>build</goal>
</goals>
<phase>install</phase>
<!-- 或使用
<phase>package</phase>
-->
<configuration>
<!-- 需要进行加密的路径 -->
<includes>
<!-- 路径为编译后的classes目录下 -->
<include>/com/**</include>
</includes>
<!-- 不需要进行加密的路径 -->
<excludes>
<exclude>templates/**</exclude>
</excludes>
<!-- 打包名称 -->
<targetJar>test-1.0-SNAPSHOT.xjar</targetJar>
</configuration>
</execution>
</executions>
</plugin>
-
打包加密使用
mvn clean package -Dxjar.password=加密密码
命令即可。将生成以下文件
jar文件:是未加密的jar包
xjar文件:是已经加密的包
go文件:是以go环境启动加密包的工具包(因服务器不含go环境,所以不考虑) -
启动加密包
1、使用java -jar xxx.xjar
启动
2、启动后控制台需要输入加密信息,以下命令逐行输入,最后一行为密码
AES/CBC/PKCS5Padding
128
128
加密密码
补充
因项目部署都是采用流水线的方式,项目启动都是通过linux的执行脚本启动的,但是在执行脚本中难以实现在java应用进程中输入解密信息,因此需要将主体xjar
包内容修改。下载xjar
源码,找到源码中的XLauncher.java
文件,将
改为
这样就可以通过java -Dpassword=加密密码 -jar xxx.xjar
的方式启动加密包了,密码也可采用获取流水线的变量值来实现部署的服务器上取不到加密密码,阿里云的流水线获取变量的方式为${加密密码}
方式获取,deploy.sh
也可行。