任何人都可以指导如何在Java代码中转换此肥皂信封吗?当我从SOAPUI中访问API时,这是标头。
<soapenv:Header>
<wsse:Security soapenv:mustUnderstand="1" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"
xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
<wsse:UsernameToken wsu:Id="UsernameToken-A139C7EBE4BD7F3FA7155144588419221">
<wsse:Username>xxxxxx</wsse:Username>
<wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">xxxxxx</wsse:Password>
<wsse:Nonce EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary">CfPQR+H2TUhwnvV1k5jByw==
</wsse:Nonce>
<wsu:Created>2019-03-01T13:11:24.192Z</wsu:Created>
</wsse:UsernameToken>
</wsse:Security>
</soapenv:Header>
这是我的Java代码:
public class MessageHandlerCustomer implements SOAPHandler<SOAPMessageContext> {
@Override
public boolean handleMessage(SOAPMessageContext smc) {
Boolean outboundProperty = (Boolean) smc.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
logger.debug("Entered in MessageHandlerCustomer");
if (outboundProperty.booleanValue()) {
String UserName = "xxx";
String Password = "xxxx";
SimpleDateFormat timeStamp = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
timeStamp.setTimeZone(TimeZone.getTimeZone("GMT"));
try {
/*
* add a header with the authentication info into the SOAP
*/
// get SOAP envelope from SOAP message
final SOAPEnvelope envelope = smc.getMessage().getSOAPPart().getEnvelope();
// create instance of SOAP factory
final SOAPFactory soapFactory = SOAPFactory.newInstance();
// create SOAP elements specifying prefix and URI
// final SOAPElement headerElm =
// soapFactory.createElement(GRCBIZConstantes.HEADER_WS_SERVICE,
// GRCBIZConstantes.PREFIX_WS_SERVICE,
// GRCBIZConstantes.URI_CUSTOMER_SERVICE);
final SOAPElement headerElm = soapFactory.createElement("Security", "wsse",
"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");
headerElm.setAttribute("soapenv:mustUnderstand", "1");
final SOAPElement securityElm = soapFactory.createElement("UsernameToken", "wsse",
"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");
securityElm.setAttribute("wsu:Id", "UsernameToken-A139C7EBE4BD7F3FA7155144588419221");
final SOAPElement userNameElm = soapFactory.createElement("Username", "wsse",
"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");
userNameElm.addTextNode(UserName);
final SOAPElement passwordElm = soapFactory.createElement("Password", "wsse",
"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");
passwordElm.addTextNode(Password);
passwordElm.setAttribute("Type",
"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText");
final SOAPElement nonce = soapFactory.createElement("Nonce", "wsse",
"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");
nonce.setAttribute("EncodingType",
"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary");
nonce.addTextNode("CfPQR+H2TUhwnvV1k5jByw==");
final SOAPElement created = soapFactory.createElement("Created", "wsu",
"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");
userNameElm.addTextNode(timeStamp.toString());
securityElm.addChildElement(userNameElm);
securityElm.addChildElement(passwordElm);
securityElm.addChildElement(nonce);
securityElm.addChildElement(created);
headerElm.addChildElement(securityElm);
// create SOAPHeader instance for SOAP envelope
final SOAPHeader sh = envelope.addHeader();
// add SOAP element for header to SOAP header object
sh.addChildElement(headerElm);
logger.debug("Header: " + sh.getAllAttributes().toString());
} catch (final Exception ex) {
logger.error(ex.getMessage(), ex);
}
}
return outboundProperty;
}
public static String elementToString(Element element) {
Document document = element.getOwnerDocument();
DOMImplementationLS domImplLS = (DOMImplementationLS) document.getImplementation();
LSSerializer serializer = domImplLS.createLSSerializer();
String str = serializer.writeToString(element);
return str;
}
@Override
public Set getHeaders() {
return null;
}
@Override
public boolean handleFault(SOAPMessageContext context) {
return true;
}
@Override
public void close(MessageContext context) {
}
}
传递的标头很少,例如mustUnderstand和wsu安全标记。其次,我想在日志中打印标题。实际上,我从Web服务获得静态响应,并且Web服务没有告诉您确切缺少了什么。