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

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

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(&quot;http://schemas.xmlsoap.org/ws/2005/08/addressing&quot;, &quot;To&quot;, &quot;wsa&quot;), &quot;--To--&quot;),
            Headers.create(new QName(&quot;http://schemas.xmlsoap.org/ws/2005/08/addressing&quot;, &quot;Action&quot;, &quot;wsa&quot;), &quot;--Action--&quot;),
            Headers.create(new QName(&quot;http://schemas.xmlsoap.org/ws/2005/08/addressing&quot;, &quot;MessageID&quot;, &quot;wsa&quot;), UUID.randomUUID().toString())
    );

Which produces (as desired) the following XML snippet:

    &lt;S:Header&gt;
		&lt;To
			xmlns=&quot;http://schemas.xmlsoap.org/ws/2005/08/addressing&quot;&gt;--to--
		&lt;/To&gt;
		&lt;Action
			xmlns=&quot;http://schemas.xmlsoap.org/ws/2005/08/addressing&quot;&gt;--action--
		&lt;/Action&gt;
		&lt;MessageID
			xmlns=&quot;http://schemas.xmlsoap.org/ws/2005/08/addressing&quot;&gt;fe1b400a-e724-4486-8618-b1d36a0acbbb
		&lt;/MessageID&gt;
	&lt;/S:Header&gt;

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

&lt;wsse:Security xmlns:wsse=&quot;http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd&quot;&gt;
    &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;
        &lt;wsse:Username&gt;--username--&lt;/wsse:Username&gt;
    &lt;/wsse:UsernameToken&gt;
&lt;/wsse:Security&gt;

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 = &quot;http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd&quot;;
        private static final String SCHEMA_PREFIX = &quot;wsse&quot;;
        private static final String USERNAME = &quot;username&quot;;
        private static final String PASSWORD = &quot;password&quot;;

        // 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, &quot;Security&quot;, SCHEMA_PREFIX));
		      SOAPElement usernameToken = security.addChildElement(&quot;UsernameToken&quot;, SCHEMA_PREFIX);
		      SOAPElement usernameElement = usernameToken.addChildElement(&quot;Username&quot;, SCHEMA_PREFIX);
		      SOAPElement passwordElement = usernameToken.addChildElement(&quot;Password&quot;, SCHEMA_PREFIX);
		      Name typeName = soapEnvelope.createName(&quot;type&quot;);
		      passwordElement.addAttribute(typeName, &quot;http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText&quot;);
	
		      usernameElement.setTextContent(USERNAME);
		      passwordElement.setTextContent(PASSWORD);
	
		      ((WSBindingProvider) webServicePort).setOutboundHeaders(Headers.create(security));
		} catch (SOAPException e) {
			logger.severe(&quot;Error setting SOAP header&quot;);
			e.printStackTrace();
		}

The XML is as follows:

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

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

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&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;
    &lt;env:Header&gt;
        &lt;wsse:Security env:mustUnderstand=&quot;1&quot;&gt;
            &lt;wsse:UsernameToken&gt;
                &lt;wsse:Username&gt;username&lt;/wsse:Username&gt;
                &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;
            &lt;/wsse:UsernameToken&gt;
        &lt;/wsse:Security&gt;
    &lt;/env:Header&gt;
    &lt;env:Body&gt;
    ...

<!-- 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:

确定