JAX-WS在SOAP头部添加用户名令牌。

huangapple go评论136阅读模式
英文:

JAX-WS add Username-Token to SOAP-Header

问题

我有一个从WSDL文件生成的JAX-WS客户端。
迄今为止,以下代码已经可以设置头部:

  1. WSBindingProvider bp = (WSBindingProvider) port;
  2. bp.setOutboundHeaders(
  3. Headers.create(new QName("http://schemas.xmlsoap.org/ws/2005/08/addressing", "To", "wsa"), "--To--"),
  4. Headers.create(new QName("http://schemas.xmlsoap.org/ws/2005/08/addressing", "Action", "wsa"), "--Action--"),
  5. Headers.create(new QName("http://schemas.xmlsoap.org/ws/2005/08/addressing", "MessageID", "wsa"), UUID.randomUUID().toString())
  6. );

这将生成(所期望的)以下XML片段:

  1. <S:Header>
  2. <To
  3. xmlns="http://schemas.xmlsoap.org/ws/2005/08/addressing">--to--
  4. </To>
  5. <Action
  6. xmlns="http://schemas.xmlsoap.org/ws/2005/08/addressing">--action--
  7. </Action>
  8. <MessageID
  9. xmlns="http://schemas.xmlsoap.org/ws/2005/08/addressing">fe1b400a-e724-4486-8618-b1d36a0acbbb
  10. </MessageID>
  11. </S:Header>

但是我需要以下链接的标签,而我无法使用Headers.create(...)来实现:

  1. <wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
  2. <wsse:UsernameToken wsu:Id="PartnerId" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
  3. <wsse:Username>--username--</wsse:Username>
  4. </wsse:UsernameToken>
  5. </wsse:Security>

有任何想法如何将此添加到头部吗?

英文:

I have a JAX-WS Client generated from WSDL files.
Setting Headers worked so far with following code:

  1. WSBindingProvider bp = (WSBindingProvider) port;
  2. bp.setOutboundHeaders(
  3. Headers.create(new QName(&quot;http://schemas.xmlsoap.org/ws/2005/08/addressing&quot;, &quot;To&quot;, &quot;wsa&quot;), &quot;--To--&quot;),
  4. Headers.create(new QName(&quot;http://schemas.xmlsoap.org/ws/2005/08/addressing&quot;, &quot;Action&quot;, &quot;wsa&quot;), &quot;--Action--&quot;),
  5. Headers.create(new QName(&quot;http://schemas.xmlsoap.org/ws/2005/08/addressing&quot;, &quot;MessageID&quot;, &quot;wsa&quot;), UUID.randomUUID().toString())
  6. );

Which produces (as desired) the following XML snippet:

  1. &lt;S:Header&gt;
  2. &lt;To
  3. xmlns=&quot;http://schemas.xmlsoap.org/ws/2005/08/addressing&quot;&gt;--to--
  4. &lt;/To&gt;
  5. &lt;Action
  6. xmlns=&quot;http://schemas.xmlsoap.org/ws/2005/08/addressing&quot;&gt;--action--
  7. &lt;/Action&gt;
  8. &lt;MessageID
  9. xmlns=&quot;http://schemas.xmlsoap.org/ws/2005/08/addressing&quot;&gt;fe1b400a-e724-4486-8618-b1d36a0acbbb
  10. &lt;/MessageID&gt;
  11. &lt;/S:Header&gt;

But I need the following chained Tags, which I couldn't acheive with Headers.create(...):

  1. &lt;wsse:Security xmlns:wsse=&quot;http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd&quot;&gt;
  2. &lt;wsse:UsernameToken wsu:Id=&quot;PartnerId&quot; xmlns:wsu=&quot;http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd&quot;&gt;
  3. &lt;wsse:Username&gt;--username--&lt;/wsse:Username&gt;
  4. &lt;/wsse:UsernameToken&gt;
  5. &lt;/wsse:Security&gt;

Any ideas how I can add this to the header?

答案1

得分: 4

以下是代码的翻译部分:

  1. // 创建一个SOAP头部
  2. try {
  3. SOAPMessage soapMessage = MessageFactory.newInstance().createMessage();
  4. SOAPPart soapPart = soapMessage.getSOAPPart();
  5. SOAPEnvelope soapEnvelope = soapPart.getEnvelope();
  6. SOAPHeader header = soapEnvelope.getHeader();
  7. // 添加安全性SOAP头部元素
  8. SOAPHeaderElement security = header.addHeaderElement(new QName(SCHEMA, "Security", SCHEMA_PREFIX));
  9. SOAPElement usernameToken = security.addChildElement("UsernameToken", SCHEMA_PREFIX);
  10. SOAPElement usernameElement = usernameToken.addChildElement("Username", SCHEMA_PREFIX);
  11. SOAPElement passwordElement = usernameToken.addChildElement("Password", SCHEMA_PREFIX);
  12. Name typeName = soapEnvelope.createName("type");
  13. passwordElement.addAttribute(typeName, "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText");
  14. usernameElement.setTextContent(USERNAME);
  15. passwordElement.setTextContent(PASSWORD);
  16. ((WSBindingProvider) webServicePort).setOutboundHeaders(Headers.create(security));
  17. } catch (SOAPException e) {
  18. logger.severe("设置SOAP头部出错");
  19. e.printStackTrace();
  20. }

以下是XML的翻译部分:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <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">
  3. <env:Header>
  4. <wsse:Security env:mustUnderstand="1">
  5. <wsse:UsernameToken>
  6. <wsse:Username>username</wsse:Username>
  7. <wsse:Password type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">password</wsse:Password>
  8. </wsse:UsernameToken>
  9. </wsse:Security>
  10. </env:Header>
  11. <env:Body>
  12. ...

请注意,这是您提供的代码的翻译部分。如果您有任何进一步的问题或需要其他帮助,请随时提问。

英文:

The following code works for me:

  1. private static final String SCHEMA = &quot;http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd&quot;;
  2. private static final String SCHEMA_PREFIX = &quot;wsse&quot;;
  3. private static final String USERNAME = &quot;username&quot;;
  4. private static final String PASSWORD = &quot;password&quot;;
  5. // Create a SOAP header
  6. try {
  7. SOAPMessage soapMessage = MessageFactory.newInstance().createMessage();
  8. SOAPPart soapPart = soapMessage.getSOAPPart();
  9. SOAPEnvelope soapEnvelope = soapPart.getEnvelope();
  10. SOAPHeader header = soapEnvelope.getHeader();
  11. // Add the security SOAP header element
  12. SOAPHeaderElement security = header.addHeaderElement(new QName(SCHEMA, &quot;Security&quot;, SCHEMA_PREFIX));
  13. SOAPElement usernameToken = security.addChildElement(&quot;UsernameToken&quot;, SCHEMA_PREFIX);
  14. SOAPElement usernameElement = usernameToken.addChildElement(&quot;Username&quot;, SCHEMA_PREFIX);
  15. SOAPElement passwordElement = usernameToken.addChildElement(&quot;Password&quot;, SCHEMA_PREFIX);
  16. Name typeName = soapEnvelope.createName(&quot;type&quot;);
  17. passwordElement.addAttribute(typeName, &quot;http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText&quot;);
  18. usernameElement.setTextContent(USERNAME);
  19. passwordElement.setTextContent(PASSWORD);
  20. ((WSBindingProvider) webServicePort).setOutboundHeaders(Headers.create(security));
  21. } catch (SOAPException e) {
  22. logger.severe(&quot;Error setting SOAP header&quot;);
  23. e.printStackTrace();
  24. }

The XML is as follows:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

  1. &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  2. &lt;env:Envelope xmlns:env=&quot;http://schemas.xmlsoap.org/soap/envelope/&quot; xmlns:xsd=&quot;http://www.w3.org/2001/XMLSchema&quot; xmlns:wsse=&quot;http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd&quot;&gt;
  3. &lt;env:Header&gt;
  4. &lt;wsse:Security env:mustUnderstand=&quot;1&quot;&gt;
  5. &lt;wsse:UsernameToken&gt;
  6. &lt;wsse:Username&gt;username&lt;/wsse:Username&gt;
  7. &lt;wsse:Password type=&quot;http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText&quot;&gt;password&lt;/wsse:Password&gt;
  8. &lt;/wsse:UsernameToken&gt;
  9. &lt;/wsse:Security&gt;
  10. &lt;/env:Header&gt;
  11. &lt;env:Body&gt;
  12. ...

<!-- end snippet -->

huangapple
  • 本文由 发表于 2020年8月14日 22:21:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/63414620.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定