1

我有一个由 CXF 使用本地 wsdl 文件生成的客户端。客户端连接正常,我从 Web 服务器收到预期的 401 错误。

我遇到的问题是无法在客户端正确配置抢占式身份验证。

我尝试了很多事情都无济于事。Web 上的大多数示例似乎都集中在 Spring 上,而不是一种普通的老式 Java 方法。

我包括客户端的主要部分。如果有人可以给我一个应该如何配置的示例,我将不胜感激。请注意,我不是在寻找任何花哨的东西。我只需要能够验证和调用服务。

public final class ServiceNowSoap_ServiceNowSoap_Client {

private static final QName SERVICE_NAME = new QName(
        "http://www.service-now.com/foo",
        "ServiceNow_foo");

private ServiceNowSoap_ServiceNowSoap_Client() {
}

public static void main(String args[]) throws java.lang.Exception {
    URL wsdlURL = ServiceNowCmdbCiComm.WSDL_LOCATION;
    if (args.length > 0 && args[0] != null && !"".equals(args[0])) {
        File wsdlFile = new File(args[0]);
        try {
            if (wsdlFile.exists()) {
                wsdlURL = wsdlFile.toURI().toURL();
            } else {
                wsdlURL = new URL(args[0]);
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
    }

    ServiceNowFoo ss = new ServiceNowFoo(wsdlURL,
            SERVICE_NAME);
    ServiceNowSoap port = ss.getServiceNowSoap();

    {
        System.out.println("Invoking deleteRecord...");
        java.lang.String _deleteRecord_sysId = "";
        java.lang.String _deleteRecord__return = port
                .deleteRecord(_deleteRecord_sysId);
        System.out.println("deleteRecord.result=" + _deleteRecord__return);

    }
    System.exit(0);
}

}
4

4 回答 4

5

另一种方法是:

import javax.xml.ws.BindingProvider;

public class CxfClientExample {

    public static void main(String[] args) throws Exception {
        String endPointAddress = "http://www.service-now.com/foo";
        ServiceNowFoo service = new ServiceNowFoo();
        ServiceNowFooPortType port = service.getServiceNowFoo();

        BindingProvider bindingProvider = (BindingProvider) port;
        bindingProvider.getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, endPointAddress);
        bindingProvider.getRequestContext().put(BindingProvider.USERNAME_PROPERTY, "theusername");
        bindingProvider.getRequestContext().put(BindingProvider.PASSWORD_PROPERTY, "thepassword");

        String deleteRecord_return = port.deleteRecord("");
        System.out.println("deleteRecord.result=" + deleteRecord_return);    
    }
}
于 2015-06-19T15:42:02.117 回答
4

好的,我想通了。归根结底很简单。希望这可以为某人节省几分钟...

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import javax.xml.namespace.QName;

import org.apache.cxf.endpoint.Client;
import org.apache.cxf.frontend.ClientProxy;
import org.apache.cxf.transport.http.HTTPConduit;

private static final QName SERVICE_NAME = new QName(
        "http://www.service-now.com/foo",
        "ServiceNow_foo");

private ServiceNowSoap_ServiceNowSoap_Client() {
}

public static void main(String args[]) throws java.lang.Exception {
    URL wsdlURL = ServiceNowFoo.WSDL_LOCATION;
    if (args.length > 0 && args[0] != null && !"".equals(args[0])) {
        File wsdlFile = new File(args[0]);
        try {
            if (wsdlFile.exists()) {
                wsdlURL = wsdlFile.toURI().toURL();
            } else {
                wsdlURL = new URL(args[0]);
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
    }

    ServiceNowFoo ss = new ServiceNowFoo(wsdlURL,
            SERVICE_NAME);

    ServiceNowSoap port = ss.getServiceNowSoap();

    Client client = ClientProxy.getClient(port);
    HTTPConduit http = (HTTPConduit) client.getConduit();
    http.getAuthorization().setUserName("theusername");
    http.getAuthorization().setPassword("thepassword");

    // Do your work here.
}
于 2013-05-22T19:43:57.587 回答
0

嗨朋友,您在调用 web 服务后配置了身份验证部分,这是如何工作的?

Client client = ClientProxy.getClient(port);
HTTPConduit http = (HTTPConduit) client.getConduit();
http.getAuthorization().setUserName("theusername");            
http.getAuthorization().setPassword("thepassword");        
于 2014-05-06T06:43:16.887 回答
0

您也可以使用拦截器。使用拦截器的一个好处是您可以将它附加到所有客户端,从而简化您的抢先式身份验证方法。看一眼 :

如何在 CXF 中修改 JAX-WS 响应的 HTTP 标头?

于 2013-05-31T18:37:39.207 回答