英文:
JAX-WS add Username-Token to SOAP-Header
问题
我有一个从WSDL文件生成的JAX-WS客户端。
迄今为止,以下代码已经可以设置头部:
WSBindingProvider bp = (WSBindingProvider) port;
bp.setOutboundHeaders(
Headers.create(new QName("http://schemas.xmlsoap.org/ws/2005/08/addressing", "To", "wsa"), "--To--"),
Headers.create(new QName("http://schemas.xmlsoap.org/ws/2005/08/addressing", "Action", "wsa"), "--Action--"),
Headers.create(new QName("http://schemas.xmlsoap.org/ws/2005/08/addressing", "MessageID", "wsa"), UUID.randomUUID().toString())
);
这将生成(所期望的)以下XML片段:
<S:Header>
<To
xmlns="http://schemas.xmlsoap.org/ws/2005/08/addressing">--to--
</To>
<Action
xmlns="http://schemas.xmlsoap.org/ws/2005/08/addressing">--action--
</Action>
<MessageID
xmlns="http://schemas.xmlsoap.org/ws/2005/08/addressing">fe1b400a-e724-4486-8618-b1d36a0acbbb
</MessageID>
</S:Header>
但是我需要以下链接的标签,而我无法使用Headers.create(...)来实现:
<wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
<wsse:UsernameToken wsu:Id="PartnerId" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
<wsse:Username>--username--</wsse:Username>
</wsse:UsernameToken>
</wsse:Security>
有任何想法如何将此添加到头部吗?
英文:
I have a JAX-WS Client generated from WSDL files.
Setting Headers worked so far with following code:
WSBindingProvider bp = (WSBindingProvider) port;
bp.setOutboundHeaders(
Headers.create(new QName("http://schemas.xmlsoap.org/ws/2005/08/addressing", "To", "wsa"), "--To--"),
Headers.create(new QName("http://schemas.xmlsoap.org/ws/2005/08/addressing", "Action", "wsa"), "--Action--"),
Headers.create(new QName("http://schemas.xmlsoap.org/ws/2005/08/addressing", "MessageID", "wsa"), UUID.randomUUID().toString())
);
Which produces (as desired) the following XML snippet:
<S:Header>
<To
xmlns="http://schemas.xmlsoap.org/ws/2005/08/addressing">--to--
</To>
<Action
xmlns="http://schemas.xmlsoap.org/ws/2005/08/addressing">--action--
</Action>
<MessageID
xmlns="http://schemas.xmlsoap.org/ws/2005/08/addressing">fe1b400a-e724-4486-8618-b1d36a0acbbb
</MessageID>
</S:Header>
But I need the following chained Tags, which I couldn't acheive with Headers.create(...):
<wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
<wsse:UsernameToken wsu:Id="PartnerId" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
<wsse:Username>--username--</wsse:Username>
</wsse:UsernameToken>
</wsse:Security>
Any ideas how I can add this to the header?
答案1
得分: 4
以下是代码的翻译部分:
// 创建一个SOAP头部
try {
SOAPMessage soapMessage = MessageFactory.newInstance().createMessage();
SOAPPart soapPart = soapMessage.getSOAPPart();
SOAPEnvelope soapEnvelope = soapPart.getEnvelope();
SOAPHeader header = soapEnvelope.getHeader();
// 添加安全性SOAP头部元素
SOAPHeaderElement security = header.addHeaderElement(new QName(SCHEMA, "Security", SCHEMA_PREFIX));
SOAPElement usernameToken = security.addChildElement("UsernameToken", SCHEMA_PREFIX);
SOAPElement usernameElement = usernameToken.addChildElement("Username", SCHEMA_PREFIX);
SOAPElement passwordElement = usernameToken.addChildElement("Password", SCHEMA_PREFIX);
Name typeName = soapEnvelope.createName("type");
passwordElement.addAttribute(typeName, "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText");
usernameElement.setTextContent(USERNAME);
passwordElement.setTextContent(PASSWORD);
((WSBindingProvider) webServicePort).setOutboundHeaders(Headers.create(security));
} catch (SOAPException e) {
logger.severe("设置SOAP头部出错");
e.printStackTrace();
}
以下是XML的翻译部分:
<?xml version="1.0" encoding="utf-8"?>
<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
<env:Header>
<wsse:Security env:mustUnderstand="1">
<wsse:UsernameToken>
<wsse:Username>username</wsse:Username>
<wsse:Password type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">password</wsse:Password>
</wsse:UsernameToken>
</wsse:Security>
</env:Header>
<env:Body>
...
请注意,这是您提供的代码的翻译部分。如果您有任何进一步的问题或需要其他帮助,请随时提问。
英文:
The following code works for me:
private static final String SCHEMA = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd";
private static final String SCHEMA_PREFIX = "wsse";
private static final String USERNAME = "username";
private static final String PASSWORD = "password";
// Create a SOAP header
try {
SOAPMessage soapMessage = MessageFactory.newInstance().createMessage();
SOAPPart soapPart = soapMessage.getSOAPPart();
SOAPEnvelope soapEnvelope = soapPart.getEnvelope();
SOAPHeader header = soapEnvelope.getHeader();
// Add the security SOAP header element
SOAPHeaderElement security = header.addHeaderElement(new QName(SCHEMA, "Security", SCHEMA_PREFIX));
SOAPElement usernameToken = security.addChildElement("UsernameToken", SCHEMA_PREFIX);
SOAPElement usernameElement = usernameToken.addChildElement("Username", SCHEMA_PREFIX);
SOAPElement passwordElement = usernameToken.addChildElement("Password", SCHEMA_PREFIX);
Name typeName = soapEnvelope.createName("type");
passwordElement.addAttribute(typeName, "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText");
usernameElement.setTextContent(USERNAME);
passwordElement.setTextContent(PASSWORD);
((WSBindingProvider) webServicePort).setOutboundHeaders(Headers.create(security));
} catch (SOAPException e) {
logger.severe("Error setting SOAP header");
e.printStackTrace();
}
The XML is as follows:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
<?xml version="1.0" encoding="utf-8"?>
<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
<env:Header>
<wsse:Security env:mustUnderstand="1">
<wsse:UsernameToken>
<wsse:Username>username</wsse:Username>
<wsse:Password type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">password</wsse:Password>
</wsse:UsernameToken>
</wsse:Security>
</env:Header>
<env:Body>
...
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论