当前位置:   article > 正文

proguard springboot 多模块代码混淆 一些坑_使用proguard对springboot多模块打包坑

使用proguard对springboot多模块打包坑

proguard springboot 多模块代码混淆 一些坑

maven依赖使用

多模块跟单模块一样,在需要混淆模块的pom文件中加入以下依赖:

<plugin>
                <groupId>com.github.wvengen</groupId>
                <artifactId>proguard-maven-plugin</artifactId>
 				<version>2.6.0</version> 
                <executions>
                    <execution>
                        <id>proguard</id>
                        <!-- 在打包阶段混淆-->
                        <phase>package</phase>
                        <goals>
                        <!-- 只使用混淆功能-->
                            <goal>proguard</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <proguardVersion>7.1.0</proguardVersion>
                    <!-- 原始jar包-->
                    <injar>${project.build.finalName}.jar</injar>
                    <!-- 混淆之后的jar包名  此处与原始一样 混覆盖原始jar包-->
                    <outjar>${project.build.finalName}.jar</outjar>
                    <obfuscate>true</obfuscate>
                    <options>
                        <option>-dontshrink</option>
                        <option>-dontoptimize</option>
                        <!--  ##对于类成员的命名的混淆采取唯一策略-->
                        <option>-useuniqueclassmembernames</option>
                        <!-- This option will replace all strings in reflections method invocations with new class names.
                             For example, invokes Class.forName('className')-->
                        <option>-adaptclassstrings</option>
                        <!--                        ## 混淆时不生成大小写混合的类名,默认是可以大小写混合-->
                        <option>-dontusemixedcaseclassnames</option>
                        <!-- This option will save all original annotations and etc. Otherwise all we be removed from files.-->
                        <option>-keepattributes
                            Exceptions,
                            InnerClasses,
                            Signature,
                            Deprecated,
                            SourceFile,
                            LineNumberTable,
                            *Annotation*,
                            EnclosingMethod
                        </option>
                        <!-- This option will save all original names in interfaces (without obfuscate).-->
                        <option>-keepnames interface **</option>
                        <!-- This option will save all original methods parameters in files defined in -keep sections,
                             otherwise all parameter names will be obfuscate.-->
                        <option>-keepparameternames</option>
                        <!-- This option will save all original class files (without obfuscate) but obfuscate all
                             in domain and service packages.-->
                        <option>-keep
                            class com.xxx.aaa.zzzSApplication {
                            public static void main(java.lang.String[]);
                            }
                        </option>
                        <!-- This option ignore warnings such as duplicate class definitions and classes in incorrectly
                            named files-->
                        <option>-ignorewarnings</option>
                        <!-- This option will save all original class files (without obfuscate) in service package-->
                        <!-- <option>-keep class com.slm.proguard.example.spring.boot.service { *; }</option>-->
                        <!-- This option will save all original interfaces files (without obfuscate) in all packages.-->
                        <option>-keep interface * extends * { *; }</option>
                        <!-- This option will save all original defined annotations in all classes in all packages.-->
                        <option>-keep class com.xxx.aaa.config.** { *; }</option>
                        <option>-keep class com.xxx.aaa.mapper.** { *; }</option>
                        <option>-keep class com.xxx.aaa.model.** { *; }</option>
                        <option>-keep class com.xxx.aaa.filter.** { *; }</option>
                        <option>-keep class com.xxx.aaa.enums.** { *; }</option>
                        <option>-keep class com.xxx.aaa.controller.** { *; }</option>
                        <option>-keep class com.xxx.aaa.entity.** { *; }</option>
                        <option>-keep class com.fasterxml.jackson.** { *; }</option>
                        <option>-keep class org.json.JSONObject.** {** put(java.lang.String,java.util.Map);}</option>
                        <option>-keep class org.gitlab4j.**</option>
                        <option>-keepclassmembers class * {
                            @org.springframework *;
                            @org.springframework.context.annotation.Bean *;
                            @org.springframework.beans.factory.annotation.Autowired *;
                            @org.springframework.beans.factory.annotation.Value *;
                            }
                        </option>
                        <option>-dontwarn com.fasterxml.jackson.databind.**</option>
                        <option>-dontwarn com.fasterxml.jackson.**</option>
                    </options>
                    <injarNotExistsSkip>true</injarNotExistsSkip>
                    <libs>
<!--                        java11-->
<!--                        <lib>${java.home}/jmods/java.base.jmod</lib>-->
<!--                        <lib>${java.home}/jmods/java.datatransfer.jmod</lib>-->
<!--                        <lib>${java.home}/jmods/java.prefs.jmod</lib>-->
<!--                        <lib>${java.home}/jmods/java.xml.jmod</lib>-->
<!--                        <lib>${java.home}/jmods/java.desktop.jmod</lib>-->
<!--                        <lib>${java.home}/jmods/</lib>-->


						<!-- java8-->
                        <!--Put here your libraries if required-->
                        <lib>${java.home}/lib/rt.jar</lib>
                    </libs>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>com.guardsquare</groupId>
                        <artifactId>proguard-base</artifactId>
                        <version>7.1.0</version>
                        <scope>runtime</scope>
                    </dependency>
                    <dependency>
                        <groupId>com.guardsquare</groupId>
                        <artifactId>proguard-core</artifactId>
                        <version>7.1.0</version>
                        <scope>runtime</scope>
                    </dependency>
                </dependencies>
            </plugin>



            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>${maven.compiler.plugin.version}</version>
                <configuration>
                    <source>8</source>
                    <target>8</target>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <includeSystemScope>true</includeSystemScope>
                </configuration>

                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                        <configuration>
                            <mainClass>com.xxx.aaa.sssApplication</mainClass>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145

