当前位置:   article > 正文

SpringBoot + proguard+maven多模块实现代码混淆_springboot java11 proguard

springboot java11 proguard

上文介绍了单个springboot的maven工程实现代码混淆,本文将会介绍多maven工程实现代码混淆。

多模块跟单模块一样,在需要混淆模块的pom文件中加入proguard依赖及配置。

 演练版本

springboot:2.5.8

jdk:1.8

proguard: 7.1.0

 新建springboot多Maven工程

新建springboot多maven工程,结构如下图,proguard-root 是顶级父工程,proguard-modu01、proguard-mudu02是两个业务模块,proguard-server是springboot启动服务类模块。

接下来会对proguard-modu01、proguard-mudu02两个模块实现proguard代码混淆。

下面以proguard-modu01工程为例介绍。

proguard-modu01 工程结构

 

proguard-modu01 的pom.xml配置代码混淆

 本次我将proguard配置项放入 <options> </options>内,也可以单独配置在proguard.cfg文件内。

特别注意避坑:多maven工程代码混淆一定要配置:<option>-keepdirectories</option>,否则在以后访问代码混淆子模块的controller时会提示404错误。

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  2. <modelVersion>4.0.0</modelVersion>
  3. <parent>
  4. <groupId>com.proguard</groupId>
  5. <artifactId>proguard-root</artifactId>
  6. <version>1.0</version>
  7. </parent>
  8. <artifactId>proguard-modu02</artifactId>
  9. <dependencies>
  10. <dependency>
  11. <groupId>org.springframework.boot</groupId>
  12. <artifactId>spring-boot-starter-web</artifactId>
  13. </dependency>
  14. <dependency>
  15. <groupId>org.springframework.boot</groupId>
  16. <artifactId>spring-boot-devtools</artifactId>
  17. <scope>runtime</scope>
  18. <optional>true</optional>
  19. </dependency>
  20. </dependencies>
  21. <!-- 配置代码混淆 开始-->
  22. <build>
  23. <plugins>
  24. <plugin>
  25. <groupId>com.github.wvengen</groupId>
  26. <artifactId>proguard-maven-plugin</artifactId>
  27. <version>2.6.0</version>
  28. <executions>
  29. <!-- 以下配置说明执行mvn的package命令时候,会执行proguard-->
  30. <execution>
  31. <phase>package</phase>
  32. <goals>
  33. <goal>proguard</goal>
  34. </goals>
  35. </execution>
  36. </executions>
  37. <configuration>
  38. <proguardVersion>7.1.0</proguardVersion>
  39. <!-- 原始jar -->
  40. <injar>${project.build.finalName}.jar</injar>
  41. <!-- 混淆后的jar -->
  42. <outjar>${project.build.finalName}.jar</outjar>
  43. <!-- 是否混淆 默认是true -->
  44. <obfuscate>true</obfuscate>
  45. <!-- 是否将生成的PG文件安装部署 -->
  46. <!-- <attach>true</attach> -->
  47. <!--指定生成文件分类-->
  48. <!--<attachArtifactClassifier>pg</attachArtifactClassifier>-->
  49. <!-- 将pom.xml打包至jar文件中 -->
  50. <addMavenDescriptor>true</addMavenDescriptor>
  51. <!-- 混淆配置文件proguard.cfg
  52. <proguardInclude>${project.basedir}/proguard.cfg</proguardInclude>-->
  53. <!-- 项目编译所需要的jar -->
  54. <libs>
  55. <lib>${java.home}/lib/rt.jar</lib>
  56. </libs>
  57. <!-- 对输入jar进行过滤比如,如下配置就是对META-INFO文件不处理。
  58. <inLibsFilter>!META-INF/**,!META-INF/versions/9/**.class</inLibsFilter>-->
  59. <!-- 输出路径配置,但是要注意这个路径必须要包括injar标签填写的jar -->
  60. <outputDirectory>${project.basedir}/target</outputDirectory>
  61. <!--配置混淆的一些细节选项,可在proguard.cfg中配置-->
  62. <options>
  63. <!-- JDK目标版本1.8-->
  64. <option>-target 1.8</option>
  65. <!-- 不做收缩(删除注释、未被引用代码)-->
  66. <option>-dontshrink</option>
  67. <!-- 不做优化(变更代码实现逻辑)-->
  68. <option>-dontoptimize</option>
  69. <!-- 不混忽略非公用类文件及成员-->
  70. <option>-dontskipnonpubliclibraryclasses</option>
  71. <option>-dontskipnonpubliclibraryclassmembers</option>
  72. <!--不用大小写混合类名机制-->
  73. <option>-dontusemixedcaseclassnames</option>
  74. <!-- 优化时允许访问并修改有修饰符的类和类的成员 -->
  75. <option>-allowaccessmodification</option>
  76. <!-- 确定统一的混淆类的成员名称来增加混淆-->
  77. <option>-useuniqueclassmembernames</option>
  78. <!-- 保持目录结构-->
  79. <option>-keepdirectories</option>
  80. <!-- 不混淆所有包名-->
  81. <!--<option>-keeppackagenames</option>-->
  82. <!-- 混淆类名之后,对使用Class.forName('className')之类的地方进行相应替代-->
  83. <option>-adaptclassstrings</option>
  84. <!-- 需要保持的属性:异常,注解等-->
  85. <option>-keepattributes Exceptions,InnerClasses,Signature,Deprecated,SourceFile,LocalVariable*Table,*Annotation*,Synthetic,EnclosingMethod</option>
  86. <!-- 此选项将保存所有软件包中的所有原始接口文件(不进行混淆) -->
  87. <option>-keepnames interface ** { *; }</option>
  88. <!-- 此选项将保存所有软件包中的所有原始接口文件(不进行混淆) -->
  89. <option>-keep interface * extends * { *; }</option>
  90. <!-- 保留参数名,因为控制器,或者Mybatis等接口的参数如果混淆会导致无法接受参数,xml文件找不到参数 -->
  91. <option>-keepparameternames</option>
  92. <!-- 保留枚举成员及方法 -->
  93. <option>-keepclassmembers enum * { *; }</option>
  94. <!-- 不混淆所有类,保存原始定义的注释 -->
  95. <option>
  96. -keepclassmembers class * {
  97. @org.springframework.context.annotation.Bean *;
  98. @org.springframework.beans.factory.annotation.Autowired *;
  99. @org.springframework.beans.factory.annotation.Value *;
  100. @org.springframework.stereotype.Service *;
  101. @org.springframework.stereotype.Component *;
  102. @org.springframework.web.bind.annotation.RestController *;
  103. }
  104. </option>
  105. <!-- 忽略warn消息 -->
  106. <option>-ignorewarnings</option> -->
  107. <!-- 忽略note消息 -->
  108. <option>-dontnote</option> -->
  109. <!-- 打印配置信息 -->
  110. <option>-printconfiguration</option>
  111. <!-- 不混淆controller入口类
  112. <option>-keep class com.platform.scamp.entity.** {*;}</option>
  113. <option>-keep class com.platform.scamp.service.ScampVehicleTrackService {*;}</option>
  114. <option>-keep class com.platform.scamp.controller.** {*;}</option>-->
  115. </options>
  116. <injarNotExistsSkip>true</injarNotExistsSkip>
  117. <!-- 把jar包放到临时目录以便缩短命令行 -->
  118. <putLibraryJarsInTempDir>true</putLibraryJarsInTempDir>
  119. </configuration>
  120. <dependencies>
  121. <dependency>
  122. <groupId>com.guardsquare</groupId>
  123. <artifactId>proguard-base</artifactId>
  124. <version>7.1.0</version>
  125. <scope>runtime</scope>
  126. </dependency>
  127. <dependency>
  128. <groupId>com.guardsquare</groupId>
  129. <artifactId>proguard-core</artifactId>
  130. <version>7.1.0</version>
  131. <scope>runtime</scope>
  132. </dependency>
  133. </dependencies>
  134. </plugin>
  135. </plugins>
  136. </build>
  137. <!-- 配置代码混淆 结束-->
  138. </project>

此时,已完成proguard-modu01模块的代码混淆配置,proguard-modu02模块同上配置即可。

proguard-server 启动服务模块的启动类配置

由于代码混淆,会存在大量的类名重名问题,因此需要在springboot启动类中修改 BeanName 生成策略。

方式一:

  1. package com.proguard;
  2. import org.springframework.beans.factory.config.BeanDefinition;
  3. import org.springframework.beans.factory.support.BeanDefinitionRegistry;
  4. import org.springframework.beans.factory.support.BeanNameGenerator;
  5. import org.springframework.boot.autoconfigure.SpringBootApplication;
  6. import org.springframework.boot.builder.SpringApplicationBuilder;
  7. @SpringBootApplication
  8. public class ProguardServerApplication {
  9. public static class CustomGenerator implements BeanNameGenerator {
  10. public String generateBeanName(BeanDefinition definition, BeanDefinitionRegistry registry) {
  11. return definition.getBeanClassName();
  12. }
  13. }
  14. public static void main(String[] args) {
  15. new SpringApplicationBuilder(ProguardServerApplication.class)
  16. .beanNameGenerator(new CustomGenerator())
  17. .run(args);
  18. }
  19. }

方式二:若是maven工程师引用了springfox-boot-starter组件、springfox-*.jar相关组件,会产生异常,重写springboot启动类,如下:

  1. @SpringBootApplication
  2. public class ProguardServerApplication {
  3. public static void main(String[] args) {
  4. new SpringApplicationBuilder(ProguardServerApplication .class)
  5. .beanNameGenerator(new ProGuardBeanNameGenerator()).run();
  6. }
  7. /**
  8. * 代码混淆后,包名、类名会存在重复,重写buildDefaultBeanName方法,获取全限定的类名
  9. */
  10. static class ProGuardBeanNameGenerator extends AnnotationBeanNameGenerator {
  11. @Override
  12. protected String buildDefaultBeanName(BeanDefinition definition) {
  13. return definition.getBeanClassName();
  14. }
  15. }
  16. }

