记录一个问题处理
通常发生在程序尝试加载或验证JAR文件时,其数字签名与文件内容不匹配。这多见于打包(如创建包含依赖的Fat JAR)或运行环境存在问题。
异常信息
java
ava.lang.SecurityException: Invalid signature file digest for Manifest main attributes
at sun.security.util.SignatureFileVerifier.processImpl (SignatureFileVerifier.java:330)
at sun.security.util.SignatureFileVerifier.process (SignatureFileVerifier.java:263)
at java.util.jar.JarVerifier.processEntry (JarVerifier.java:318)
报错路径:

报错原因:
Fat JAR打包:使用Maven Shade Plugin等工具创建包含所有依赖的"Fat JAR"时,如果多个依赖JAR都带有签名文件,这些文件可能会被错误地合并或包含进最终的JAR,导致签名冲突和验证失败
解决方案:
如果你的项目使用Maven构建,在pom.xml
文件中配置maven-shade-plugin是根除此问题的最佳方法。在插件的配置中,排除所有依赖中的签名相关文件:
xml
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.1.0</version> <!-- 建议使用最新版本 -->
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<!-- 排除所有依赖JAR中的签名文件 -->
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
<!-- 如果你的JAR是可执行的,别忘了指定主类 -->
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>你的主类的完整路径</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>