Spring 使用 proguard 生成 jar 文件后启动 returns 404
Spring Boot returns 404 after generated jar file with proguard
我有一个多模块 Maven 项目,在 Netty 下使用 Spring Boot 和 Webflux 运行。 Si 我正在使用 proguard maven 插件生成混淆的 jar 文件。
我的问题是当我查看日志时一切看起来都很好但是当我向它发送请求时我收到 404 错误。
pom.xml 处的构建部分如下所示:
<build>
<plugins>
<plugin>
<groupId>com.github.wvengen</groupId>
<artifactId>proguard-maven-plugin</artifactId>
<version>2.5.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>proguard</goal>
</goals>
</execution>
</executions>
<configuration>
<proguardVersion>6.2.2</proguardVersion>
<injar>${project.build.finalName}.jar</injar>
<outjar>${project.build.finalName}.jar</outjar>
<obfuscate>true</obfuscate>
<proguardInclude>proguard.cfg</proguardInclude>
</configuration>
<dependencies>
<dependency>
<groupId>net.sf.proguard</groupId>
<artifactId>proguard-base</artifactId>
<version>6.2.2</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<mainClass>com.dummy.test.MainClass</mainClass>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
而 proguard.cfg 看起来像:
-dontshrink
-dontoptimize
-useuniqueclassmembernames
-adaptclassstrings
-keep public class * extends org.springframework.boot.web.support.SpringBootServletInitializer
-keep public class * extends org.springframework.boot.loader.**
-keepclasseswithmembers public class * { public static void main(java.lang.String[]);}
-keepclassmembers enum * { *; }
-keepclassmembers class * {
@org.springframework.beans.factory.annotation.Autowired *;
@org.springframework.beans.factory.annotation.Qualifier *;
@org.springframework.beans.factory.annotation.Value *;
@org.springframework.beans.factory.annotation.Required *;
@org.springframework.context.annotation.Bean *;
@org.springframework.context.annotation.Primary *;
@org.springframework.boot.context.properties.ConfigurationProperties *;
@org.springframework.boot.context.properties.EnableConfigurationProperties *;
@javax.inject.Inject *;
}
-keep @org.springframework.cache.annotation.EnableCaching class *
-keep @org.springframework.context.annotation.Configuration class *
-keep @org.springframework.boot.context.properties.ConfigurationProperties class *
-keep @org.springframework.boot.autoconfigure.SpringBootApplication class *
-keep @org.springframework.boot.autoconfigure.data.redis.RedisReactiveAutoConfiguration class *
-keep @org.springframework.stereotype.Repository class *
-allowaccessmodification
-keepattributes Exceptions,InnerClasses,Signature,Deprecated,SourceFile,LineNumberTable,*Annotation*,EnclosingMethod,RuntimeVisibleAnnotations
-keepdirectories org.springframework.boot.autoconfigure
-keepnames class * implements java.io.Serializable
-keepclassmembers class * {
@org.springframework.beans.factory.annotation.Autowired *;
}
## keep same
-keepclassmembernames class com.dummy.test.entity.** { *; }
## keep getters and setters for mail template
-keepclassmembers class * {
*** get*();
void set*(***);
}
原来,Proguard把所有的混淆文件都移到了BOOT-INF下
Spring 启动扫描主包但它找不到你的混淆文件,因为它们不在主包中。所以你需要给 Proguard 一个正确的包名,以便将它们移动到主包下的某个地方。
您需要做的就是给一个新的包名,例如:
-repackageclasses com.foo.bar.obfuscated
我有一个多模块 Maven 项目,在 Netty 下使用 Spring Boot 和 Webflux 运行。 Si 我正在使用 proguard maven 插件生成混淆的 jar 文件。 我的问题是当我查看日志时一切看起来都很好但是当我向它发送请求时我收到 404 错误。
pom.xml 处的构建部分如下所示:
<build>
<plugins>
<plugin>
<groupId>com.github.wvengen</groupId>
<artifactId>proguard-maven-plugin</artifactId>
<version>2.5.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>proguard</goal>
</goals>
</execution>
</executions>
<configuration>
<proguardVersion>6.2.2</proguardVersion>
<injar>${project.build.finalName}.jar</injar>
<outjar>${project.build.finalName}.jar</outjar>
<obfuscate>true</obfuscate>
<proguardInclude>proguard.cfg</proguardInclude>
</configuration>
<dependencies>
<dependency>
<groupId>net.sf.proguard</groupId>
<artifactId>proguard-base</artifactId>
<version>6.2.2</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<mainClass>com.dummy.test.MainClass</mainClass>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
而 proguard.cfg 看起来像:
-dontshrink
-dontoptimize
-useuniqueclassmembernames
-adaptclassstrings
-keep public class * extends org.springframework.boot.web.support.SpringBootServletInitializer
-keep public class * extends org.springframework.boot.loader.**
-keepclasseswithmembers public class * { public static void main(java.lang.String[]);}
-keepclassmembers enum * { *; }
-keepclassmembers class * {
@org.springframework.beans.factory.annotation.Autowired *;
@org.springframework.beans.factory.annotation.Qualifier *;
@org.springframework.beans.factory.annotation.Value *;
@org.springframework.beans.factory.annotation.Required *;
@org.springframework.context.annotation.Bean *;
@org.springframework.context.annotation.Primary *;
@org.springframework.boot.context.properties.ConfigurationProperties *;
@org.springframework.boot.context.properties.EnableConfigurationProperties *;
@javax.inject.Inject *;
}
-keep @org.springframework.cache.annotation.EnableCaching class *
-keep @org.springframework.context.annotation.Configuration class *
-keep @org.springframework.boot.context.properties.ConfigurationProperties class *
-keep @org.springframework.boot.autoconfigure.SpringBootApplication class *
-keep @org.springframework.boot.autoconfigure.data.redis.RedisReactiveAutoConfiguration class *
-keep @org.springframework.stereotype.Repository class *
-allowaccessmodification
-keepattributes Exceptions,InnerClasses,Signature,Deprecated,SourceFile,LineNumberTable,*Annotation*,EnclosingMethod,RuntimeVisibleAnnotations
-keepdirectories org.springframework.boot.autoconfigure
-keepnames class * implements java.io.Serializable
-keepclassmembers class * {
@org.springframework.beans.factory.annotation.Autowired *;
}
## keep same
-keepclassmembernames class com.dummy.test.entity.** { *; }
## keep getters and setters for mail template
-keepclassmembers class * {
*** get*();
void set*(***);
}
原来,Proguard把所有的混淆文件都移到了BOOT-INF下
Spring 启动扫描主包但它找不到你的混淆文件,因为它们不在主包中。所以你需要给 Proguard 一个正确的包名,以便将它们移动到主包下的某个地方。
您需要做的就是给一个新的包名,例如:
-repackageclasses com.foo.bar.obfuscated