springboot项目使用proguard配置代码混淆

springboot项目使用proguard配置代码混淆

代码混淆是一些软件开发过程中必不可少的步骤。
常用的代码混淆技术有
proguard maven plugin ,
yguard maven plugin,
procyon maven plugin,
dex maven plugin .
这些代码混淆技术大同小异,都是对maven打包生成class时进行干预,来完成对java字节码的代码混淆。
本文以springboot项目使用proguard为例,讲一下如何使用proguard完成代码混淆。

物料准备:

1.pom引入proguard-maven-plugin插件

2.在proguard.cfg配置文件里设置具体的代码混淆配置

3.maven package 打包测试

pom.xml配置proguard插件


    4.0.0
    cn.thinkpet
    springboot-app-scaffold
    0.0.1-SNAPSHOT
    springboot-app-scaffold
    Demo project for Spring Boot
    
        1.8
        UTF-8
        UTF-8
        2.4.2
    
    
        
            cn.hutool
            hutool-all
            5.8.16
        
        
            org.dom4j
            dom4j
            2.0.0
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.boot
            spring-boot-starter-validation
        
        
            org.springframework.boot
            spring-boot-configuration-processor
        
         
        
            org.apache.cxf
            cxf-spring-boot-starter-jaxws
            3.2.4
        
        
            org.projectlombok
            lombok
            true
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    
    
        
            
                org.springframework.boot
                spring-boot-dependencies
                ${spring-boot.version}
                pom
                import
            
        
    

    

        
            
                org.apache.maven.plugins
                maven-compiler-plugin
                3.8.1
                
                    1.8
                    1.8
                    UTF-8
                
            


            
                com.github.wvengen
                proguard-maven-plugin
                2.6.0
                
                    
                        package
                        
                            proguard
                        
                    
                
                
        
                    7.2.2
          
                    ${project.build.finalName}.jar
                      ${project.build.finalName}.jar
                      ${project.basedir}/proguard.cfg

  
                    
                        ${java.home}/lib/rt.jar
                        ${java.home}/lib/jce.jar
                        ${java.home}/lib/jsse.jar
                    
   
                      ${project.basedir}/target
                
            


            
                org.springframework.boot
                spring-boot-maven-plugin
                ${spring-boot.version}

                
                    
                        repackage
                        
                            repackage
                        
                        
                            cn.thinkpet.springbootappscaffold.SpringbootAppScaffoldApplication
                        
                    
                
                
                    true
                
            
        

        
            
                src/main/java
                
                    **/*.properties
                    **/*.xml
                
                false
            
            
                src/main/resources
                
                    **/*.properties
                    **/*.xml
                    **/*.yml
                    **/Dockerfile
                    **/*.xdb
                    **/*.xlsx
                
                false
            
        

    



配置proguard.cfg文件

需要注意这个文件要配置到maven项目的根目录下
#jdk版本1.8
#-target 1.8
#不做收缩(不删除注释以及未被引用的代码)
-dontshrink
#不做优化(不变更代码实现逻辑)
-dontoptimize

#-dontobfuscate
#-microedition

#不使用大小写混合,混淆后的类名为小写
-dontusemixedcaseclassnames

#使用唯一的类名来混淆
-useuniqueclassmembernames

#允许访问并修改有修饰符的类和类的成员
-allowaccessmodification

#保持 包名不变
-keeppackagenames

#需要保持的属性:异常,内部类,注解等
-keepattributes Exceptions,InnerClass,Signature,Deprecated,SourceFile,LineNumberTable,LocalVariable*Table,*Annotation*,Synthetic,EnclosingMethod

#spring 相关的注解,不要混淆
-keepclassmembers class * {
  @org.springframework.** *;
  @org.springframework.beans.factory.annotation.Autowired ;
  @org.springframework.beans.factory.annotation.Autowired ;
  @javax.annotation.PostConstruct *;
  @javax.annotation.PreDestroy *;
  @javax.annotation.Resource *;
  @org.springframework.scheduling.annotation.Async ;
}

#不混淆所有的get/set方法
-keepclassmembers public class *{
  void set*(***); *** get*();
}

-keepnames interface ** {*;}
-keep interface * extends * {*;}
-keepparameternames
-keepclassmembers enum * {*;}

# 保持启动类不变
-keep public class cn.thinkpet.springbootappscaffold.SpringbootAppScaffoldApplication {*;}


#不混淆被Component等注解标记的类
-keep @org.springframework.stereotype.Component class * {*;}
-keep @org.springframework.stereotype.Service class * {*;}
-keep @org.springframework.web.bind.annotation.RestController class * {*;}
-keep @org.springframework.context.annotation.Configuration class * {*;}
#-keep @org.aspectj.lang.annotation.Aspect class * {*;}



-adaptclassstrings
#跳过非公共库的类
-skipnonpubliclibraryclasses


#忽略警告
-ignorewarnings
-dontnote
# 打印配置内容
-printconfiguration


# 配置不混淆某些类
-keep class org.slf4j.** {*;}





打包测试

执行命令

mvn clean package -DskipTests

观察控制台,等待打包完成后,看下target目录如下

在这里插入图片描述

图中的

springboot-app-springbootappscaffold-0.0.1-SNAPSHOT.jar 即为代码混淆后的jar包

proguard_map.txt里记录了jar里哪些代码被混淆了,

如Apple.class 被混淆成 a.class 这种详细的内容 ,它是依据proguard.cfg里配置的项目来进行代码混淆的。

本文来自网络,不代表协通编程立场,如若转载,请注明出处:https://net2asp.com/51117df7d7.html