我必须从Axis2升级到Axis2,但是不确定如何升级此功能以与Axis2一起使用。
这是我使用轴将安全标头添加到SOAP请求中的代码:
public void addSecurityHeader(WSAPIExternal api, SecurityHeaderBean credentials) {
try {
Stub s = (Stub) api;
QName namespace = new QName("http://default-url", "localPart", "prefix");
SOAPHeaderElement header = new SOAPHeaderElement(namespace);
SOAPElement nodeCredentials = header.addChildElement("credentials");
SOAPElement nodeUsername = nodeCredentials.addChildElement("username");
nodeUsername.addTextNode(credentials.getUsername());
SOAPElement nodePassword = nodeCredentials.addChildElement("password");
nodePassword.addTextNode(credentials.getPassword());
s.setHeader(header);
} catch (SOAPException e) {
logger.error("{}", e);
}
}
WSAPIExternal
类扩展了Remote
,因此可以将其强制转换为Stub
。
我尝试用OMElement
创建类似内容,但感到困惑。
到目前为止,我有这个:
public void addSecurityHeader(WSAPIExternal api, SecurityHeaderBean credentials) {
Stub s = (Stub) api;
ServiceClient serviceClient = s._getServiceClient();
OMFactory factory = OMAbstractFactory.getOMFactory();
OMNamespace namePptData = factory.createOMNamespace("http://default-url", "prefix");
OMElement nodeCredentials = factory.createOMElement("credentials", namePptData);
OMElement nodeUsername = factory.createOMElement("username", namePptData);
nodeUsername.setText(credentials.getUsername());
nodeCredentials.addChild(nodeUsername);
OMElement nodePassword = factory.createOMElement("password", namePptData);
nodePassword.setText(credentials.getPassword());
nodeCredentials.addChild(nodePassword);
serviceClient.addHeader(nodeCredentials);
}
任何帮助将不胜感激。
答案 0 :(得分:0)
SOAPEnvelope envelope = smc.getMessage().getSOAPPart().getEnvelope();
SOAPHeader header = envelope.addHeader();
SOAPElement security =
header.addChildElement("Security", "wsse", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");
SOAPElement usernameToken =
security.addChildElement("UsernameToken", "wsse");
usernameToken.addAttribute(new QName("xmlns:wsu"), "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd");
SOAPElement username =
usernameToken.addChildElement("Username", "wsse");
username.addTextNode("test");
SOAPElement password =
usernameToken.addChildElement("Password", "wsse");
password.setAttribute("Type", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText");
password.addTextNode("test321");