2010-07-01 59 views
7

您好,試圖在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不打印出所有多餘的空位? 乾杯

+0

如何肥皂服務..please給出的示例代碼,我頭...... java和axis2 1.6.7 – 2015-11-14 05:30:08

回答

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); 

我也建議您使用不同的網絡服務引擎(不Axis2的),如果你在這件事上有任何選擇。

2

嘗試這種方式來創建自定義頁眉與軸1 *(上面的代碼does not看起來像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> 
+0

爲我工作,謝謝! – 2012-01-27 13:32:41