【问题标题】:Spring Boot ProGuard Embedded Tomcat ObfuscationSpring Boot ProGuard 嵌入式 Tomcat 混淆
【发布时间】:2016-05-19 08:57:09
【问题描述】:

我不确定 ProGuard 是否适合这个。 我有一个需要混淆的 Spring Boot 应用程序。使用 proguard-maven-plugin 我达到了我的代码被混淆的地步。无论如何,我不得不重新打包一些 jar,因为 spring loader 不支持压缩的,但这不是问题。 嵌入式 tomcat 的启动方式有问题,因为我遇到了这个问题:

[main] ERROR o.s.boot.SpringApplication - Application startup failed                                              
org.springframework.context.ApplicationContextException: Unable to start embedded container; nested exception is org.springfram
ework.context.ApplicationContextException: Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContaine
rFactory bean.  

proguard-maven-plugin 配置

<?xml version="1.0" encoding="UTF-8"?>
<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 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>org.foogroup</groupId>
        <artifactId>foo</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

    <name>fooname</name>
    <description>foo description</description>
    <groupId>foogid</groupId>
    <artifactId>fooaid</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <packaging>jar</packaging>

    <properties>
        <!-- Specify Java Compiler Version -->
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <dependencies>

        <dependency>
            <groupId>org.apache.pdfbox</groupId>
            <artifactId>pdfbox</artifactId>
            <version>1.8.11</version>
        </dependency>

        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>itextpdf</artifactId>
            <version>5.5.3</version>
        </dependency>


    </dependencies>

    <build>

        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>

        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>

            <plugin>
                <groupId>com.github.wvengen</groupId>
                <artifactId>proguard-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>proguard</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <obfuscate>true</obfuscate>
                    <injar>${project.build.finalName}.jar</injar>
                    <outjar>${project.build.finalName}-small.jar</outjar>
                    <outputDirectory>${project.build.directory}</outputDirectory>

                    <options>
                        <option>-optimizations !class/marking/final</option>
                        <option>-adaptresourcefilecontents
                            **.properties,META-INF/MANIFEST.MF,META-INF/spring.*</option>
                        <option>-keepattributes
                            Exceptions,InnerClasses,Signature,Deprecated,SourceFile,LineNumberTable,*Annotation*,EnclosingMethod</option>
                        <option>-keepclasseswithmembers public class * { public static
                            void main(java.lang.String[]);}</option>
                        <option>-keepclassmembers class * {
                            @org.springframework.beans.factory.annotation.Autowired *;
                            @org.springframework.beans.factory.annotation.Value *;
                            }
                        </option>
                        <option>-keep public class org.foo.**</option>
                        <option>-keep public class org.springframework.**</option>
                    </options>
                    <libs>
                        <lib>${java.home}/lib/rt.jar</lib>
                    </libs>
                    <injarNotExistsSkip>true</injarNotExistsSkip>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>net.sf.proguard</groupId>
                        <artifactId>proguard-base</artifactId>
                        <version>5.2.1</version>
                        <scope>runtime</scope>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>
</project>

我找到了一些提示here,但没有提及任何有关嵌入式 servlet 容器的任何问题。

我怀疑我在此过程中做错了什么,我在网络上找不到任何关于此特定问题的可靠工作示例/提示。

如果你们对如何解决这个问题有任何建议,我都会听到 :)

【问题讨论】:

  • 您找到解决方案了吗?
  • 我现在在使用 spring boot、gradle 和 proguard 时遇到了同样的问题。你有没有找到解决方案?

标签: java spring maven tomcat obfuscation


【解决方案1】:

我也遇到了同样的问题。

事实上,这个问题不是由“嵌入式 servlet 容器”引起的。我认为这是因为 Proguard 在 spring-boot 重新打包之前混淆了一些类或接口。但不知何故,spring boot-info 中的类/接口信息(如名称或路径)没有相应地修改。因此 spring boot 无法初始化 bean 并且无法启动应用程序。

我通过简单地将“-keepattributes xxx”添加到我的 proguard.conf 文件中来修复它。

因为您的配置中已经有了“-keepattributes”。我建议你通过使用'-keep class !xxx'来缩小受保护文件的范围,它可能会帮助你找出导致问题的人。

我的程序配置:

-target 1.8
-dontshrink
-dontoptimize
-dontnote
-keepattributes Exceptions,InnerClasses,Signature,Deprecated,*Annotation*,EnclosingMethod
-keep class !com.hbyuan27.demo.service.DemoService { *; }
#-keep class !com.hbyuan27.demo.**.service.** { *; }

【讨论】:

    【解决方案2】:

    尝试改变插件的顺序,所以先给 proguard 插件,然后再给 spring-boot-maven-plugin 和 repckage。对我来说,这会很好。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-05-28
      • 2017-03-12
      • 2018-06-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-16
      相关资源
      最近更新 更多