我有来自ERP云的wsdl。要使用Java消费它,我使用wsimport
对其进行了编译并提取了所有Java文件。
现在,我正在尝试使用这些java文件,以便通过wsdl与服务器数据进行通信。 wsdl需要认证。
问题
[每当我尝试运行自定义代码以连接到wsdl时,为了进行身份验证,它都可以通过在绑定上下文中简单地设置用户名和密码来工作。
private void attachAuthentication(Object className) { BindingProvider prov = (BindingProvider) className; prov.getRequestContext().put(BindingProvider.USERNAME_PROPERTY, "xxx"); prov.getRequestContext().put(BindingProvider.PASSWORD_PROPERTY, "xxx"); }
但是每当我在weblogic GPI实例上部署代码时,都会出现以下错误。
com.sun.xml.ws.fault.ServerSOAPFaultException: Client received SOAP Fault from server: The request was invalid or malformed Please see the server log to find more detail regarding exact cause of the failure.
at com.sun.xml.ws.fault.SOAP12Fault.getProtocolException(SOAP12Fault.java:229)
at com.sun.xml.ws.fault.SOAPFaultBuilder.createException(SOAPFaultBuilder.java:139)
at com.sun.xml.ws.client.sei.StubHandler.readResponse(StubHandler.java:253)
at com.sun.xml.ws.db.DatabindingImpl.deserializeResponse(DatabindingImpl.java:203)
at com.sun.xml.ws.db.DatabindingImpl.deserializeResponse(DatabindingImpl.java:290)
at com.sun.xml.ws.client.sei.SyncMethodHandler.invoke(SyncMethodHandler.java:119)
InitializeErp.java
HeaderHandlerResolver handlerResolver = new HeaderHandlerResolver(); InvoiceInterfaceService_Service interfaceInvoice = new InvoiceInterfaceService_Service(); interfaceInvoice.setHandlerResolver(handlerResolver); invoiceInterfaceServiceSoapHttpPort = interfaceInvoice.getInvoiceInterfaceServiceSoapHttpPort();
HeaderHandlerResolver.java
public class HeaderHandlerResolver implements HandlerResolver { @Override public List<Handler> getHandlerChain(PortInfo portInfo) { List<Handler> handlerChain = new ArrayList<Handler>(); HandlerHeader hh = new HandlerHeader(); handlerChain.add(hh); return handlerChain; } }
HeaderHandler.java
public class HandlerHeader implements SOAPHandler<SOAPMessageContext> { @Override public Set<QName> getHeaders() { return Collections.emptySet(); } @Override public boolean handleMessage(SOAPMessageContext context) { final Boolean outInd = (Boolean) context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY); if (outInd) { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.mmm'Z'"); Date creationDate = new Date(); Date expirationDate = new Date(creationDate.getTime() + TimeUnit.HOURS.toMillis(1)); try { SOAPMessage message = context.getMessage(); SOAPEnvelope env = message.getSOAPPart().getEnvelope(); env.addNamespaceDeclaration("sch", "http://xmlns.oracle.com/scheduler"); env.addNamespaceDeclaration("typ", "http://xmlns.oracle.com/scheduler/types"); SOAPHeader header = env.getHeader(); SOAPBody body = env.getBody(); env.removeNamespaceDeclaration("SOAP-ENV"); env.setPrefix("soapenv"); header.setPrefix("soapenv"); body.setPrefix("soapenv"); QName security = new QName("http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd", "Security", "wsse"); // Constructing Header SOAPElement securityElement = header.addChildElement(security); securityElement.addNamespaceDeclaration("wsu", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"); securityElement.setAttribute("soapenv:mustUnderstand", "1"); QName usernameToken = new QName("UsernameToken", "wsse"); SOAPElement usernameTokenElement = securityElement.addChildElement("UsernameToken", "wsse"); usernameTokenElement.setAttribute("wsu:Id", "UsernameToken-97B1FF404874F4997215144527824364"); QName username = new QName("Username", "wsse"); SOAPElement usernameElement = usernameTokenElement.addChildElement("Username", "wsse"); usernameElement.addTextNode("xxx"); QName passwrod = new QName("Password", "wsse"); SOAPElement passwordElement = usernameTokenElement.addChildElement("Password", "wsse"); passwordElement.setAttribute("Type", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText"); passwordElement.addTextNode("xxx"); QName nonce = new QName("Nonce", "wsse"); SOAPElement nonceElement = usernameTokenElement.addChildElement("Nonce", "wsse"); nonceElement.setAttribute("EncodingType", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary"); nonceElement.addTextNode("Ox0BI3sh1pPSe2S7NsUzeg=="); QName created = new QName("Created", "wsu"); SOAPElement createdElement = usernameTokenElement.addChildElement("Created", "wsu"); createdElement.addTextNode(format.format(creationDate)); QName timestamp = new QName("Timestamp", "wsu"); SOAPElement timestampElement = securityElement.addChildElement("Timestamp", "wsu"); timestampElement.setAttribute("wsu:Id", "TS-4"); QName createdTimestamp = new QName("Created", "wsu"); SOAPElement createdTimestampElement = timestampElement.addChildElement("Created", "wsu"); createdTimestampElement.addTextNode(format.format(creationDate)); QName expiresTimestamp = new QName("Expires", "wsu"); SOAPElement expiresTimestampElement = timestampElement.addChildElement("Expires", "wsu"); expiresTimestampElement.addTextNode(format.format(expirationDate)); // Print out the outbound SOAP message to System.out System.out.println("setting attributes..."); message.writeTo(System.out); System.out.println(""); System.out.println("message printed.."); } catch (Exception e) { e.printStackTrace(); } } return true; } @Override public boolean handleFault(SOAPMessageContext context) { try { SOAPMessage message = context.getMessage(); ByteArrayOutputStream out = new ByteArrayOutputStream(); message.writeTo(out); String strMsg = new String(out.toByteArray()); RequestFilter.getSession().setAttribute("soap fault", "Soap ->" + strMsg); } catch (SOAPException | IOException ex) { RequestFilter.getSession().setAttribute("mailContent", ex.toString()); } return true; } @Override public void close(MessageContext context) { } }
这是我的肥皂盒的外观。
在服务器上运行它。这就是我在SoapFault中得到的
Soap -> ns1:Senderns2:InvalidRequestThe request was invalid or malformedweblogic.wsee.security.wst.faults.InvalidRequestException: Server Authentication Required at weblogic.wsee.security.wst.framework.TrustSoapClient.invoke(TrustSoapClient.java:157) at weblogic.wsee.security.wst.framework.TrustSoapClient.requestTrustToken(TrustSoapClient.java:110) at weblogic.wsee.security.saml.SAMLTrustCredentialProvider.createCredential(SAMLTrustCredentialProvider.java:432) at
我有很多修改Soap标头的方法,删除了安全性,只是传递了用户名密码,在BindingContext中添加了处理程序和凭据,但是在服务器上似乎没有任何作用。
我现在很烦。任何人都可以建议我任何方法或指出我缺少的内容。我现在应该尝试什么。任何帮助表示赞赏。欢迎提出所有建议。
我有来自ERP云的wsdl。要在Java中使用它,我使用wsimport对其进行了编译,并提取了所有Java文件。现在,我正在尝试使用这些java文件来与服务器数据进行通信...
您找到解决方案了吗?我也面临这个问题。我所有的代码都已成功运行,但是由于某些原因,我需要重新部署所有东西,而现在这确实发生了。.