Bouncy Castle Security提供程序未在Java 11/12下加载 [英] Bouncy castle security provider doesn't load under Java 11/12
问题描述
我正在尝试做一些琐碎的事情,但是我遇到了一个问题.
I'm trying to do something that's supposed to be trivial but there is a problem that I'm facing.
在将JavaFx应用程序从Java 8迁移到Java 11后(同样尝试过Java 12),将不会加载有弹性的城堡安全提供程序. 我更新了一个Maven项目,以使用兼容Java 11的插件.编译正常,但是在运行时我在终端窗口中看到了这一点:
The bouncy castle security provider won't load when after migrating a JavaFx application from Java 8 to Java 11 (tried Java 12 as well, same results). It's a Maven project that I updated to use Java 11 compliant plugins. It compiles fine but when running I get this in the terminal window:
jar .Launcher java.lang.reflect.InvocationTargetException在 java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(本机 方法) java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 在 java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 在java.base/java.lang.reflect.Method.invoke(Method.java:567)在 javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:464) 在 javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:363) 在 java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(本机 方法) java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 在 java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 在java.base/java.lang.reflect.Method.invoke(Method.java:567)在 java.base/sun.launcher.LauncherHelper $ FXHelper.main(LauncherHelper.java:1051) 块引用 原因:java.lang.NoClassDefFoundError: org/bouncycastle/jce/provider/BouncyCastleProvider 在 .Launcher.main(Launcher.java:14)... 另外11个原因:java.lang.ClassNotFoundException: org.bouncycastle.jce.provider.BouncyCastleProvider位于 java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:583) 在 java.base/jdk.internal.loader.ClassLoaders $ AppClassLoader.loadClass(ClassLoaders.java:178) 在java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521) ...另外12个
jar .Launcher java.lang.reflect.InvocationTargetException at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:567) at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:464) at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:363) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:567) at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1051) Blockquote Caused by: java.lang.NoClassDefFoundError: org/bouncycastle/jce/provider/BouncyCastleProvider at .Launcher.main(Launcher.java:14) ... 11 more Caused by: java.lang.ClassNotFoundException: org.bouncycastle.jce.provider.BouncyCastleProvider at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:583) at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178) at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521) ... 12 more
在我的java.security中,我将有弹性的城堡提供程序作为第一个提供程序(也尝试将其作为最后一个提供程序,但结果相同)
In my java.security I have the bouncy castle provider as the first provider (also tried it as the last provider but the results are the same)
#
# List of providers and their preference orders (see above):
#
security.provider.1=org.bouncycastle.jce.provider.BouncyCastleProvider
security.provider.2=SUN
security.provider.3=SunRsaSign
security.provider.4=SunEC
security.provider.5=SunJSSE
security.provider.6=SunJCE
security.provider.7=SunJGSS
security.provider.8=SunSASL
security.provider.9=XMLDSig
security.provider.10=SunPCSC
security.provider.11=JdkLDAP
security.provider.10=JdkSASL
security.provider.11=Apple
security.provider.12=SunPKCS11
在我的代码中有以下内容(过去测试从Java 8升级到9或10时效果很好):
And in my code have the following (which worked fine when I tested upgrading from java 8 to 9 or 10 in the past):
//Since Java 9 we set the unlimited crypto policy in code, not by applying the JCE jars.
Security.setProperty("crypto.policy", "unlimited");
//verify that JCE is applied
// init the BC security provider
if (Security.getProvider("BC") == null) {
Security.insertProviderAt(new BouncyCastleProvider(), 0);
logger.info("Security provider added successfully");
}
充气城堡罐为bcprov-jdk15on-1.61(Bouncy城堡提供程序版本1.61)和bcpkix-jdk15on-1.61.jar.居住在正确的地方.
The Bouncy castle jars are bcprov-jdk15on-1.61 (Bouncy castle provider version 1.61) and bcpkix-jdk15on-1.61.jar. The reside in the right place.
我用来启动JavaFx应用程序的命令行是
the command line I use to launch the JavaFx application is
java -cp lib --module-path mods --add-modules = javafx.controls,javafx.fxml,javafx.graphics,javafx.web,javafx.swing -jar .Launcher 并确保将mods和libs放在正确的位置,并且罐子可以访问它们.
java -cp lib --module-path mods --add-modules=javafx.controls,javafx.fxml,javafx.graphics,javafx.web,javafx.swing -jar .Launcher And ensured that the mods and libs are in the right place and accessible to the jar.
我在StackOverflow.com中看到了几个与此类似的问题,但是尝试了提供的解决方案,但这些问题无法解决.
关于为何不加载Bouncy城堡提供商以及如何解决它的任何想法?
Any ideas as to why the Bouncy castle provider won't load and how to resolve it?
推荐答案
我认为您应该(如果尚未完成)使用BouncyCastle 1.66重试:它是具有
I think you should (if not already done) retry with BouncyCastle 1.66: it is a Multi Release JAR with a module-info which could help.
我使用的依赖项:
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15on</artifactId>
<version>1.66</version>
</dependency>
您可以使用-Djava.security.debug=jca
为JCA启用调试模式(请参见安全性疑难解答).
You can enable debug mode for JCA using -Djava.security.debug=jca
(see Troubleshooting Security).
为了测试,我只添加了一行:
For my test, I simply added a new line:
security.provider.14=org.bouncycastle.jce.provider.BouncyCastleProvider
在我的AdoptOpenJDK 11安装的${JAVA_HOME}/conf/security/java.security
中. BouncyCastle由Maven作为scope = compile依赖项提供.
In the ${JAVA_HOME}/conf/security/java.security
of my AdoptOpenJDK 11 installation. The BouncyCastle was provided by Maven as scope=compile dependency.
这个简单的代码:
for (final Provider provider : Security.getProviders()) {
System.out.println("provider: " + provider.getName());
}
告诉我BC
已加载.
此外,如果您需要强制BouncyCastle作为第一提供商,您将在module-info.java
中要求 org.bouncycastle.provider
:
Also, if you ever need to force BouncyCastle as first provider, you will require org.bouncycastle.provider
in your module-info.java
:
顺便说一句,您的代码是错误的:它不会先插入BC,而是最后插入:
By the way, your code is wrong: it won't insert BC first, but last:
if (Security.getProvider("BC") == null) {
Security.insertProviderAt(new BouncyCastleProvider(), 0);
logger.info("Security provider added successfully");
}
在指定位置添加新的提供程序.该职位是
优先顺序,在该顺序中搜索提供者以请求
算法. 该职位是基于1的,也就是说,最优先选择1,
其次是2,依此类推.
Adds a new provider, at a specified position. The position is the
preference order in which providers are searched for requested
algorithms. The position is 1-based, that is, 1 is most preferred,
followed by 2, and so on. 如果传递0(与调用 If you pass 0, which is the same as calling 这篇关于Bouncy Castle Security提供程序未在Java 11/12下加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
Security.addProvider(Provider)
相同),则提供程序将排在最后(请参阅Security.addProvider(Provider)
, your provider will go at the end (see ProviderList).