Java - 发送 SOAP header(用于身份验证)
Java - Send SOAP header (for authentication)
我已经从 wsdl 文件生成了一些 Java 代码,请求本身似乎可以正常工作,但我无法发送我的凭据。
我已经使用名为 "SoapUI" 的工具测试了 Web 服务,一切似乎都很顺利。
这里是(工作)xml:
的一个例子
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:beac="url">
<soapenv:Header>
<nsAuthn:authnHeader xmlns:nsAuthn="url/auth">
<nsAuthn:id>id</nsAuthn:id>
<nsAuthn:password>password</nsAuthn:password>
</nsAuthn:authnHeader>
</soapenv:Header>
<soapenv:Body>
<beac:getData>
<saisonid>int</saisonid>
</beac:getData>
</soapenv:Body>
</soapenv:Envelope>
这是我的尝试:
public RankDtoResponse getData(int saisonid) throws java.rmi.RemoteException, SOAPException {
if (super.cachedEndpoint == null) {
throw new org.apache.axis.NoEndPointException();
}
SOAPHeaderElement authentication = new SOAPHeaderElement("url","auth");
SOAPHeaderElement user = new SOAPHeaderElement("url","id", "id");
SOAPHeaderElement password = new SOAPHeaderElement("url","password", "password");
try {
authentication.addChild(user);
authentication.addChild(password);
} catch (SOAPException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
org.apache.axis.client.Call _call = createCall();
_call.setOperation(_operations[3]);
_call.setUseSOAPAction(true);
_call.setSOAPActionURI("");
_call.setEncodingStyle(null);
_call.setProperty(org.apache.axis.client.Call.SEND_TYPE_ATTR, Boolean.FALSE);
_call.setProperty(org.apache.axis.AxisEngine.PROP_DOMULTIREFS, Boolean.FALSE);
_call.setSOAPVersion(org.apache.axis.soap.SOAPConstants.SOAP11_CONSTANTS);
_call.setOperationName(new javax.xml.namespace.QName("url", "getData"));
setHeader(authentication);
setRequestHeaders(_call);
setAttachments(_call);
try { java.lang.Object _resp = _call.invoke(new java.lang.Object[] {new java.lang.Integer(saisonid)});
if (_resp instanceof java.rmi.RemoteException) {
throw (java.rmi.RemoteException)_resp;
}
else {
extractAttachments(_call);
try {
return (RankDtoResponse) _resp;
} catch (java.lang.Exception _exception) {
return (RankDtoResponse) org.apache.axis.utils.JavaUtils.convert(_resp, RankDtoResponse.class);
}
}
} catch (org.apache.axis.AxisFault axisFaultException) {
if (axisFaultException.detail != null) {
if (axisFaultException.detail instanceof java.rmi.RemoteException) {
throw (java.rmi.RemoteException) axisFaultException.detail;
}
if (axisFaultException.detail instanceof SOAPException) {
throw (SOAPException) axisFaultException.detail;
}
}
throw axisFaultException;
}
}
错误:
AxisFault
faultCode: 1-1-3
faultSubcode:
faultString: Could not authenticate, credentials not specified
faultActor:
faultNode:
faultDetail:
{http://xml.apache.org/axis/}stackTrace:Could not authenticate, credentials not specified
at org.apache.axis.message.SOAPFaultBuilder.createFault(SOAPFaultBuilder.java:222)
at org.apache.axis.message.SOAPFaultBuilder.endElement(SOAPFaultBuilder.java:129)
at org.apache.axis.encoding.DeserializationContext.endElement(DeserializationContext.java:1087)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.endElement(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanEndElement(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl.parse(Unknown Source)
at org.apache.axis.encoding.DeserializationContext.parse(DeserializationContext.java:227)
at org.apache.axis.SOAPPart.getAsSOAPEnvelope(SOAPPart.java:696)
at org.apache.axis.Message.getSOAPEnvelope(Message.java:435)
at org.apache.axis.handlers.soap.MustUnderstandChecker.invoke(MustUnderstandChecker.java:62)
at org.apache.axis.client.AxisClient.invoke(AxisClient.java:206)
at org.apache.axis.client.Call.invokeEngine(Call.java:2784)
at org.apache.axis.client.Call.invoke(Call.java:2767)
at org.apache.axis.client.Call.invoke(Call.java:2443)
at org.apache.axis.client.Call.invoke(Call.java:2366)
at org.apache.axis.client.Call.invoke(Call.java:1812)
at getData.getData(getData.java:496)
at Client.main(Client.java:20)
大部分代码是由 wsdl2java 从 wsdl 文件自动生成的。
您需要更多信息吗?我是不是漏掉了什么?
提前致谢
我觉得你的代码不错,所以不确定真正的问题在哪里。
而是在调用服务方法时修改存根 class,而不是修改存根 class。您的代码应如下所示,您可以删除对生成的存根 class 的代码修改,它应该可以工作。
OMNamespace hdrNs =
OMAbstractFactory.getOMFactory().createOMNamespace("url/auth", "nsAuthn");
SOAPHeaderBlock header = OMAbstractFactory.getSOAP12Factory().createSOAPHeaderBlock("authnHeader", hdrNs);
header.addChild(AXIOMUtil.stringToOM("<id>$id</id>"));
header.addChild(AXIOMUtil.stringToOM("<password>$id</password>"));
stub._getServiceClient().addHeader(header);
希望对您有所帮助!
我已经使用了这个 approach 并且因为您已经有了一个向 body 添加数据的示例,所以创建和向 header 添加内容同样简单。
只需使用 envelope.getHeader()
获取 Header 并将我的 header 数据添加到其中。
感谢您的帮助!
我已经从 wsdl 文件生成了一些 Java 代码,请求本身似乎可以正常工作,但我无法发送我的凭据。
我已经使用名为 "SoapUI" 的工具测试了 Web 服务,一切似乎都很顺利。
这里是(工作)xml:
的一个例子<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:beac="url">
<soapenv:Header>
<nsAuthn:authnHeader xmlns:nsAuthn="url/auth">
<nsAuthn:id>id</nsAuthn:id>
<nsAuthn:password>password</nsAuthn:password>
</nsAuthn:authnHeader>
</soapenv:Header>
<soapenv:Body>
<beac:getData>
<saisonid>int</saisonid>
</beac:getData>
</soapenv:Body>
</soapenv:Envelope>
这是我的尝试:
public RankDtoResponse getData(int saisonid) throws java.rmi.RemoteException, SOAPException {
if (super.cachedEndpoint == null) {
throw new org.apache.axis.NoEndPointException();
}
SOAPHeaderElement authentication = new SOAPHeaderElement("url","auth");
SOAPHeaderElement user = new SOAPHeaderElement("url","id", "id");
SOAPHeaderElement password = new SOAPHeaderElement("url","password", "password");
try {
authentication.addChild(user);
authentication.addChild(password);
} catch (SOAPException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
org.apache.axis.client.Call _call = createCall();
_call.setOperation(_operations[3]);
_call.setUseSOAPAction(true);
_call.setSOAPActionURI("");
_call.setEncodingStyle(null);
_call.setProperty(org.apache.axis.client.Call.SEND_TYPE_ATTR, Boolean.FALSE);
_call.setProperty(org.apache.axis.AxisEngine.PROP_DOMULTIREFS, Boolean.FALSE);
_call.setSOAPVersion(org.apache.axis.soap.SOAPConstants.SOAP11_CONSTANTS);
_call.setOperationName(new javax.xml.namespace.QName("url", "getData"));
setHeader(authentication);
setRequestHeaders(_call);
setAttachments(_call);
try { java.lang.Object _resp = _call.invoke(new java.lang.Object[] {new java.lang.Integer(saisonid)});
if (_resp instanceof java.rmi.RemoteException) {
throw (java.rmi.RemoteException)_resp;
}
else {
extractAttachments(_call);
try {
return (RankDtoResponse) _resp;
} catch (java.lang.Exception _exception) {
return (RankDtoResponse) org.apache.axis.utils.JavaUtils.convert(_resp, RankDtoResponse.class);
}
}
} catch (org.apache.axis.AxisFault axisFaultException) {
if (axisFaultException.detail != null) {
if (axisFaultException.detail instanceof java.rmi.RemoteException) {
throw (java.rmi.RemoteException) axisFaultException.detail;
}
if (axisFaultException.detail instanceof SOAPException) {
throw (SOAPException) axisFaultException.detail;
}
}
throw axisFaultException;
}
}
错误:
AxisFault
faultCode: 1-1-3
faultSubcode:
faultString: Could not authenticate, credentials not specified
faultActor:
faultNode:
faultDetail:
{http://xml.apache.org/axis/}stackTrace:Could not authenticate, credentials not specified
at org.apache.axis.message.SOAPFaultBuilder.createFault(SOAPFaultBuilder.java:222)
at org.apache.axis.message.SOAPFaultBuilder.endElement(SOAPFaultBuilder.java:129)
at org.apache.axis.encoding.DeserializationContext.endElement(DeserializationContext.java:1087)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.endElement(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanEndElement(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl.parse(Unknown Source)
at org.apache.axis.encoding.DeserializationContext.parse(DeserializationContext.java:227)
at org.apache.axis.SOAPPart.getAsSOAPEnvelope(SOAPPart.java:696)
at org.apache.axis.Message.getSOAPEnvelope(Message.java:435)
at org.apache.axis.handlers.soap.MustUnderstandChecker.invoke(MustUnderstandChecker.java:62)
at org.apache.axis.client.AxisClient.invoke(AxisClient.java:206)
at org.apache.axis.client.Call.invokeEngine(Call.java:2784)
at org.apache.axis.client.Call.invoke(Call.java:2767)
at org.apache.axis.client.Call.invoke(Call.java:2443)
at org.apache.axis.client.Call.invoke(Call.java:2366)
at org.apache.axis.client.Call.invoke(Call.java:1812)
at getData.getData(getData.java:496)
at Client.main(Client.java:20)
大部分代码是由 wsdl2java 从 wsdl 文件自动生成的。
您需要更多信息吗?我是不是漏掉了什么?
提前致谢
我觉得你的代码不错,所以不确定真正的问题在哪里。 而是在调用服务方法时修改存根 class,而不是修改存根 class。您的代码应如下所示,您可以删除对生成的存根 class 的代码修改,它应该可以工作。
OMNamespace hdrNs =
OMAbstractFactory.getOMFactory().createOMNamespace("url/auth", "nsAuthn");
SOAPHeaderBlock header = OMAbstractFactory.getSOAP12Factory().createSOAPHeaderBlock("authnHeader", hdrNs);
header.addChild(AXIOMUtil.stringToOM("<id>$id</id>"));
header.addChild(AXIOMUtil.stringToOM("<password>$id</password>"));
stub._getServiceClient().addHeader(header);
希望对您有所帮助!
我已经使用了这个 approach 并且因为您已经有了一个向 body 添加数据的示例,所以创建和向 header 添加内容同样简单。
只需使用 envelope.getHeader()
获取 Header 并将我的 header 数据添加到其中。
感谢您的帮助!