SoapFaultClientException:用户名和/或/密码不能为空

时间:2016-09-29 09:17:09

标签: web-services soap spring-boot soap-client

我已经构建了一个客户端,它应该从远程的基于wsdl的webservice(使用SOAP)获取数据。

但是每当我尝试连接(通过调用函数)服务时,我都会遇到以下异常:

org.springframework.ws.soap.client.SoapFaultClientException: Username and/or Password cannot be null
    at org.springframework.ws.soap.client.core.SoapFaultMessageResolver.resolveFault(SoapFaultMessageResolver.java:38) ~[spring-ws-core-2.2.0.RELEASE.jar:2.2.0.RELEASE]
    at org.springframework.ws.client.core.WebServiceTemplate.handleFault(WebServiceTemplate.java:826) ~[spring-ws-core-2.2.0.RELEASE.jar:2.2.0.RELEASE]
    at org.springframework.ws.client.core.WebServiceTemplate.doSendAndReceive(WebServiceTemplate.java:621) ~[spring-ws-core-2.2.0.RELEASE.jar:2.2.0.RELEASE]
    at org.springframework.ws.client.core.WebServiceTemplate.sendAndReceive(WebServiceTemplate.java:555) ~[spring-ws-core-2.2.0.RELEASE.jar:2.2.0.RELEASE]
    at org.springframework.ws.client.core.WebServiceTemplate.marshalSendAndReceive(WebServiceTemplate.java:390) ~[spring-ws-core-2.2.0.RELEASE.jar:2.2.0.RELEASE]
    at com.test.adminUI.myPartners.client.MyPartnersServiceClient.sendSoapRequest(MyPartnersServiceClient.java:113) [bin/:na]
    at com.test.adminUI.myPartners.client.MyPartnersServiceClient.findUser(PartnersServiceClient.java:70) [bin/:na]...

如果我输错了密码,例如服务注册了它,则抛出未经授权的401错误 这就是说,它实际上验证了我的useraccount细节。

我的客户:

public class MyServiceClient  extends WebServiceGatewaySupport {

    @Autowired
    private ObjectFactory factory;
    @Autowired
    private SoapProperties adProperties;

    private static final String WS_ADDRESSING_URI = "http://www.w3.org/2005/08/addressing";
    private static final String TO_TAG = "To";
    private static final String ACTION_TAG = "Action";
    private static final String WSA_PREFIX = "wsa";

    private static final String SOAP_ACTION_FIND_IFXPERSON = adProperties.getsoapURL();


    public List<Person> findUser(String email, String globalID) {
        List<Person> list = null;
        FindPerson findperson = new FindPerson();
        try {
            findperson.setGlobalID(factory.createGlobalID(globalID));

            findperson.setServiceUsername(factory.createServiceUsername(adProperties.getServiceUser()));
            findperson.setServicePassword(factory.createServicePassword(adProperties.getServicePassword()));


            FindPersonResponse response = (FindPersonResponse) sendSoapRequest(
                    SOAP_ACTION_FIND_PERSON, findperson);

            list = response.getFindPersonResult().getValue();
        } catch (Exception ex) {
            log.error("could not find Person: ", ex);
        }

        return null;
    }


    private Object sendSoapRequest(final String soapAction, Object payLoad) {

        Object response = null;

        try {
            Credentials auth = new NTCredentials(adProperties.getAuthUser(),
                    adProperties.getAuthPassword(), null, adProperties.getAuthDomain());

            HttpClientBuilder clientBuilder = HttpClientBuilder.create();

            CredentialsProvider credsProvider = new BasicCredentialsProvider();
            credsProvider.setCredentials(AuthScope.ANY, auth);
            clientBuilder.setDefaultCredentialsProvider(credsProvider);

            RemoveSoapHeadersInterceptor interceptor = new RemoveSoapHeadersInterceptor();
            clientBuilder.addInterceptorFirst(interceptor);

            HttpClient httpClient = clientBuilder.build();

            HttpComponentsMessageSender messageSender = new HttpComponentsMessageSender();
            messageSender.setHttpClient(httpClient);
            messageSender.setCredentials(auth);
            messageSender.afterPropertiesSet();

            getWebServiceTemplate().setMessageSender(messageSender);
            SaajSoapMessageFactory messageFactory = new SaajSoapMessageFactory(
                    MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL));
            getWebServiceTemplate().setMessageFactory(messageFactory);

            response = getWebServiceTemplate().marshalSendAndReceive(
                    adProperties.getServiceEndpoint(), payLoad, new SoapActionCallback(soapAction) {

                        public void doWithMessage(WebServiceMessage message) {
                            try {
                                SaajSoapMessage soapMessage = (SaajSoapMessage) message;
                                SOAPMessage saajMessage = soapMessage.getSaajMessage();

                                SOAPEnvelope envelope = saajMessage.getSOAPPart().getEnvelope();
                                SOAPHeader header = envelope.getHeader();

                                QName wsaToQName = new QName(WS_ADDRESSING_URI, TO_TAG, WSA_PREFIX);
                                SOAPHeaderElement wsaTo = header.addHeaderElement(wsaToQName);
                                wsaTo.setTextContent(adProperties.getServiceEndpoint());

                                QName wsaActionQName = new QName(WS_ADDRESSING_URI, ACTION_TAG,
                                        WSA_PREFIX);
                                SOAPHeaderElement wsaAction = header
                                        .addHeaderElement(wsaActionQName);
                                wsaAction.setTextContent(soapAction);

                            } catch (Exception e) {
                                log.error("", e);
                            }
                        }
                    });
        } catch (Exception ex) {
            log.error(ex);
        }
        return response;
    }
}

配置:

@Configuration
public class MyPartnersServiceConfiguration {

    @Bean
    public Jaxb2Marshaller marshaller() {
        Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
        marshaller.setPackagesToScan("com.test.adminUI.myPartners.wsdl");
        return marshaller;
    }

    @Bean
    public IFXPartnersServiceClient iFXPartnersServiceClient(Jaxb2Marshaller marshaller) {
        IFXPartnersServiceClient client = new IFXPartnersServiceClient();
        client.setDefaultUri("http://test.idms.partnersservice");
        client.setMarshaller(marshaller);
        client.setUnmarshaller(marshaller);
        return client;
    }

}

有人知道我要做什么吗?

提前致谢!

更新

我忘了提及,我的控制台也收到警告:

 o.a.http.impl.auth.HttpAuthenticator: NEGOTIATE authentication error: No valid credentials provided (Mechanism level: No valid credentials provided (Mechanism level: Failed to find any Kerberos tgt))

这是问题吗?在这种情况下我该怎么做?

1 个答案:

答案 0 :(得分:0)

   @Bean
public IFXPartnersServiceClient iFXPartnersServiceClient(Jaxb2Marshaller marshaller) {
    IFXPartnersServiceClient client = new IFXPartnersServiceClient();
    client.setDefaultUri("http://test.idms.partnersservice");
    client.setMarshaller(marshaller);
    client.setUnmarshaller(marshaller);

 //Set messageSender to client
client.setMessageSender(httpComponentsMessageSender());

    return client;
}