如何使用 spring-boot gradle 插件进行混淆

2024-01-02

如何使用 Spring Boot 2 和 gradle 构建设置 proguard 混淆?

你好。尝试使用其 gradle 插件和 Proguard gradle 插件设置 Spring Boot 应用程序的代码混淆。 Google 主要为旧的 spring-boot-gradle-plugin 版本提供了一些方法(即这个最接近的 https://gist.github.com/kimhanjoon/7423bcc8e062832fbb25527c764f3e2a使用不存在的 bootRepackage 任务),或使用 Maven 插件(具有重新打包目标)。

据我了解,想法是在 jar 打包之前混淆类,但我在当前的 gradle 插件版本中没有看到任何入口点,并且希望避免手动提取和压缩回来。

有人使用这个组合吗? Spring Boot 版本 >=2.0.3。


我认为今天是不可能的,你必须通过手动提取和压缩回来来完成。

手动提取和压缩回来的示例可能会有所帮助:

构建.gradle

version = '0.1.0'

buildscript {
    dependencies {
        classpath 'net.sf.proguard:proguard-gradle:6.0.3'
        classpath 'net.sf.proguard:proguard-base:6.0.3'
    }
}

apply plugin: 'java'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

task extractJar(type: Copy) {
    def zipFile = file("${buildDir}/libs/your_project_name-${version}.jar")
    def outputDir = file("${buildDir}/unpacked/")

    from zipTree(zipFile)
    into outputDir
}

task proguard(type: proguard.gradle.ProGuardTask) {
    doFirst {
        tasks.extractJar.execute();
    }
    configuration 'proguard.conf'
    injars  "${buildDir}/unpacked/BOOT-INF/classes"
    outjars "${buildDir}/obfClasses"

    libraryjars "${System.getProperty('java.home')}/lib/rt.jar"
    libraryjars "${buildDir}/unpacked/BOOT-INF/lib"

    doLast {
        tasks.deleteClasses.execute();
    }
}

task deleteClasses(type: Delete) {
    delete "${buildDir}/unpacked/BOOT-INF/classes/"

    doLast {
        tasks.copyObfuscatedClasses.execute()
    }
}

task copyObfuscatedClasses(type: Copy) {
    from "${buildDir}/obfClasses"
    into "${buildDir}/unpacked/BOOT-INF/classes/"
    include 'com/**'
    include '*.properties'

    doLast {
        tasks.copyObfuscatedJars.execute()
    }
}

task copyObfuscatedJars(type: Copy) {
    from "${buildDir}/obfClasses"
    into "${buildDir}/unpacked/BOOT-INF/lib/"
    include '*.jar'

    doLast {
        tasks.deleteObfuscated.execute()
    }
}

task deleteObfuscated(type: Delete) {
    delete 'build/obfClasses'

    doLast {
        tasks.repackage.execute()
    }
}

task repackage(type: Zip) {
    from  "${buildDir}/unpacked"
    entryCompression ZipEntryCompression.STORED
    archiveName "your_project_name-${version}-obf.jar"
    destinationDir(file("${buildDir}/libs"))
}

混淆器配置文件

-ignorewarnings
-keepdirectories

-keep interface com.your_package.** { *; }
-keep class com.your_package.main{ *; }
-keep class com.your_package.model.** { *; }

-keepparameternames
-keepclassmembers @org.springframework.** class * {
    *;
}

-keepclassmembers @org.springframework.** interface * {
    *;
}

-keepclassmembers enum * {
    public static **[] values();
    public static ** valueOf(java.lang.String);
}

-keep @org.springframework.** class *
-keepclassmembers @javax.** class * { *; }

-dontwarn org.springframework.**
-dontwarn javax.**
-dontwarn org.yaml.snakeyaml.**
-dontwarn okhttp3.**
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何使用 spring-boot gradle 插件进行混淆 的相关文章

随机推荐