如何为抢占式HTTP身份验证配置CXF生成的客户端?

时间:2013-05-22 17:46:23

标签: java authentication soap cxf preemptive

我有一个由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 个答案:

答案 0 :(得分: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);    
    }
}

答案 1 :(得分: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");

答案 2 :(得分:0)

您也可以使用拦截器。使用拦截器的一个好处是,您可以将其附加到您的所有客户端,简化您的先发制人身份验证方法。 看看:

How do i modify HTTP headers for a JAX-WS response in CXF?

答案 3 :(得分:0)

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

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