Hi,
 
I write some sample code to test signing a SOAP Envelope with UsernameToken using WSS4J 1.5. The following is the sample code.
But this code produced the exception:
 
org.apache.ws.security.WSSecurityException: Signature creation failed (Cannot setup signature data structure)
 at org.apache.ws.security.message.WSSecSignature.prepare(WSSecSignature.java:323)
 at org.apache.ws.security.message.WSSecSignature.build(WSSecSignature.java:643)
 at tests.com.crimsonlogic.wsg.core.ws.security.TestUsernameTokenSignature.main(TestUsernameTokenSignature.java:87)
 
I debug the code and the error occurs at  WSSecSignature#prepare(Document doc, Crypto cr, WSSecHeader secHeader).
The exact line is at:
   SignatureAlgorithm signatureAlgorithm = new SignatureAlgorithm(doc, sigAlgo);
 
Could anyone provide some comment on this? Am I using WSS4J in the right way?
 
 
Regards,
Xinjun
 
 
/************************************************************************************************************
Sample Code to Test Signature with UsernameToken
************************************************************************************************************/
 public static void main(String[] args) {
  // get sample SOAP Envelope
  org.apache.axis2.soap.SOAPEnvelope unsignedEnv = Utils.getSampleSOAP11Envelope();

  org.w3c.dom.Document doc = null;
  try {
   doc = Axis2Util.getDocumentFromSOAPEnvelope(unsignedEnv);
  } catch (Exception e1) {
   e1.printStackTrace();
  }
  String username = "TradeX";
  String password = "TradeX";
  
  WSSecUsernameToken ut = new WSSecUsernameToken();
        ut.setPasswordType(WSConstants.PASSWORD_DIGEST);
        ut.setUserInfo(username, password);
        ut.addCreated();
        ut.addNonce();
        ut.prepare(doc);
       
        WSSecSignature sign = new WSSecSignature();
        Vector signParts = new Vector();
       
        String name = "Body";
        String namespace = "http://schemas.xmlsoap.org/soap/envelope/";
        String encMod = "Content";
        WSEncryptionPart part = new WSEncryptionPart(name, namespace, encMod);
        signParts.add(part);
       
        if(signParts.size() > 0) {
         sign.setParts(signParts);
        }
       
        sign.setUsernameToken(ut);
        sign.setKeyIdentifierType (WSConstants.UT_SIGNING);
        sign.setSignatureAlgorithm(XMLSignature.ALGO_ID_MAC_HMAC_SHA1);

        String actor = "";
        boolean mustUnderstand = true;
        //WSSecHeader secHeader = new WSSecHeader(actor, mustUnderstand);
        WSSecHeader secHeader = new WSSecHeader();

        secHeader.insertSecurityHeader(doc);
        try {
            sign.build(doc, null, secHeader);
            System.out.println(new String(sign.getSignatureValue()));
        } catch (WSSecurityException e) {
         e.printStackTrace();
        }
        ut.prependToHeader(secHeader);
  
 }

Reply via email to