可能的坑

  1. 需要保留 不能混淆的类
<option>-keep class com.xxx.aaa.config.** { *; }</option>
<option>-keep class com.xxx.aaa.mapper.** { *; }</option>
<option>-keep class com.xxx.aaa.model.** { *; }</option>
<option>-keep class com.xxx.aaa.filter.** { *; }</option>
<option>-keep class com.xxx.aaa.enums.** { *; }</option>
<option>-keep class com.xxx.aaa.controller.** { *; }</option>
<option>-keep class com.xxx.aaa.entity.** { *; }</option>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

若项目中存在xxxMapper.xml,对于xml文件中引用的相关entity、model等类,这些类不能混淆,要排除。
项目中存在过滤器类、枚举也不能混淆;
2 混淆后的jar包内不存在BOOT-INF
需要加入这两个依赖,一般后一个依赖没加

<dependencies>
    <dependency>
        <groupId>com.guardsquare</groupId>
        <artifactId>proguard-base</artifactId>
        <version>7.1.0</version>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>com.guardsquare</groupId>
        <artifactId>proguard-core</artifactId>
        <version>7.1.0</version>
        <scope>runtime</scope>
    </dependency>
</dependencies>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

3,对于java不同版本

                    <libs>
<!--                        java11-->
<!--                        <lib>${java.home}/jmods/java.base.jmod</lib>-->
<!--                        <lib>${java.home}/jmods/java.datatransfer.jmod</lib>-->
<!--                        <lib>${java.home}/jmods/java.prefs.jmod</lib>-->
<!--                        <lib>${java.home}/jmods/java.xml.jmod</lib>-->
<!--                        <lib>${java.home}/jmods/java.desktop.jmod</lib>-->
<!--                        <lib>${java.home}/jmods/</lib>-->


						<!-- java8-->
                        <!--Put here your libraries if required-->
                        <lib>${java.home}/lib/rt.jar</lib>
                    </libs>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

4,启动jar包时:

org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [com.xxx.aaa.zzzCSApplication]; nested exception is org.springframework.context.annotation.ConflictingBeanDefinitionException: Annotation-specified bean name 'b' for bean class [com.xxx.aaa.c.b] conflicts with existing, non-compatible bean definition of same name and class [com.xxx.aaa.a.b]
	at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:189)
	at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:331)
	at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:247)
	at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:311)
	at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:112)
	at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:746)
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:564)
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:147)
	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:734)
	at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:408)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:308)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1306)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1295)
	at com.xxx.aaa.SSCSApplication.main(SSCSApplication.java:47)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:49)
	at org.springframework.boot.loader.Launcher.launch(Launcher.java:108)
	at org.springframework.boot.loader.Launcher.launch(Launcher.java:58)
	at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:65)
Caused by: org.springframework.context.annotation.ConflictingBeanDefinitionException: Annotation-specified bean name 'b' for bean class [com.xxx.aaa.c.b] conflicts with existing, non-compatible bean definition of same name and class [com.xxx.aaa.a.b]
	at org.springframework.context.annotation.ClassPathBeanDefinitionScanner.checkCandidate(ClassPathBeanDefinitionScanner.java:349)
	at org.springframework.context.annotation.ClassPathBeanDefinitionScanner.doScan(ClassPathBeanDefinitionScanner.java:287)
	at org.springframework.context.annotation.ComponentScanAnnotationParser.parse(ComponentScanAnnotationParser.java:128)
	at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:296)
	at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:250)
	at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:207)
	at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:175)
	... 21 common frames omitted

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33

问题原因:
出现这个问题主要还是混淆之后,bean重名了,spring默认是把类名的首字母小写加载到容器里面,我们混淆类名之后,就容易造成beanName重复。
解决方案:
通过改变spring加载bean的命名策略来解决这个问题,把包名带上,同时在获取Spring上下文getBean的时候,加上包名路径即可
启动类配置,具体如下:

@SpringBootApplication
public class CcnyApplication{

	public static class CustomGenerator implements BeanNameGenerator {

		@Override
		public String generateBeanName(BeanDefinition definition, BeanDefinitionRegistry registry) {
			return definition.getBeanClassName();
		}
	}

	public static void main(String[] args) {
		SpringApplicationBuilder sab=new SpringApplicationBuilder(CcnyApplication.class)
				.beanNameGenerator(new CustomGenerator());
		sab.run(args);
	}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

总结:个人感觉这个主要混淆service,很多不太适用

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

闽ICP备14008679号