java 移动到资源文件夹时生成的证书停止工作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17298126/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
generated certificate stops working when moved to resources folder
提问by Kevin R
I'm having problems with a generated certificate I'm using to connect to the apple push services. All works fine when the generated p12 file was in my src/main/java folder, but I moved it to src/main/resources and it decided to stop working with the following error:
我在使用生成的证书连接到苹果推送服务时遇到问题。当生成的 p12 文件位于我的 src/main/java 文件夹中时,一切正常,但我将其移至 src/main/resources 并决定停止工作,并出现以下错误:
DerInputStream.getLength(): lengthTag=111, too big.
To get into some more detail: I'm using the notnoop push notifications library and followed the tutorial from Ray Wenderlich to generate the certificates. after, I used to following commands to generate a p12 file for use in java:
要了解更多细节:我正在使用 notnoop 推送通知库,并按照 Ray Wenderlich 的教程生成证书。之后,我习惯于按照以下命令生成一个在java中使用的p12文件:
openssl x509 -in aps_development.cer -inform DER -out aps_development.pem -outform PEM
openssl pkcs12 -nocerts -in single.p12 -out single.pem
openssl pkcs12 -export -inkey single.pem -in aps_development.pem -out dual.p12
after that I moved the dual.p12 into my java project. At first the file was in my /src/main/java folder, lets say at com.company.push.certificates
(while the code requesting the file is at com.company.push
). I request an inputstream by using
之后,我将 dual.p12 移到了我的 java 项目中。起初该文件在我的 /src/main/java 文件夹中,让我们说 at com.company.push.certificates
(而请求文件的代码在com.company.push
)。我通过使用请求输入流
InputStream stream = this.getClass().getResourceAsStream("certificates/dual.p12");
This working fine in development, but not when building the project (using maven), thats why I moved the resource to the resources folder, using the exact same package.
The resource still gets found, but now I get the above-mentioned java.io.IOException
这在开发中工作正常,但在构建项目(使用 maven)时不行,这就是我使用完全相同的包将资源移动到资源文件夹的原因。资源仍然被找到,但现在我得到了上面提到的java.io.IOException
Anyone knows what might be causing this?
有谁知道这可能是什么原因造成的?
Ps: when I move the file back into the package in src/main/java, all works fine again, so the certificate seems to be valid.
Ps:当我将文件移回 src/main/java 中的包时,一切又正常了,所以证书似乎是有效的。
回答by chris
This is happening because maven's resource filtering is corrupting your p12 file.
发生这种情况是因为 maven 的资源过滤正在破坏您的 p12 文件。
We solved this by excluding p12 files from maven resource filtering with this in pom.xml:
我们通过在pom.xml 中从 maven 资源过滤中排除 p12 文件解决了这个问题:
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<excludes>
<exclude>**/*.p12</exclude>
</excludes>
</resource>
<resource>
<directory>src/main/resources</directory>
<filtering>false</filtering>
<includes>
<include>**/*.p12</include>
</includes>
</resource>
</resources>
回答by Keith
Check the content of the certificate file AFTER a build, when maven has copied to to target/classes/... Probably maven resource filtering, or something else in your build is modifying the file. Verify what ends up in the classes folder is EXACTLY the same as what is in the resources folder.
在构建后检查证书文件的内容,当 maven 已复制到目标/类/...可能是 maven 资源过滤,或者构建中的其他内容正在修改文件。验证类文件夹中的内容与资源文件夹中的内容完全相同。
http://maven.apache.org/plugins/maven-resources-plugin/examples/filter.html
http://maven.apache.org/plugins/maven-resources-plugin/examples/filter.html
回答by joensson
Personally I don't like duplicating the excludes/includes section in the maven build and having two resource sections that must be kept in sync - so I prefer to use the maven resources plugin to configure the nonFilteredFileExtensions instead.
我个人不喜欢在 maven 构建中复制 excludes/includes 部分,并且有两个必须保持同步的资源部分 - 所以我更喜欢使用 maven 资源插件来配置 nonFilteredFileExtensions。
The resources section in the build section then simply is (plus any specific includes you might need):
构建部分中的资源部分就是(加上您可能需要的任何特定包含):
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
Tell the maven-resources-plugin which files NOT to filter when including:
告诉 maven-resources-plugin 在包含以下文件时不要过滤哪些文件:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.7</version>
<configuration>
<delimiters>
<delimiter>@</delimiter>
</delimiters>
<nonFilteredFileExtensions>
<nonFilteredFileExtension>p12</nonFilteredFileExtension>
<nonFilteredFileExtension>pfx</nonFilteredFileExtension>
<nonFilteredFileExtension>pem</nonFilteredFileExtension>
</nonFilteredFileExtensions>
</configuration>
</plugin>
See: http://maven.apache.org/plugins/maven-resources-plugin/examples/binaries-filtering.html
请参阅:http: //maven.apache.org/plugins/maven-resources-plugin/examples/binaries-filtering.html