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

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

maven依赖使用

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

<plugin>
                <groupId>com.github.wvengengroupId>
                <artifactId>proguard-maven-pluginartifactId>
 				<version>2.6.0version> 
                <executions>
                    <execution>
                        <id>proguardid>
                        
                        <phase>packagephase>
                        <goals>
                        
                            <goal>proguardgoal>
                        goals>
                    execution>
                executions>
                <configuration>
                    <proguardVersion>7.1.0proguardVersion>
                    
                    <injar>${project.build.finalName}.jarinjar>
                    
                    <outjar>${project.build.finalName}.jaroutjar>
                    <obfuscate>trueobfuscate>
                    <options>
                        <option>-dontshrinkoption>
                        <option>-dontoptimizeoption>
                        
                        <option>-useuniqueclassmembernamesoption>
                        
                        <option>-adaptclassstringsoption>
                        
                        <option>-dontusemixedcaseclassnamesoption>
                        
                        <option>-keepattributes
                            Exceptions,
                            InnerClasses,
                            Signature,
                            Deprecated,
                            SourceFile,
                            LineNumberTable,
                            *Annotation*,
                            EnclosingMethod
                        option>
                        
                        <option>-keepnames interface **option>
                        
                        <option>-keepparameternamesoption>
                        
                        <option>-keep
                            class com.xxx.aaa.zzzSApplication {
                            public static void main(java.lang.String[]);
                            }
                        option>
                        
                        <option>-ignorewarningsoption>
                        
                        
                        
                        <option>-keep interface * extends * { *; }option>
                        
                        <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>trueinjarNotExistsSkip>
                    <libs>









						
                        
                        <lib>${java.home}/lib/rt.jarlib>
                    libs>
                configuration>
                <dependencies>
                    <dependency>
                        <groupId>com.guardsquaregroupId>
                        <artifactId>proguard-baseartifactId>
                        <version>7.1.0version>
                        <scope>runtimescope>
                    dependency>
                    <dependency>
                        <groupId>com.guardsquaregroupId>
                        <artifactId>proguard-coreartifactId>
                        <version>7.1.0version>
                        <scope>runtimescope>
                    dependency>
                dependencies>
            plugin>



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

            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
                <configuration>
                    <includeSystemScope>trueincludeSystemScope>
                configuration>

                <executions>
                    <execution>
                        <goals>
                            <goal>repackagegoal>
                        goals>
                        <configuration>
                            <mainClass>com.xxx.aaa.sssApplicationmainClass>
                        configuration>
                    execution>
                executions>
            plugin>

可能的坑

  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>

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

<dependencies>
    <dependency>
        <groupId>com.guardsquaregroupId>
        <artifactId>proguard-baseartifactId>
        <version>7.1.0version>
        <scope>runtimescope>
    dependency>
    <dependency>
        <groupId>com.guardsquaregroupId>
        <artifactId>proguard-coreartifactId>
        <version>7.1.0version>
        <scope>runtimescope>
    dependency>
dependencies>

3,对于java不同版本

                    <libs>









						
                        
                        <lib>${java.home}/lib/rt.jarlib>
                    libs>

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

问题原因:
出现这个问题主要还是混淆之后,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);
	}
}

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

你可能感兴趣的:(源代码混淆,java,spring,boot)