java 轴安全标头
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3156013/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Axis security header
提问by yurl
Hi trying to generate a security header in a Java Axis2 Client program in the format of.
您好,尝试在 Java Axis2 Client 程序中以 .
<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>
using the following code
使用以下代码
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);
Unfortunately its not generating quite what I wanted and I get.
不幸的是,它并没有产生我想要的东西,我得到了。
<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>
The service on the other end doesn't seem to handle the extra bits, resulting in an error
另一端的服务似乎没有处理额外的位,导致错误
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]
How do I generate the SOAPHeader to not print out all the extra empty bits? Cheers
如何生成 SOAPHeader 以不打印出所有额外的空位?干杯
回答by Justin Garrick
You're passing an empty string as the first argument to MessageElement, and you need to pass null. Note that nulland the empty string ("") are not the same thing in Java. Also, you are really cheating by passing the namespace prefix to the local name (second) parameter of the MessageElement constructor...this is not what it is designed for. That being said, you can fix the problem by passing nullas the namespace (first) parameter. If you try to pass it directly, you'll likely get an ambiguous constructor error, so do something like the following:
您将一个空字符串作为第一个参数传递给 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);
I'd also recommend you use a different web service engine (not Axis2) if you have any choice in the matter.
如果您有任何选择,我还建议您使用不同的 Web 服务引擎(不是 Axis2)。
回答by HariKrishna
Try this way to create custom header with Axis 1.* (The above code doesnt look like with Axis2)
尝试这种方式来创建带有 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>

