ServerSOAPFaultException:客户端从服务器收到SOAP Fault

时间:2018-03-23 11:13:49

标签: java soap wsdl wsimport

我有一个来自ERP云的wsdl。为了在Java中使用它,我使用wsimport编译它并提取所有java文件。

现在我尝试使用这些java文件,以便通过wsdl与服务器数据进行通信。 wsdl需要身份验证。

问题

每当我尝试运行自定义代码连接到wsdl时,对于身份验证,它可以在Binding上下文中简单设置用户名和密码。

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)

enter image description here 我尝试将Soap处理程序附加到我的SEI。这就是我所做的。

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) {
    }
}

这就是我的Soap HEader的样子。

enter image description here

在服务器上运行它。这就是我在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 

enter image description here

我有很多方法可以修改Soap标头,删除安全性,只是通过用户名密码,在BindingContext中添加了处理程序和凭据,但似乎没有任何东西可以在服务器上运行。

附加标题后,我在本地enter image description here

上收到以下错误

我现在没办法了。任何人都可以建议我任何方法或指出我错过了什么。我该怎么办呢任何帮助表示赞赏。欢迎提出所有建议。

0 个答案:

没有答案