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

问题描述 投票:1回答:4

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

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

我尝试了许多尝试都没有用。网络上的大多数示例似乎都集中在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);
}

}
java authentication soap cxf preemptive
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);    
    }
}

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

0
投票

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

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


0
投票

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

Client client = ClientProxy.getClient(port);
HTTPConduit http = (HTTPConduit) client.getConduit();
http.getAuthorization().setUserName("theusername");            
http.getAuthorization().setPassword("thepassword");        
© www.soinside.com 2019 - 2024. All rights reserved.