|
|
@@ -11,115 +11,66 @@ import java.util.concurrent.TimeUnit;
|
|
|
/**
|
|
|
* 测试originJarOrWar
|
|
|
*
|
|
|
- * 场景说明:
|
|
|
- * 项目my-project-with-encrypted-lib-no-pwd中依赖了lib包,但是这个lib包是被class-winter混淆了的,
|
|
|
- * 那么在对my-project-with-encrypted-lib-no-pwd进行打包时,就需要指定alreadyProtectedLibs
|
|
|
- *
|
|
|
- * 以使用maven插件进行加密为例(示例一):
|
|
|
- * <plugin>
|
|
|
- * <groupId>com.idea-aedi</groupId>
|
|
|
- * <artifactId>class-winter-maven-plugin</artifactId>
|
|
|
- * <version>1.0.0</version>
|
|
|
- * <configuration>
|
|
|
- * <finalName>my-project-with-encrypted-lib-no-pwd</finalName>
|
|
|
- * <!-- 如果项目my-project-with-encrypted-lib-no-pwd本身中没有任何类需要混淆,那么这里随便写一个就行 -->
|
|
|
- * <includePrefix>non-exist</includePrefix>
|
|
|
- * <alreadyProtectedLibs>encrypted-lib-no-pwd-1.0.0.jar</alreadyProtectedLibs>
|
|
|
- * </configuration>
|
|
|
- * <executions>
|
|
|
- * <execution>
|
|
|
- * <phase>package</phase>
|
|
|
- * <goals>
|
|
|
- * <goal>class-winter</goal>
|
|
|
- * </goals>
|
|
|
- * </execution>
|
|
|
- * </executions>
|
|
|
- * </plugin>
|
|
|
- *
|
|
|
- * 以使用maven插件进行加密为例(示例二,lib需要输入密码):
|
|
|
- * <plugin>
|
|
|
- * <groupId>com.idea-aedi</groupId>
|
|
|
- * <artifactId>class-winter-maven-plugin</artifactId>
|
|
|
- * <version>1.0.0</version>
|
|
|
- * <configuration>
|
|
|
- * <finalName>my-project-with-encrypted-lib-have-pwd</finalName>
|
|
|
- * <includePrefix>non-exist</includePrefix>
|
|
|
- * <alreadyProtectedLibs>encrypted-lib-have-pwd-1.0.0.jar:qwer123~</alreadyProtectedLibs>
|
|
|
- * </configuration>
|
|
|
- * <executions>
|
|
|
- * <execution>
|
|
|
- * <phase>package</phase>
|
|
|
- * <goals>
|
|
|
- * <goal>class-winter</goal>
|
|
|
- * </goals>
|
|
|
- * </execution>
|
|
|
- * </executions>
|
|
|
- * </plugin>
|
|
|
- *
|
|
|
* @author {@link JustryDeng}
|
|
|
* @since 2021/6/12 14:34:45
|
|
|
*/
|
|
|
public class AlreadyProtectedLibs_Test {
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
- Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
|
|
|
- @Override
|
|
|
- public void run() {
|
|
|
- try {
|
|
|
- TimeUnit.SECONDS.sleep(3);
|
|
|
- } catch (InterruptedException e) {
|
|
|
- // ignore
|
|
|
- }
|
|
|
- // 杀下进程(以防下一个测试方法启动jar时因为端口被占用启动不起来)
|
|
|
- BashUtil.killProcessByPorts("8080");
|
|
|
- }
|
|
|
- }));
|
|
|
+ // 杀下进程(以保证端口没有被占用)
|
|
|
+ BashUtil.killProcessByPorts("8080");
|
|
|
|
|
|
-// test0();
|
|
|
- test1();
|
|
|
+ test0();
|
|
|
+// test1();
|
|
|
// test2();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 场景:
|
|
|
- * 项目my-project-with-encrypted-lib-no-pwd.jar中的lib包
|
|
|
- * encrypted-lib-no-pwd-1.0.0.jar本身就是被class-winter以自动生成的密码加密过了的
|
|
|
- * 测试期望结果:
|
|
|
- * 不用代理,直接(java -jar)启动jar包,会失败
|
|
|
+ * 项目my-project-with-encrypted-lib-no-pwd.jar中依赖了encrypted-lib-no-pwd-1.0.0.jar包,
|
|
|
+ * 但是这个encrypted-lib-no-pwd-1.0.0.jar包是被class-winter混淆了的,
|
|
|
+ * 如过想要正常使用my-project-with-encrypted-lib-no-pwd.jar,那么必须
|
|
|
+ * 对my-project-with-encrypted-lib-no-pwd.jar进行class-winter混淆,
|
|
|
+ * 且指定alreadyProtectedLibs为encrypted-lib-no-pwd-1.0.0.jar。
|
|
|
+ *
|
|
|
+ * 测试case:
|
|
|
+ * 不对项目my-project-with-encrypted-lib-no-pwd.jar进行class-winter混淆,直接(java -jar)启动jar。
|
|
|
+ *
|
|
|
+ * 期望结果:
|
|
|
+ * 启动后,不能正常使用encrypted-lib-no-pwd-1.0.0.jar包的功能。
|
|
|
+ * 根据my-project-with-encrypted-lib-no-pwd.jar中的逻辑,启动后会报错。
|
|
|
*/
|
|
|
private static void test0() {
|
|
|
String projectRootDir = PathUtil.getProjectRootDir(AlreadyProtectedLibs_Test.class);
|
|
|
- // 加密
|
|
|
- EncryptExecutor encryptExecutor = EncryptExecutor.builder()
|
|
|
- .originJarOrWar(projectRootDir + Constant.LINUX_FILE_SEPARATOR + "my-project-with-encrypted-lib-no-pwd.jar")
|
|
|
- // includePrefix字段是必填的,但是我们项目(my-project-with-encrypted-lib-no-pwd.jar)本身是没有混淆的,
|
|
|
- // 只是其中的lib(encrypted-lib-no-pwd-1.0.0.jar)混淆了,我们项目是没必要进行解密的,所以这里值随便填什么都可以的
|
|
|
- .includePrefix("non-exist")
|
|
|
- .alreadyProtectedLibs("encrypted-lib-no-pwd-1.0.0.jar")
|
|
|
- .build();
|
|
|
- String encryptedJar = encryptExecutor.process();
|
|
|
- System.out.println(encryptedJar);
|
|
|
-
|
|
|
+ String originJarOrWar = projectRootDir + "my-project-with-encrypted-lib-no"
|
|
|
+ + "-pwd.jar";
|
|
|
// 启动
|
|
|
- BashUtil.runCmdAndPrint(String.format("java -jar %s", encryptedJar));
|
|
|
+ BashUtil.runCmdAndPrint(String.format("java -jar %s", originJarOrWar));
|
|
|
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 场景:
|
|
|
- * 项目my-project-with-encrypted-lib-no-pwd.jar中的lib包
|
|
|
- * encrypted-lib-no-pwd-1.0.0.jar本身就是被class-winter以自动生成的密码加密过了的
|
|
|
- * 测试期望结果:
|
|
|
- * 正常启动项目my-project-with-encrypted-lib-no-pwd.jar,并
|
|
|
- * 正常使用encrypted-lib-no-pwd-1.0.0.jar中的功能
|
|
|
+ * 项目my-project-with-encrypted-lib-no-pwd.jar中依赖了encrypted-lib-no-pwd-1.0.0.jar包,
|
|
|
+ * 但是这个encrypted-lib-no-pwd-1.0.0.jar包是被class-winter混淆了的,
|
|
|
+ * 如过想要正常使用my-project-with-encrypted-lib-no-pwd.jar,那么必须
|
|
|
+ * 对my-project-with-encrypted-lib-no-pwd.jar进行class-winter混淆,
|
|
|
+ * 且指定alreadyProtectedLibs为encrypted-lib-no-pwd-1.0.0.jar。
|
|
|
+ *
|
|
|
+ * 测试case:
|
|
|
+ * 对项目my-project-with-encrypted-lib-no-pwd.jar进行class-winter混淆,然后使用javaagent启动jar。
|
|
|
+ *
|
|
|
+ * 期望结果:
|
|
|
+ * 项目正常启动,且能正常使用encrypted-lib-no-pwd-1.0.0.jar的功能。
|
|
|
+ * 根据my-project-with-encrypted-lib-no-pwd.jar中的逻辑,启动后会调用encrypted-lib-no-pwd-1.0.0.jar中的类输出一个加密解密。
|
|
|
*/
|
|
|
private static void test1() {
|
|
|
String projectRootDir = PathUtil.getProjectRootDir(AlreadyProtectedLibs_Test.class);
|
|
|
// 加密
|
|
|
EncryptExecutor encryptExecutor = EncryptExecutor.builder()
|
|
|
.originJarOrWar(projectRootDir + Constant.LINUX_FILE_SEPARATOR + "my-project-with-encrypted-lib-no-pwd.jar")
|
|
|
- // includePrefix字段是必填的,但是我们项目(my-project-with-encrypted-lib-no-pwd.jar)本身是没有混淆的,
|
|
|
- // 只是其中的lib(encrypted-lib-no-pwd-1.0.0.jar)混淆了,我们项目是没必要进行解密的,所以这里值随便填什么都可以的
|
|
|
+ // includePrefix字段是必填的,但是如果我们项目(my-project-with-encrypted-lib-no-pwd.jar)本身不需要进行混淆的话,
|
|
|
+ // 那么这里值随便填一个使其不匹配当前项目的包前缀即可
|
|
|
.includePrefix("non-exist")
|
|
|
.alreadyProtectedLibs("encrypted-lib-no-pwd-1.0.0.jar")
|
|
|
.build();
|