Axis安全标头

时间:2010-07-01 08:06:06

标签: java soap header axis2 soap-client

您正尝试以。?/ / p>的格式在Java Axis2客户端程序中生成安全标头

<soapenv:Header>
 <wsse:Security xmlns:wsse="http://schemas.xmlsoap.org/ws/2002/04/secext>
  <wsse:UsernameToken>
   <wsse:Username>myUsername</wsse:Username>
   <wsse:Password>myPassword</wsse:Password>
  </wsse:UsernameToken>
 </wsse:Security>
</soapenv:Header>

使用以下代码

SOAPHeaderElement wsseSecurity = new SOAPHeaderElement(new PrefixedQName("http://schemas.xmlsoap.org/ws/2002/04/secext","Security", "wsse"));
MessageElement usernameToken = new MessageElement("", "wsse:UsernameToken");
MessageElement username = new MessageElement("", "wsse:Username");
MessageElement password = new MessageElement("", "wsse:Password");
username.setObjectValue(myProps.getProperty("username"));
usernameToken.addChild(username);
password.setObjectValue(myProps.getProperty("password"));
usernameToken.addChild(password);
wsseSecurity.addChild(usernameToken);

BookingPort bp = bsl.getBooking();
((Stub) bp).setHeader(wsseSecurity);

不幸的是,它并没有产生我想要的东西而且我得到了。

<soapenv:Header>
 <wsse:Security soapenv:actor="http://schemas.xmlsoap.org/soap/actor/next" soapenv:mustUnderstand="0" xmlns:wsse="http://schemas.xmlsoap.org/ws/2002/04/secext">
  <wsse:UsernameToken xmlns:wsse="">
   <wsse:Username xmlns:wsse="">myUsername</wsse:Username>
   <wsse:Password xmlns:wsse="">myPassword</wsse:Password>
  </wsse:UsernameToken>
 </wsse:Security>
</soapenv:Header>

另一端的服务似乎没有处理额外的位,导致错误

faultDetail:
        {http://xml.apache.org/axis/}stackTrace:com.ctc.wstx.exc.WstxParsingException: Non-default namespace can not map to empty URI (as per Namespace 1.0 # 2) in XML 1.0 documents
 at [row,col {unknown-source}]: [1,450]

如何生成SOAPHeader以不打印出所有额外的空位? 干杯

2 个答案:

答案 0 :(得分:6)

您将一个空字符串作为MessageElement的第一个参数传递,您需要传递null。请注意,null和空字符串("")在Java中不是一回事。此外,您通过将名称空间前缀传递给MessageElement构造函数的本地名称(第二个)参数来实际作弊...这不是它的设计目的。话虽这么说,您可以通过传递null作为命名空间(第一个)参数来解决问题。如果你试图直接传递它,你可能会得到一个模糊的构造函数错误,所以做类似以下的事情:

SOAPHeaderElement wsseSecurity = new SOAPHeaderElement(new PrefixedQName("http://schemas.xmlsoap.org/ws/2002/04/secext","Security", "wsse"));
String nullString = null;
MessageElement usernameToken = new MessageElement(nullString, "wsse:UsernameToken");
MessageElement username = new MessageElement(nullString, "wsse:Username");
MessageElement password = new MessageElement(nullString, "wsse:Password");
username.setObjectValue(myProps.getProperty("username"));
usernameToken.addChild(username);
password.setObjectValue(myProps.getProperty("password"));
usernameToken.addChild(password);
wsseSecurity.addChild(usernameToken);

BookingPort bp = bsl.getBooking();
((Stub) bp).setHeader(wsseSecurity);

如果您对此事有任何选择,我还建议您使用其他Web服务引擎(而不是Axis2)。

答案 1 :(得分:2)

尝试这种方式使用Axis 1创建自定义标题。*(上面的代码看起来与Axis2不同)

import org.apache.axis.message.SOAPHeaderElement;
import javax.xml.soap.SOAPElement;


public void createCustomHeader(){

SOAPElement oHeaderElement;
SOAPElement oElement;   

    oHeaderElement = new SOAPHeaderElement("http://ws.mycompany.com", "securityHeader");
    oHeaderElement.setPrefix("sec");
    oHeaderElement.setMustUnderstand(false);

    oElement = oHeaderElement.addChildElement("username");
    oElement.addTextNode("myusername");
    oElement = oHeaderElement.addChildElement("password");
    oElement.addTextNode("mypassword");

    // You can create client code something like this..
    MySampleServiceServiceLocator service  = new MySampleServiceServiceLocator();
    service.setMySampleServiceEndpointAddress("endpointURL");
    MySampleWebService serv = service.getMySampleService();
    MySampleServiceSoapBindingStub stub = (MySampleServiceSoapBindingStub)serv;

    // add this header to your stubs
stub.setHeader(oHeaderElement);


// Finally call your web service methid
serv.getMyClaimStatus("XYZ001");


}

//It creates the custom header like this:

<soapenv:Header>
    <sec:securityHeader xmlns:sec="http://ws.mycompany.com"
        soapenv:actor="http://schemas.xmlsoap.org/soap/actor/next" soapenv:mustUnderstand="0">
        <sec:username>myusername</sec:username>
        <sec:password>mypassword</sec:password>
    </sec:securityHeader>
</soapenv:Header>