我们如何使用java在soap请求xml中生成wsu:Id

时间:2017-03-21 11:34:42

标签: soap ws-security wss4j

请告知我们如何在soap请求xml中生成动态wsu:Id。

 <wsu:Timestamp wsu:Id="TS-C68ABF4EC1E628F5B5143638245038886955">
                <wsu:Created>2015-07-08T19:07:30.388Z</wsu:Created>
                <wsu:Expires>2015-07-08T19:12:30.388Z</wsu:Expires>
            </wsu:Timestamp>

Java代码:


private Element addTimestamp(Element wsSecurityHeaderElement) throws SOAPException, DatatypeConfigurationException { /if (false == this.addTimestamp) { return null; }/ Document document = wsSecurityHeaderElement.getOwnerDocument(); Element timestampElement = document.createElementNS(WSU_NAMESPACE, "wsu:Timestamp"); timestampElement.setAttributeNS(WSU_NAMESPACE, "wsu:Id", "TS"); // hard coded ts needs to be removed Attr idAttr = timestampElement.getAttributeNodeNS(WSU_NAMESPACE, "Id"); timestampElement.setIdAttributeNode(idAttr, true); Element createdElement = document.createElementNS(WSU_NAMESPACE, "wsu:Created"); DatatypeFactory datatypeFactory = DatatypeFactory.newInstance(); GregorianCalendar gregorianCalendar = new GregorianCalendar(); Date now = new Date(); gregorianCalendar.setTime(now); gregorianCalendar.setTimeZone(TimeZone.getTimeZone("UTC")); XMLGregorianCalendar xmlGregorianCalendar = datatypeFactory .newXMLGregorianCalendar(gregorianCalendar); createdElement.setTextContent(xmlGregorianCalendar.toXMLFormat()); timestampElement.appendChild(createdElement);

Element expiresElement = document.createElementNS(WSU_NAMESPACE, "wsu:Expires"); Date expiresDate = new Date(now.getTime() + 1000 * 60 * 5); gregorianCalendar.setTime(expiresDate); xmlGregorianCalendar = datatypeFactory .newXMLGregorianCalendar(gregorianCalendar); expiresElement.setTextContent(xmlGregorianCalendar.toXMLFormat()); timestampElement.appendChild(expiresElement); wsSecurityHeaderElement.appendChild(timestampElement); return timestampElement; } </code>

0 个答案:

没有答案