Skip to content

Instantly share code, notes, and snippets.

@javajack
Forked from carloprad/gist:10dbfbeea3be120d348d
Last active July 6, 2017 19:42
Show Gist options
  • Save javajack/da3c65f4f37f1ed78bf7 to your computer and use it in GitHub Desktop.
Save javajack/da3c65f4f37f1ed78bf7 to your computer and use it in GitHub Desktop.
wsse usernametoken password digest soap generator
import org.apache.ws.security.WSConstants;
import org.apache.ws.security.message.WSSecHeader;
import org.apache.ws.security.message.WSSecUsernameToken;
import org.apache.ws.security.WSSConfig;
import org.apache.xalan.processor.TransformerFactoryImpl;
import org.apache.xerces.jaxp.DocumentBuilderFactoryImpl;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.apache.ws.security.message.WSSecTimestamp;
WSSecUsernameToken token = new WSSecUsernameToken();
token.setPasswordType(WSConstants.PASSWORD_DIGEST);
//Set your username and password here
token.setUserInfo("myUser", "myPass");
Document document = DocumentBuilderFactoryImpl.newInstance().newDocumentBuilder().newDocument();
document.appendChild(document.createElement("envelope"));
WSSecHeader header = new WSSecHeader();
header.insertSecurityHeader(document);
token.build(document, header);
WSSecTimestamp timestamp = new WSSecTimestamp();
timestamp.setTimeToLive(3000);
timestamp.prepare(document);
timestamp.prependToHeader(header);
Transformer transformer = TransformerFactoryImpl.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
StreamResult result = new StreamResult(new StringWriter());
DOMSource source = new DOMSource(header.getSecurityHeader());
transformer.transform(source, result);
String xmlString = result.getWriter().toString();
vars.put("security", xmlString);
//log.info(vars.get("security"));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment