如何为抢占式HTTP认证配置CXF生成的客户端



我有一个由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);
}
}

另一种方法是:

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

好了,我明白了。其实很简单。希望这能节省一些人几分钟…

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.
}

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

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

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

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

相关内容

  • 没有找到相关文章

最新更新