我的java程序被打包在一个jar文件中,并使用了一个外部jar库,bouncy castle。我的代码编译良好,但运行jar会导致以下错误:
java.lang.SecurityException: Manifest主属性的签名文件摘要无效
我在谷歌上搜索了一个多小时,想要找到一个解释,但几乎没有什么价值。如果有人看到这个错误之前,可以提供一些帮助,我将不胜感激。
我的java程序被打包在一个jar文件中,并使用了一个外部jar库,bouncy castle。我的代码编译良好,但运行jar会导致以下错误:
java.lang.SecurityException: Manifest主属性的签名文件摘要无效
我在谷歌上搜索了一个多小时,想要找到一个解释,但几乎没有什么价值。如果有人看到这个错误之前,可以提供一些帮助,我将不胜感激。
当前回答
安全性已经是一个棘手的话题,但我很失望地看到最流行的解决方案是删除安全性签名。JCE需要这些签名。Maven阴影会爆炸将签名放入META-INF的BouncyCastle jar文件,但BouncyCastle签名对新的超级jar无效(仅对BC jar有效),这就是导致此线程中出现无效签名错误的原因。
是的,排除或删除@ruhsuzbaykus所建议的签名确实可以消除最初的错误,但它也可能导致新的、神秘的错误:
java.security.NoSuchAlgorithmException: PBEWithSHA256And256BitAES-CBC-BC SecretKeyFactory not available
通过显式指定查找算法的位置,如下所示:
SecretKeyFactory.getInstance("PBEWithSHA256And256BitAES-CBC-BC","BC");
我得到了一个不同的错误:
java.security.NoSuchProviderException: JCE cannot authenticate the provider BC
JCE无法对提供者进行身份验证,因为我们已经按照同一线程中其他地方的建议删除了加密签名。
我找到的解决方案是可执行的封装器插件,它使用jar-in-jar的方法将BouncyCastle签名保存在单个可执行的jar中。
更新:
另一种方法(正确的方法?)是使用Maven Jar签名器。这允许您继续使用Maven shade而不会出现安全错误。但是,你必须有一个代码签名证书(Oracle建议搜索“Java代码签名证书”)。POM配置如下所示:
<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>org.bouncycastle:*</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>your.class.here</mainClass>
</transformer>
</transformers>
<shadedArtifactAttached>true</shadedArtifactAttached>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jarsigner-plugin</artifactId>
<version>1.4</version>
<executions>
<execution>
<id>sign</id>
<goals>
<goal>sign</goal>
</goals>
</execution>
<execution>
<id>verify</id>
<goals>
<goal>verify</goal>
</goals>
</execution>
</executions>
<configuration>
<keystore>/path/to/myKeystore</keystore>
<alias>myfirstkey</alias>
<storepass>111111</storepass>
<keypass>111111</keypass>
</configuration>
</plugin>
不,没有办法让JCE承认自签名的证书,所以如果你需要保留BouncyCastle证书,你必须使用jar-in-jar插件或获得JCE证书。
其他回答
I've recently started using IntelliJ on my projects. However, some of my colleagues still use Eclipse on the same projects. Today, I've got the very same error after executing the jar-file created by my IntelliJ. While all the solutions in here talking about almost the same thing, none of them worked for me easily (possibly because I don't use ANT, maven build gave me other errors which referred me to http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException, and also I couldn't figure out what are the signed jars by myself!)
最后,这帮助了我
zip -d demoSampler.jar 'META-INF/*.SF' 'META-INF/*.RSA' 'META-INF/*SF'
猜猜我的jar文件里删除了什么!
deleting: META-INF/ECLIPSE_.SF
deleting: META-INF/ECLIPSE_.RSA
这个问题似乎与一些与eclipse相关的文件有关。
我在使用IntelliJ IDEA 14.01时遇到了这个问题。
我可以通过以下方法修复它:
文件->项目结构->添加新的(工件)->jar->从模块与依赖创建jar从模块窗口:
选择你的主要类
来自库的JAR文件 选择复制到输出目录和链接通过清单
一种策略包括使用ANT来简化从每个Jar文件中删除签名。它将采取下列步骤:
复制舱单。MF在临时文件中 从临时文件中删除Name和SHA条目 用临时清单创建一个临时Jar文件 移除临时舱单 将原始Jar文件与临时Jar文件交换
下面是一个ANT宏def做的工作:
<macrodef name="unsignjar" description="To unsign a specific Jar file">
<attribute name="jarfile"
description="The jar file to unsign" />
<sequential>
<!-- Copying to the temporary manifest file -->
<copy toFile="@{jarFile}_MANIFEST.tmp">
<resources>
<zipentry zipfile="@{jarFile}" name="META-INF/MANIFEST.MF"/>
</resources>
</copy>
<!-- Removing the Name and SHA entries from the temporary file -->
<replaceregexp file="@{jarFile}_MANIFEST.tmp" match="\nName:(.+?)\nSH" replace="SH" flags="gis" byline="false"/>
<replaceregexp file="@{jarFile}_MANIFEST.tmp" match="SHA(.*)" replace="" flags="gis" byline="false"/>
<!-- Creating a temporary Jar file with the temporary manifest -->
<jar jarfile="@{jarFile}.tmp"
manifest="@{jarFile}_MANIFEST.tmp">
<zipfileset src="@{jarFile}">
<include name="**"/>
<exclude name="META-INF/*.SF"/>
<exclude name="META-INF/*.DSA"/>
<exclude name="META-INF/*.RSA"/>
</zipfileset>
</jar>
<!-- Removing the temporary manifest -->
<delete file="@{jarFile}_MANIFEST.tmp" />
<!-- Swapping the original Jar file with the temporary one -->
<move file="@{jarFile}.tmp"
tofile="@{jarFile}"
overwrite="true" />
</sequential>
`
在ANT任务中可以这样调用定义:
<target name="unsignJar">
<unsignjar jarFile="org.test.myjartounsign.jar" />
</target>
这里列出的解决方案可能提供一个指针。
清单主属性的签名文件摘要无效
底线:
最好把官方的罐子保存为 只是将其作为依赖项添加到您的manifest文件中 应用程序jar文件。
如果你正在使用gradle,这里有一个完整的farJar任务:
version = '1.0'
//create a single Jar with all dependencies
task fatJar(type: Jar) {
manifest {
attributes 'Implementation-Title': 'Gradle Jar File Example',
'Implementation-Version': version,
'Main-Class': 'com.example.main'
}
baseName = project.name + '-all'
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
exclude 'META-INF/*.RSA', 'META-INF/*.SF','META-INF/*.DSA'
with jar
}