Generate UsernameToken with WSS4J

62 Views Asked by At

I am trying to update a project to WSS4J 2.3. The problem I have is that I am not sure how to add UsernameToken header to an existing SOAP message.

I have tried the following code to generate the UsernameToken.

    public static void addWSS(Document soap, String userName, String pwd) throws Exception {
        WSSecHeader secHeader = new WSSecHeader(soap);
        secHeader.setMustUnderstand(false);
        secHeader.insertSecurityHeader();

        WSSecUsernameToken builder = new WSSecUsernameToken(secHeader);
        builder.setPasswordType(WSConstants.PASSWORD_TEXT);
        builder.setUserInfo(userName, pwd);
        builder.addCreated();
        builder.appendToHeader();
        builder.build();// I am not sure if I have to return the signed document or not and if so how to convert it back to a SOAPMessage
    }

I generate the soap message with the following code:

MessageFactory mf = MessageFactory.newInstance();
SOAPMessage msg = mf.createMessage();
SOAPBody body = msg.getSOAPBody();

And finally, I send the SOAP message:

SOAPConnection con = SOAPConnectionFactory.newInstance().createConnection();
URL url = new URL(endpoint);
SOAPMessage respuesta = con.call(msg, url);

The output message does not contain the usernametoken.

0

There are 0 best solutions below