Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send BASIC auth credentials using cxf client

I am sending the auth credentials using a cxf webservice client and it says:

javax.xml.ws.WebServiceException: Could not send Message.

Caused by: org.apache.cxf.transport.http.HTTPException: HTTP response '401: Unauthorized' when communicating with http://localhost:8080/AccountFacadeService/AccountService

My client is:

QName SERVICE_NAME = new QName("http://webservice.account.com/", "AccountFacadeService");
URL WSDL_LOCATION = http://localhost:8080/AccountFacadeService/AccountService?wsdl;

AccountFacadeService stub = new AccountFacadeService(WSDL_LOCATION, SERVICE_NAME);
AccountService port = stub.getAccountServicePort(); 

((BindingProvider) port).getRequestContext().put(BindingProvider.USERNAME_PROPERTY, "user");
((BindingProvider) port).getRequestContext().put(BindingProvider.PASSWORD_PROPERTY, "pass");

Is there more to headers that I'm missing?

like image 548
Atif Imran Avatar asked Jan 27 '16 10:01

Atif Imran


People also ask

How do I add basic authentication to HttpClient Java?

Firstly, we create an HttpClient, which can be used to execute HTTP requests. Secondly, we create an HttpRequest using the builder design pattern. The GET method sets the HTTP method of the request. The uri method sets the URL where we would like to send the request.

Where are basic auth credentials stored?

You can store your Authorization header values in localStorage or sessionStorage. The value of Authorization header, stored in LocalStorage, will be automatically each time you make HTTP requests.

What is Apache CXF used for?

Apache CXF™ is an open source services framework. CXF helps you build and develop services using frontend programming APIs, like JAX-WS and JAX-RS. These services can speak a variety of protocols such as SOAP, XML/HTTP, RESTful HTTP, or CORBA and work over a variety of transports such as HTTP, JMS or JBI.

What is CXF endpoint?

In Apache Camel, the Camel CXF component is the key to integrating routes with Web services. You can use the Camel CXF component to create a CXF endpoint, which can be used in either of the following ways: Consumer — (at the start of a route) represents a Web service instance, which integrates with the route.


1 Answers

Alright, after many hours of digging into the issue I finally found the answer to my question.

  1. Make sure the role in the security realm of the glassfish server console is correctly configured and the same is maintained in the glassfish-ejb-jar.xml
  2. The authentication credentials are passed the way I'm passing in my post. Sometimes when the client is unable to create for you the header you may try password authenticator.

final String username = "user";
final String password = "pass";
Authenticator.setDefault(new Authenticator() {
    @Override
    protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(
                    username,
                    password.toCharArray());
            }
});
like image 58
Atif Imran Avatar answered Oct 15 '22 01:10

Atif Imran