首页 开发教程 java.lang.SecurityException异常

java.lang.SecurityException异常

开发教程 2025年12月4日
525 浏览

记录一个问题处理
通常发生在程序尝试加载或验证JAR文件时,其数字签名与文件内容不匹配。这多见于打包(如创建包含依赖的Fat JAR)或运行环境存在问题。

异常信息

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)

报错路径:

image.png

报错原因:

Fat JAR打包:使用Maven Shade Plugin等工具创建包含所有依赖的\”Fat JAR\”时,如果多个依赖JAR都带有签名文件,这些文件可能会被错误地合并或包含进最终的JAR,导致签名冲突和验证失败

解决方案:

如果你的项目使用Maven构建,在pom.xml文件中配置maven-shade-plugin是根除此问题的最佳方法。在插件的配置中,排除所有依赖中的签名相关文件:

<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>
                  
                  <exclude>META-INF/*.SF</exclude>
                  <exclude>META-INF/*.DSA</exclude>
                  <exclude>META-INF/*.RSA</exclude>
                </excludes>
              </filter>
            </filters>
            
            <transformers>
              <transformer implementation=\"org.apache.maven.plugins.shade.resource.ManifestResourceTransformer\">
                <mainClass>你的主类的完整路径</mainClass>
              </transformer>
            </transformers>
          </configuration>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>

发表评论
暂无评论

还没有评论呢,快来抢沙发~

客服

点击联系客服 点击联系客服

在线时间:09:00-18:00

关注微信公众号

关注微信公众号
客服电话

400-888-8888

客服邮箱 122325244@qq.com

手机

扫描二维码

手机访问本站

扫描二维码
搜索