BouncyCastle NoSuchProviderException 即使它是 Maven 依赖项
BouncyCastle NoSuchProviderException even though it's a Maven dependency
我正在尝试使用 Wildfly Swarm 构建一个现有的 JavaEE 项目,但我将 运行 保留在我的一个库中的一个问题中。它应该从服务器加载 PEM 格式的 public 密钥,并使用它来验证签名。但是,我一直收到这个:
2017-06-08 20:55:59,229 ERROR [stderr] (default task-3) java.security.NoSuchProviderException: no such provider: BC
2017-06-08 20:55:59,234 ERROR [stderr] (default task-3) at sun.security.jca.GetInstance.getService(GetInstance.java:83)
2017-06-08 20:55:59,238 ERROR [stderr] (default task-3) at sun.security.jca.GetInstance.getInstance(GetInstance.java:206)
2017-06-08 20:55:59,238 ERROR [stderr] (default task-3) at java.security.KeyFactory.getInstance(KeyFactory.java:211)
2017-06-08 20:55:59,239 ERROR [stderr] (default task-3) at enterprises.mccollum.wmapp.ssauthclient.PublicKeySingleton.loadPubKey(PublicKeySingleton.java:83)
2017-06-08 20:55:59,239 ERROR [stderr] (default task-3) at enterprises.mccollum.wmapp.ssauthclient.PublicKeySingleton.init(PublicKeySingleton.java:57)
导致问题的代码在这里:
PublicKeySingleton.java snippet:
81: PemObject pemPubKey = ldPemFromServer();
82: if(pemPubKey != null){
83: KeyFactory kf = KeyFactory.getInstance("RSA", BouncyCastleProvider.PROVIDER_NAME);
84: PublicKey lPubKey = kf.generatePublic(new X509EncodedKeySpec(pemPubKey.getContent()));
85: Logger.getLogger(SSAuthClient.SUBSYSTEM_NAME).log(Level.INFO, "Read public key from url successfully");
86: return lPubKey;
这是使用上述代码的库的 pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>enterprises.mccollum.wmapp</groupId>
<artifactId>ssauthclient</artifactId>
<version>1.0.5-SNAPSHOT</version>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<failOnMissingWebXml>false</failOnMissingWebXml>
</properties>
<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>enterprises.mccollum.utils</groupId>
<artifactId>genericentityejb</artifactId>
<version>1.0.5</version>
</dependency>
<dependency>
<groupId>enterprises.mccollum.jee</groupId>
<artifactId>urlutils</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15on</artifactId>
<version>1.56</version>
<!-- Tried changing the version to 1.52, as used by Swarm itself, but to no avail -->
</dependency>
</dependencies>
<build>
<finalName>ssauthclient</finalName>
<extensions>
<extension>
<groupId>org.apache.maven.wagon</groupId>
<artifactId>wagon-webdav</artifactId>
<version>1.0-beta-2</version>
</extension>
</extensions>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
这里是 Swarm 项目的 pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>ie.countries.cdn</groupId>
<artifactId>cbook</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<version.wildfly.swarm>2017.6.0</version.wildfly.swarm>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<failOnMissingWebXml>false</failOnMissingWebXml>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.wildfly.swarm</groupId>
<artifactId>bom</artifactId>
<version>${version.wildfly.swarm}</version>
<scope>import</scope>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>enterprises.mccollum.wmapp</groupId>
<artifactId>ssauthclient</artifactId>
<version>1.0.5-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.primefaces</groupId>
<artifactId>primefaces</artifactId>
<version>6.1</version>
</dependency>
<dependency>
<groupId>org.ocpsoft.rewrite</groupId>
<artifactId>rewrite-servlet</artifactId>
<version>3.4.1.Final</version>
</dependency>
<dependency>
<groupId>org.ocpsoft.rewrite</groupId>
<artifactId>rewrite-config-prettyfaces</artifactId>
<version>3.4.1.Final</version>
</dependency>
<dependency>
<groupId>org.primefaces.themes</groupId>
<artifactId>bootstrap</artifactId>
<version>1.0.10</version>
</dependency>
<dependency>
<groupId>org.omnifaces</groupId>
<artifactId>omnifaces</artifactId>
<version>2.6.2</version>
</dependency>
<!-- WildFly Swarm Fractions -->
<dependency>
<groupId>org.wildfly.swarm</groupId>
<artifactId>cdi</artifactId>
</dependency>
<dependency>
<groupId>org.wildfly.swarm</groupId>
<artifactId>ejb</artifactId>
</dependency>
<dependency>
<groupId>org.wildfly.swarm</groupId>
<artifactId>management</artifactId>
</dependency>
<dependency>
<groupId>org.wildfly.swarm</groupId>
<artifactId>jpa</artifactId>
</dependency>
<dependency>
<groupId>org.wildfly.swarm</groupId>
<artifactId>datasources</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.195</version>
</dependency>
<dependency>
<groupId>org.wildfly.swarm</groupId>
<artifactId>management-console</artifactId>
</dependency>
<dependency>
<groupId>org.wildfly.swarm</groupId>
<artifactId>cdi-config</artifactId>
</dependency>
<dependency>
<groupId>org.wildfly.swarm</groupId>
<artifactId>jsf</artifactId>
</dependency>
<dependency>
<groupId>org.wildfly.swarm</groupId>
<artifactId>jaxrs</artifactId>
</dependency>
</dependencies>
<build>
<finalName>cbook</finalName>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>META-INF/persistence.xml</include>
</includes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.wildfly.swarm</groupId>
<artifactId>wildfly-swarm-plugin</artifactId>
<version>${version.wildfly.swarm}</version>
<executions>
<execution>
<goals>
<goal>package</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
我很困惑为什么这行不通,尤其是当 mvn package
生产的 uberjar 和 war 都将 bouncycastle 提供程序依赖项作为 jar 包含时。
出了什么问题?这是 Swarm 中的一个错误,还是我错过了一个我需要做的技巧才能让它工作?
您需要安装 BouncyCastle 作为提供程序。两种方式:
首先在纯 java:
Security.addProvider(new BouncyCastleProvider());
第二种方法静态作为 java.security 文件的条目:
security.provider.N=org.bouncycastle.jce.provider.BouncyCastleProvider
你显然需要它在你的类路径中。
默认情况下,提供程序不在 JVM 中(您可以在 $JAVA_HOME/jre/lib/security/java.security
或使用 Security.getProviders()
中查看提供程序列表)。
您必须使用 Security
class:
添加它
import java.security.Security;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
Security.addProvider(new BouncyCastleProvider());
有些人更喜欢检查提供程序是否已经存在,只有在不存在时才添加:
// if provider is not present, add it
if (Security.getProvider(BouncyCastleProvider.PROVIDER_NAME) == null) {
// insert at specific position
Security.insertProviderAt(new BouncyCastleProvider(), 1);
}
上述方法的区别在于addProvider
将提供者添加到提供者列表的末尾(getProviders
返回的那个),而insertProviderAt
将其添加到指定的位置(其他的都移位了)。
另一种方法是编辑 $JAVA_HOME/jre/lib/security/java.security
文件并在所需位置添加提供商:
security.provider.2=org.bouncycastle.jce.provider.BouncyCastleProvider
可以找到有关此方法的更多详细信息 here。
我正在尝试使用 Wildfly Swarm 构建一个现有的 JavaEE 项目,但我将 运行 保留在我的一个库中的一个问题中。它应该从服务器加载 PEM 格式的 public 密钥,并使用它来验证签名。但是,我一直收到这个:
2017-06-08 20:55:59,229 ERROR [stderr] (default task-3) java.security.NoSuchProviderException: no such provider: BC
2017-06-08 20:55:59,234 ERROR [stderr] (default task-3) at sun.security.jca.GetInstance.getService(GetInstance.java:83)
2017-06-08 20:55:59,238 ERROR [stderr] (default task-3) at sun.security.jca.GetInstance.getInstance(GetInstance.java:206)
2017-06-08 20:55:59,238 ERROR [stderr] (default task-3) at java.security.KeyFactory.getInstance(KeyFactory.java:211)
2017-06-08 20:55:59,239 ERROR [stderr] (default task-3) at enterprises.mccollum.wmapp.ssauthclient.PublicKeySingleton.loadPubKey(PublicKeySingleton.java:83)
2017-06-08 20:55:59,239 ERROR [stderr] (default task-3) at enterprises.mccollum.wmapp.ssauthclient.PublicKeySingleton.init(PublicKeySingleton.java:57)
导致问题的代码在这里:
PublicKeySingleton.java snippet:
81: PemObject pemPubKey = ldPemFromServer();
82: if(pemPubKey != null){
83: KeyFactory kf = KeyFactory.getInstance("RSA", BouncyCastleProvider.PROVIDER_NAME);
84: PublicKey lPubKey = kf.generatePublic(new X509EncodedKeySpec(pemPubKey.getContent()));
85: Logger.getLogger(SSAuthClient.SUBSYSTEM_NAME).log(Level.INFO, "Read public key from url successfully");
86: return lPubKey;
这是使用上述代码的库的 pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>enterprises.mccollum.wmapp</groupId>
<artifactId>ssauthclient</artifactId>
<version>1.0.5-SNAPSHOT</version>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<failOnMissingWebXml>false</failOnMissingWebXml>
</properties>
<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>enterprises.mccollum.utils</groupId>
<artifactId>genericentityejb</artifactId>
<version>1.0.5</version>
</dependency>
<dependency>
<groupId>enterprises.mccollum.jee</groupId>
<artifactId>urlutils</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15on</artifactId>
<version>1.56</version>
<!-- Tried changing the version to 1.52, as used by Swarm itself, but to no avail -->
</dependency>
</dependencies>
<build>
<finalName>ssauthclient</finalName>
<extensions>
<extension>
<groupId>org.apache.maven.wagon</groupId>
<artifactId>wagon-webdav</artifactId>
<version>1.0-beta-2</version>
</extension>
</extensions>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
这里是 Swarm 项目的 pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>ie.countries.cdn</groupId>
<artifactId>cbook</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<version.wildfly.swarm>2017.6.0</version.wildfly.swarm>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<failOnMissingWebXml>false</failOnMissingWebXml>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.wildfly.swarm</groupId>
<artifactId>bom</artifactId>
<version>${version.wildfly.swarm}</version>
<scope>import</scope>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>enterprises.mccollum.wmapp</groupId>
<artifactId>ssauthclient</artifactId>
<version>1.0.5-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.primefaces</groupId>
<artifactId>primefaces</artifactId>
<version>6.1</version>
</dependency>
<dependency>
<groupId>org.ocpsoft.rewrite</groupId>
<artifactId>rewrite-servlet</artifactId>
<version>3.4.1.Final</version>
</dependency>
<dependency>
<groupId>org.ocpsoft.rewrite</groupId>
<artifactId>rewrite-config-prettyfaces</artifactId>
<version>3.4.1.Final</version>
</dependency>
<dependency>
<groupId>org.primefaces.themes</groupId>
<artifactId>bootstrap</artifactId>
<version>1.0.10</version>
</dependency>
<dependency>
<groupId>org.omnifaces</groupId>
<artifactId>omnifaces</artifactId>
<version>2.6.2</version>
</dependency>
<!-- WildFly Swarm Fractions -->
<dependency>
<groupId>org.wildfly.swarm</groupId>
<artifactId>cdi</artifactId>
</dependency>
<dependency>
<groupId>org.wildfly.swarm</groupId>
<artifactId>ejb</artifactId>
</dependency>
<dependency>
<groupId>org.wildfly.swarm</groupId>
<artifactId>management</artifactId>
</dependency>
<dependency>
<groupId>org.wildfly.swarm</groupId>
<artifactId>jpa</artifactId>
</dependency>
<dependency>
<groupId>org.wildfly.swarm</groupId>
<artifactId>datasources</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.195</version>
</dependency>
<dependency>
<groupId>org.wildfly.swarm</groupId>
<artifactId>management-console</artifactId>
</dependency>
<dependency>
<groupId>org.wildfly.swarm</groupId>
<artifactId>cdi-config</artifactId>
</dependency>
<dependency>
<groupId>org.wildfly.swarm</groupId>
<artifactId>jsf</artifactId>
</dependency>
<dependency>
<groupId>org.wildfly.swarm</groupId>
<artifactId>jaxrs</artifactId>
</dependency>
</dependencies>
<build>
<finalName>cbook</finalName>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>META-INF/persistence.xml</include>
</includes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.wildfly.swarm</groupId>
<artifactId>wildfly-swarm-plugin</artifactId>
<version>${version.wildfly.swarm}</version>
<executions>
<execution>
<goals>
<goal>package</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
我很困惑为什么这行不通,尤其是当 mvn package
生产的 uberjar 和 war 都将 bouncycastle 提供程序依赖项作为 jar 包含时。
出了什么问题?这是 Swarm 中的一个错误,还是我错过了一个我需要做的技巧才能让它工作?
您需要安装 BouncyCastle 作为提供程序。两种方式: 首先在纯 java:
Security.addProvider(new BouncyCastleProvider());
第二种方法静态作为 java.security 文件的条目:
security.provider.N=org.bouncycastle.jce.provider.BouncyCastleProvider
你显然需要它在你的类路径中。
默认情况下,提供程序不在 JVM 中(您可以在 $JAVA_HOME/jre/lib/security/java.security
或使用 Security.getProviders()
中查看提供程序列表)。
您必须使用 Security
class:
import java.security.Security;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
Security.addProvider(new BouncyCastleProvider());
有些人更喜欢检查提供程序是否已经存在,只有在不存在时才添加:
// if provider is not present, add it
if (Security.getProvider(BouncyCastleProvider.PROVIDER_NAME) == null) {
// insert at specific position
Security.insertProviderAt(new BouncyCastleProvider(), 1);
}
上述方法的区别在于addProvider
将提供者添加到提供者列表的末尾(getProviders
返回的那个),而insertProviderAt
将其添加到指定的位置(其他的都移位了)。
另一种方法是编辑 $JAVA_HOME/jre/lib/security/java.security
文件并在所需位置添加提供商:
security.provider.2=org.bouncycastle.jce.provider.BouncyCastleProvider
可以找到有关此方法的更多详细信息 here。