Axis 1.4 Soap Header添加用户名和密码

时间:2016-12-14 22:21:14

标签: java xml web-services soap axis

我有一个axis 1.4 Web服务客户端。我需要在soap消息头中添加用户名和密码。我做到了,但我也产生了一些不受欢迎的属性。

这是我的代码:

   SOAPHeaderElement sec = new SOAPHeaderElement(
            "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd",
            "wsse:Security");

    SOAPHeaderElement token = new SOAPHeaderElement("",
            "wsse:UsernameToken");
    SOAPHeaderElement username = new SOAPHeaderElement("", "wsse:Username");
    SOAPHeaderElement password = new SOAPHeaderElement("", "wsse:Password");
    username.addTextNode("testuser");
    password.addTextNode("testpass");

    token.addChildElement(username);
    token.addChildElement(password);
    sec.addChildElement(token);

这是我想要生成的soap标题:

    <soapenv:Header> 
<wsse:Security 
     xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">         
    <wsse:UsernameToken> 
        <wsse:Username>testuser</wsse:Username> 
        <wsse:Password>testpass</wsse:Password>
    </wsse:UsernameToken> 
</wsse:Security> 

这是生成的标题:

    <soapenv:Header>
<wsse:Security 
    soapenv:actor="http://schemas.xmlsoap.org/soap/actor/next" 
    soapenv:mustUnderstand="0" 
    xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" 
    xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
    <wsse:UsernameToken 
        soapenv:actor="http://schemas.xmlsoap.org/soap/actor/next" 
        soapenv:mustUnderstand="0" 
        xmlns:wsse="">
            <wsse:Username 
                soapenv:actor="http://schemas.xmlsoap.org/soap/actor/next" 
                soapenv:mustUnderstand="0" 
                xmlns:wsse="">testuser</wsse:Username>
            <wsse:Password 
                soapenv:actor="http://schemas.xmlsoap.org/soap/actor/next" 
                soapenv:mustUnderstand="0" 
                xmlns:wsse="">testpass</wsse:Password>
    </wsse:UsernameToken>
</wsse:Security>

其他名称空间和属性没有问题,但空 xmlns:wsse =“”属性导致我的客户端从Web服务获取错误。在soap-ui中,当我抛出这个空属性时,我得到了成功的回复。在java代码中,我从Web服务获得解析错误。

如果没有这些属性,我无法生成标题。如果我在标题元素中添加“wsse”前缀,它也会将 xmlns:wsse =“”属性添加到这些元素中。 如果没有wsse前缀,我会从Web服务中收到错误。

如何在1.4轴中生成带有wsse前缀且没有 xmlns:wsse =“”属性的标头?

1 个答案:

答案 0 :(得分:0)

不要将空字符串传递给名称空间URI,而只需执行与安全标记相同的操作:

String namespaceUri = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd";
SOAPHeaderElement sec = new SOAPHeaderElement(namespaceUri, "wsse:Security");
SOAPHeaderElement token = new SOAPHeaderElement(namespaceUri, "wsse:UsernameToken");
SOAPHeaderElement username = new SOAPHeaderElement(namespaceUri, "wsse:Username");
SOAPHeaderElement password = new SOAPHeaderElement(namespaceUri, "wsse:Password");