2015-08-28 144 views
0

我想使用BouncyCastle API(v 1.52)在WildFly服務器上的Web應用程序中使用PBKDF2WithHmacSHA1-alogrithm對密碼進行哈希處理。但總是當我部署我的應用程序時,我從服務器上得到一個「ClassNotFoundException org.bouncycastle.crypto.PBEParametersGenerator」。我使用Eclipse Mars和WildFly 8.2.0和9.0.1。在Eclipse中我的項目中沒有出現任何錯誤。我已經嘗試將BouncyCastle JAR添加到我的Classpath中,如另一個主題中所述,但它沒有幫助。我想知道爲什麼我在這裏或谷歌找不到任何其他結果關於這個問題,任何人都可以幫助我嗎?我知道Java 8有一個PBKDF2WithHmacSHA256實現,但我想用BouncyCastle API作爲替代。BouncyCastle將ClassNotFoundException部署到WildFly

這裏是產生所描述的錯誤項目的一個非常簡單的例子:

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> 

回答

3

BouncyCastle庫作爲JBoss模塊包含在WildFly發行版中。

請嘗試將org.bouncycastle模塊導入您的應用程序,並確保您的WAR中不包含BouncyCastle庫的副本,例如,在您的POM中使用provided作用域用於BouncyCastle依賴關係。

有關導入模塊的更多詳細信息,請參見Classloading in WildFly

+0

項目正在建設中,但我仍然在部署中遇到同樣的錯誤。我使用的這個相關性:' \t \t \t org.bouncycastle \t \t \t bcprov-jdk16 \t \t \t 1.46 \t \t \t 提供 \t \t' – Benny