java - 部署到 WildFly 时出现 BouncyCaSTLe ClassNotFoundException

标签 java eclipse wildfly bouncycastle classnotfoundexception

我想在 WildFly 服务器上的 Web 应用程序中使用 BouncyCaSTLe API(v 1.52)通过 PBKDF2WithHmacSHA1-alogrithm 对密码进行哈希处理。但当我部署应用程序时,我总是从服务器收到“ClassNotFoundException org.bouncycaSTLe.crypto.PBEParametersGenerator”。我正在使用 Eclipse Mars 和 WildFly 8.2.0 和 9.0.1。我在 Eclipse 中的项目中没有收到任何错误。我已经尝试将 BouncyCaSTLe JAR 添加到我的类路径中,如此处另一个主题中所述,但没有帮助。我想知道为什么我在这里或谷歌上找不到关于这个问题的任何其他结果,有人可以帮助我吗?我知道 Java 8 有 PBKDF2WithHmacSHA256 实现,但我仍然想使用 BouncyCaSTLe API 作为替代方案。

这是一个非常简单的项目示例,该项目会产生所描述的错误:

BouncyCaSTLeHasher.java:

import java.util.Arrays;
import java.util.Base64;

import javax.faces.bean.ManagedBean;
import org.bouncycastle.crypto.PBEParametersGenerator;
import org.bouncycastle.crypto.generators.PKCS5S2ParametersGenerator;
import org.bouncycastle.crypto.params.KeyParameter;

@ManagedBean
public class BouncyCastleHasher {
    private String input;
    private String output;

    public String hash() {
        if(input!=null) {
            byte[] salt = "12345678".getBytes();
            PBEParametersGenerator generator = new PKCS5S2ParametersGenerator();
            generator.init(PBEParametersGenerator.PKCS5PasswordToUTF8Bytes(input.toCharArray()), salt, 1);
            KeyParameter params = (KeyParameter)generator.generateDerivedParameters(128);
            byte[] hash = Arrays.toString(params.getKey()).getBytes();
            String encodedText = Base64.getEncoder().encodeToString(hash);
            setOutput(encodedText);
        }
        return "out";
    }

    public String getInput() {
        return input;
    }

    public void setInput(String input) {
        this.input = input;
    }

    public String getOutput() {
        return output;
    }

    public void setOutput(String output) {
        this.output = output;
    }
}

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>Test_BouncyCastle</display-name>
  <welcome-file-list>
    <welcome-file>in.xhtml</welcome-file>
  </welcome-file-list>
</web-app>

in.xhtml:

<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml"
    xmlns:f="http://xmlns.jcp.org/jsf/core"
    xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
    xmlns:a="http://xmlns.jcp.org/jsf/passthrough"
    xmlns:h="http://xmlns.jcp.org/jsf/html">
<h:head>
    <title>Hashing site</title>
</h:head>
<h:body>
    <h:form>
        <table>
            <tr>
                <td>
                    <h:outputText value="Text to hash:" />
                </td>
                <td>
                    <h:inputText value="#{bouncyCastleHasher.input}" ></h:inputText>
                </td>
            </tr>
            <tr>
                <td>
                    <h:commandButton value="Save"
                        action="#{bouncyCastleHasher.hash}"></h:commandButton>
                </td>
            </tr>
        </table>
    </h:form>
</h:body>
</html>

out.xhtml:

<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml"
    xmlns:f="http://xmlns.jcp.org/jsf/core"
    xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
    xmlns:a="http://xmlns.jcp.org/jsf/passthrough"
    xmlns:h="http://xmlns.jcp.org/jsf/html">
<h:head>
    <title>Result</title>
</h:head>
<h:body>
    <h:form>
        <table>
            <tr>
                <td>
                    <h:outputText value="hashed text:" />
                </td>
                <td>
                    <h:outputText value="#{bouncyCastleHasher.output}"></h:outputText>
                </td>
            </tr>
        </table>
    </h:form>
    <h:link outcome="in"/>
</h:body>
</html>

最佳答案

BouncyCaSTLe 库作为 JBoss 模块包含在 WildFly 发行版中。

请尝试将 org.bouncycaSTLe 模块导入到您的应用程序中,并确保您的 WAR 中不包含 BouncyCaSTLe 库的副本,例如通过在 POM 中为 BouncyCaSTLe 依赖项使用 provided 范围。

参见Classloading in WildFly有关导入模块的更多详细信息。

关于java - 部署到 WildFly 时出现 BouncyCaSTLe ClassNotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32269231/

相关文章:

java - 无法将日期转换为天数

java - 无法在Azure上设置hadoop中的映射器数量

java - bash : ./eclipse:无法执行二进制文件

java - @Dependent 范围在 Wildfly 中不是默认的吗?

java - Wildfly:org.jboss.as.controller.registry.FastCopyHashMap

java - WebDriverEventListener 中的 onException 何时触发?

java - 在处理 Java 类时如何从 XML DB 切换到 MySQL 表

multithreading - Eclipse 的多线程调试器

java - 方面j : deploying aspects in an existing web application

jdbc - 在 WildFly 中将 JDBC 驱动程序作为模块安装有什么好处