GitHub: https://github.com/core-lib/xjar
JDK 1.7 +
<project>
<repositories>
<repository>
<id>jitpack.ioid>
<url>https://jitpack.iourl>
repository>
repositories>
<dependencies>
<dependency>
<groupId>com.github.core-libgroupId>
<artifactId>xjarartifactId>
<version>v2.0.5version>
dependency>
dependencies>
project>
// Spring-Boot Jar包加密
String password = "io.xjar";
XKey xKey = XKit.key(password);
XBoot.encrypt("/path/to/read/plaintext.jar", "/path/to/save/encrypted.jar", xKey);
// 危险加密模式,即不需要输入密码即可启动的加密方式,这种方式META-INF/MANIFEST.MF中会保留密钥,请谨慎使用!
String password = "io.xjar";
XKey xKey = XKit.key(password);
XBoot.encrypt("/path/to/read/plaintext.jar", "/path/to/save/encrypted.jar", xKey, XConstants.MODE_DANGER);
// Spring-Boot Jar包解密
String password = "io.xjar";
XKey xKey = XKit.key(password);
XBoot.decrypt("/path/to/read/encrypted.jar", "/path/to/save/decrypted.jar", xKey);
// Jar包加密
String password = "io.xjar";
XKey xKey = XKit.key(password);
XJar.encrypt("/path/to/read/plaintext.jar", "/path/to/save/encrypted.jar", xKey);
// 危险加密模式,即不需要输入密码即可启动的加密方式,这种方式META-INF/MANIFEST.MF中会保留密钥,请谨慎使用!
String password = "io.xjar";
XKey xKey = XKit.key(password);
XJar.encrypt("/path/to/read/plaintext.jar", "/path/to/save/encrypted.jar", xKey, XConstants.MODE_DANGER);
// Jar包解密
String password = "io.xjar";
XKey xKey = XKit.key(password);
XJar.decrypt("/path/to/read/encrypted.jar", "/path/to/save/decrypted.jar", xKey);
// 命令行运行JAR 然后在提示输入密码的时候输入密码后按回车即可正常启动
java -jar /path/to/encrypted.jar
// 也可以通过传参的方式直接启动,不太推荐这种方式,因为泄露的可能性更大!
java -jar /path/to/encrypted.jar --xjar.password=PASSWORD
// 对于 nohup 或 javaw 这种后台启动方式,无法使用控制台来输入密码,推荐使用指定密钥文件的方式启动
nohup java -jar /path/to/encrypted.jar --xjar.keyfile=/path/to/xjar.key
参数名称 | 参数含义 | 缺省值 | 说明 |
---|---|---|---|
–xjar.password | 密码 | ||
–xjar.algorithm | 密钥算法 | AES | 支持JDK所有内置算法,如AES / DES … |
–xjar.keysize | 密钥长度 | 128 | 根据不同的算法选取不同的密钥长度。 |
–xjar.ivsize | 向量长度 | 128 | 根据不同的算法选取不同的向量长度。 |
–xjar.keyfile | 密钥文件 | ./xjar.key | 密钥文件相对或绝对路径。 |
密钥文件采用properties的书写格式:
password: PASSWORD
algorithm: ALGORITHM
keysize: KEYSIZE
ivsize: IVSIZE
hold: HOLD
其中 algorithm/keysize/ivsize/hold 均有缺省值,当 hold 值不为 true | 1 | yes | y 时,密钥文件在读取后将自动删除。
参数名称 | 参数含义 | 缺省值 | 说明 |
---|---|---|---|
password | 密码 | 无 | 密码字符串 |
algorithm | 密钥算法 | AES | 支持JDK所有内置算法,如AES / DES … |
keysize | 密钥长度 | 128 | 根据不同的算法选取不同的密钥长度。 |
ivsize | 向量长度 | 128 | 根据不同的算法选取不同的向量长度。 |
hold | 是否保留 | false | 读取后是否保留密钥文件。 |
默认情况下,即没有提供过滤器的时候,将会加密所有资源其中也包括项目其他依赖模块以及第三方依赖的 JAR 包资源,
框架提供使用过滤器的方式来灵活指定需要加密的资源或排除不需要加密的资源。
// 假如项目所有类的包名都以 com.company.project 开头,那只加密自身项目的字节码即可采用以下方式。
XBoot.encrypt(
"/path/to/read/plaintext.jar",
"/path/to/save/encrypted.jar",
"io.xjar",
(entry) -> {
String name = entry.getName();
String pkg = "com/company/project/";
return name.startsWith(pkg);
}
);
// 1. 采用Ant表达式过滤器更简洁地来指定需要加密的资源。
XBoot.encrypt(plaintext, encrypted, password, new XJarAntEntryFilter("com/company/project/**"));
XBoot.encrypt(plaintext, encrypted, password, new XJarAntEntryFilter("mapper/*Mapper.xml"));
XBoot.encrypt(plaintext, encrypted, password, new XJarAntEntryFilter("com/company/project/**/*API.class"));
// 2. 采用更精确的正则表达式过滤器。
XBoot.encrypt(plaintext, encrypted, password, new XJarRegexEntryFilter("com/company/project/(.+)"));
XBoot.encrypt(plaintext, encrypted, password, new XJarRegexEntryFilter("mapper/(.+)Mapper.xml"));
XBoot.encrypt(plaintext, encrypted, password, new XJarRegexEntryFilter("com/company/project/(.+)/(.+)API.class"));
当过滤器的逻辑复杂或条件较多时可以将过滤器分成多个,并且使用 XKit 工具类提供的多个过滤器混合方法混合成一个,XKit 提供 “与” “或” “非” 三种逻辑运算的混合。
// 1. 与运算,即所有过滤器都满足的情况下才满足,mix() 方法返回的是this,可以继续拼接。
XEntryFilter and = XKit.and()
.mix(new XJarAntEntryFilter("com/company/project/**"))
.mix(new XJarAntEntryFilter("*/**.class"));
XEntryFilter all = XKit.all()
.mix(new XJarAntEntryFilter("com/company/project/**"))
.mix(new XJarAntEntryFilter("*/**.class"));
// 2. 或运算,即任意一个过滤器满足的情况下就满足,mix() 方法返回的是this,可以继续拼接。
XEntryFilter or = XKit.or()
.mix(new XJarAntEntryFilter("com/company/project/**"))
.mix(new XJarAntEntryFilter("mapper/*Mapper.xml"));
XEntryFilter any = XKit.any()
.mix(new XJarAntEntryFilter("com/company/project/**"))
.mix(new XJarAntEntryFilter("mapper/*Mapper.xml"));
// 3. 非运算,即除此之外都满足,该例子中即排除项目或其他模块和第三方依赖jar中的静态文件。
XEntryFilter not = XKit.not(
XKit.or()
.mix(new XJarAntEntryFilter("static/**"))
.mix(new XJarAntEntryFilter("META-INF/resources/**"))
);
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
如果项目中使用了 JPA 且实现为Hibernate时,由于Hibernate自己解析加密后的Jar文件,所以无法正常启动,
可以采用以下解决方案
由于静态文件被加密后文件体积变大,Spring Boot 会采用文件的大小作为 Content-Length 头返回给浏览器,
但实际上通过 XJar 加载解密后文件大小恢复了原本的大小,所以浏览器认为还没接收完导致一直等待服务端。
由此我们需要在加密时忽略静态文件的加密,实际上静态文件也没加密的必要,因为即便加密了用户在浏览器
查看源代码也是能看到完整的源码的。通常情况下静态文件都会放在 static/ 和 META-INF/resources/ 目录下,
我们只需要在加密时通过过滤器排除这些资源即可,可以采用以下的过滤器:
XKit.not(
XKit.or()
.mix(new XJarAntEntryFilter("static/**"))
.mix(new XJarAntEntryFilter("META-INF/resources/**"))
);
或通过插件配置排除
<plugin>
<groupId>com.github.core-libgroupId>
<artifactId>xjar-maven-pluginartifactId>
<version>v2.0.5version>
<executions>
<execution>
<goals>
<goal>buildgoal>
goals>
<phase>packagephase>
<configuration>
<password>io.xjarpassword>
<excludes>
<exclude>static/**exclude>
<exclude>META-INF/resources/**exclude>
excludes>
configuration>
execution>
executions>
plugin>
XJar-Maven-Plugin
GitHub: https://github.com/core-lib/xjar-maven-plugin
<project>
<pluginRepositories>
<pluginRepository>
<id>jitpack.ioid>
<url>https://jitpack.iourl>
pluginRepository>
pluginRepositories>
<build>
<plugins>
<plugin>
<groupId>com.github.core-libgroupId>
<artifactId>xjar-maven-pluginartifactId>
<version>v2.0.5version>
<executions>
<execution>
<goals>
<goal>buildgoal>
goals>
<phase>packagephase>
<configuration>
<password>io.xjarpassword>
<includes>
<include>com/company/project/**include>
<include>mapper/*Mapper.xmlinclude>
includes>
configuration>
execution>
executions>
plugin>
plugins>
build>
project>
mvn xjar:build -Dxjar.password=io.xjar
mvn xjar:build -Dxjar.password=io.xjar -Dxjar.targetDir=/directory/to/save/target.xjar
mvn clean package -Dxjar.password=io.xjar
mvn clean install -Dxjar.password=io.xjar -Dxjar.targetDir=/directory/to/save/target.xjar
参数名称 | 命令参数名称 | 参数说明 | 参数类型 | 缺省值 | 示例值 |
---|---|---|---|---|---|
password | -Dxjar.password | 密码字符串 | String | 必须 | 任意字符串,io.xjar |
algorithm | -Dxjar.algorithm | 加密算法名称 | String | AES | JDK内置加密算法,如:AES / DES |
keySize | -Dxjar.keySize | 密钥长度 | int | 128 | 根据加密算法而定,56,128,256 |
ivSize | -Dxjar.ivSize | 密钥向量长度 | int | 128 | 根据加密算法而定,128 |
mode | -Dxjar.mode | 加密模式 | int | 0 | 0:普通模式 1:危险模式(免密码启动) |
sourceDir | -Dxjar.sourceDir | 源jar所在目录 | File | ${project.build.directory} | 文件目录 |
sourceJar | -Dxjar.sourceJar | 源jar名称 | String | ${project.build.finalName}.jar | 文件名称 |
targetDir | -Dxjar.targetDir | 目标jar存放目录 | File | ${project.build.directory} | 文件目录 |
targetJar | -Dxjar.targetJar | 目标jar名称 | String | ${project.build.finalName}.xjar | 文件名称 |
includes | -Dxjar.includes | 需要加密的资源路径表达式 | String[] | 无 | com/company/project/** , mapper/*Mapper.xml , 支持Ant表达式 |
excludes | -Dxjar.excludes | 无需加密的资源路径表达式 | String[] | 无 | static/** , META-INF/resources/** , 支持Ant表达式 |
当 includes 和 excludes 同时使用时即加密在includes的范围内且排除了excludes的资源。
更多文档:XJar-Maven-Plugin
Apache-2.0
QQ 950956093