启动,验证

通过Java反编译工具 查看源代码结果如下图,包名、类名已经重新命名,代表代码混淆已生效。

 proguard-modu01:

proguard-modu02:

启动访问

  • 访问proguard-modu01中接口:

 

  •  访问proguard-modu02中接口:

 踩过坑

1. proguard-modu01、proguard-modu02配置混淆后,访问接口提示404。去掉代码混淆恢复正常访问。

解决:在proguard-modu01、proguard-modu02的pom.xml文件中配置保持目录结构:<option>-keepdirectories</option> 

 2. 若是maven工程师引用了springfox-boot-starter组件、springfox-*.jar相关组件,会产生异常。

 2023-06-05 17:07:38.322[0;39m [31mERROR[0;39m [35m19088[0;39m [2m---[0;39m [2m[  restartedMain][0;39m [36mo.s.boot.SpringApplication              [0;39m [2m:[0;39m Application run failed

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'springfox.documentation.spring.web.scanners.ApiDocumentationScanner' defined in URL [jar:file:/D:/maven/maven-repository/io/springfox/springfox-spring-web/3.0.0/springfox-spring-web-3.0.0.jar!/springfox/documentation/spring/web/scanners/ApiDocumentationScanner.class]: Unsatisfied dependency expressed through constructor parameter 1; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'springfox.documentation.spring.web.scanners.ApiListingScanner' defined in URL [jar:file:/D:/maven/maven-repository/io/springfox/springfox-spring-web/3.0.0/springfox-spring-web-3.0.0.jar!/springfox/documentation/spring/web/scanners/ApiListingScanner.class]: Unsatisfied dependency expressed through constructor parameter 1; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'springfox.documentation.spring.web.scanners.ApiModelReader' defined in URL [jar:file:/D:/maven/maven-repository/io/springfox/springfox-spring-web/3.0.0/springfox-spring-web-3.0.0.jar!/springfox/documentation/spring/web/scanners/ApiModelReader.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'springfox.documentation.schema.CachingModelProvider' defined in URL [jar:file:/D:/maven/maven-repository/io/springfox/springfox-schema/3.0.0/springfox-schema-3.0.0.jar!/springfox/documentation/schema/CachingModelProvider.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'springfox.documentation.schema.DefaultModelProvider' defined in URL [jar:file:/D:/maven/maven-repository/io/springfox/springfox-schema/3.0.0/springfox-schema-3.0.0.jar!/springfox/documentation/schema/DefaultModelProvider.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'springfox.documentation.schema.property.CachingModelPropertiesProvider' defined in URL [jar:file:/D:/maven/maven-repository/io/springfox/springfox-schema/3.0.0/springfox-schema-3.0.0.jar!/springfox/documentation/schema/property/CachingModelPropertiesProvider.class]: Unsatisfied dependency expressed through constructor parameter 1; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'springfox.documentation.schema.property.ModelPropertiesProvider' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Qualifier(value=optimized)}
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:800) ~[spring-beans-5.3.14.jar:5.3.14]
    at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:229) ~[spring-beans-5.3.14.jar:5.3.14]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1372) ~[spring-beans-5.3.14.jar:5.3.14]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1222) ~[spring-beans-5.3.14.jar:5.3.14]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:582) ~[spring-beans-5.3.14.jar:5.3.14]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542) ~[spring-beans-5.3.14.jar:5.3.14]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) ~[spring-beans-5.3.14.jar:5.3.14]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.14.jar:5.3.14]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.14.jar:5.3.14]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.14.jar:5.3.14]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:953) ~[spring-beans-5.3.14.jar:5.3.14]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:918) ~[spring-context-5.3.14.jar:5.3.14]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583) ~[spring-context-5.3.14.jar:5.3.14]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:145) ~[spring-boot-2.5.8.jar:2.5.8]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:765) ~[spring-boot-2.5.8.jar:2.5.8]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:445) ~[spring-boot-2.5.8.jar:2.5.8]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:338) ~[spring-boot-2.5.8.jar:2.5.8]
    at org.springframework.boot.builder.SpringApplicationBuilder.run(SpringApplicationBuilder.java:143) [spring-boot-2.5.8.jar:2.5.8]
    at com.proguard.ProguardServerApplication.main(ProguardServerApplication.java:22) [classes/:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_192]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_192]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_192]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_192]
    at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) [spring-boot-devtools-2.5.8.jar:2.5.8]

 解决方式:重写springboot启动类

  1. @SpringBootApplication
  2. public class ProguardServerApplication {
  3. public static void main(String[] args) {
  4. new SpringApplicationBuilder(ProguardServerApplication .class)
  5. .beanNameGenerator(new ProGuardBeanNameGenerator()).run();
  6. }
  7. /**
  8. * 代码混淆后,包名、类名会存在重复,重写buildDefaultBeanName方法,获取全限定的类名
  9. */
  10. static class ProGuardBeanNameGenerator extends AnnotationBeanNameGenerator {
  11. @Override
  12. protected String buildDefaultBeanName(BeanDefinition definition) {
  13. return definition.getBeanClassName();
  14. }
  15. }
  16. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/天景科技苑/article/detail/1015815
推荐阅读
相关标签
  

闽ICP备14008679号