Achille
Achille

Reputation: 647

CXF + wsdl2java + authentication

I created the jar from the WSDL for my client using the wsdl2java command. Now, I need to know how I can authenticate my client in order to complete an operation?

I am using CXF 2.7.16. I created my service using the generated class MyApp_Service, I am struggling with this. Isn't there a simple way to tell my client the credentials it should use to gain access to the web service?

I read about the Spring configuration, however I am unable to figure out if it applies to my case and how if yes. I tried to cast the MyApp_Service class to BindingProvider in order to use the method which consist to put the USERNAME and PASSWORD properties in the context with a value. However, MyApp_Service cannot be cast to BindingProvider.

This is my first web service client application ever. So, any help will be welcomed.

Update 2015-05-28: I tried to define the AuthenticationPolicy but is seems not working. Here is the code:

        Client client = JaxWsDynamicClientFactory.newInstance().createClient(wsdlUrl);
        ClientImpl clt = (ClientImpl) client;
        HTTPConduit cc = (HTTPConduit) clt.getConduit();

        org.apache.cxf.configuration.security.ObjectFactory secOF = new org.apache.cxf.configuration.security.ObjectFactory();
        AuthorizationPolicy ap = secOF.createAuthorizationPolicy();
        ap.setUserName(usagerWS);
        ap.setPassword(mdpWS);
        ap.setAuthorizationType("Basic");

        cc.setAuthorization(ap);

Sniffing with WireShark, the Authorization header is clearly missing in the HTTP request.

What is missing?

Upvotes: 3

Views: 2807

Answers (1)

Achille
Achille

Reputation: 647

Problem solved, here is the solution:

        MyApp_Service service = new MyApp_Service(wsdlUrl, new QName(namespace, serviceName));
        MyApp port = service.getMyApp();

        // Set credentials
        Map<String, Object> reqCtxt = ((javax.xml.ws.BindingProvider) port).getRequestContext();
        reqCtxt.put(javax.xml.ws.BindingProvider.USERNAME_PROPERTY, username);
        reqCtxt.put(javax.xml.ws.BindingProvider.PASSWORD_PROPERTY, password);

No more usage of the dynamic client. Only the classes generated with wsdl2java are used.

Upvotes: 3

Related Questions