Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java Apache Cxf HTTP authentication

Tags:

java

cxf

I have WSDL. I need to make HTTP basic (preemptive) authentication. What to do?

I tried :

Authenticator myAuth = new Authenticator() {
    @Override
    protected PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication("user", "pass".toCharArray());
    }
};
Authenticator.setDefault(myAuth);

But it does not work: Caused by:

java.io.IOException: Server returned HTTP response code: 401 for URL ..

P.S. I use Apache CXF 2.6.2 and JBoss 5.0.1

like image 713
Bera Avatar asked Oct 15 '12 07:10

Bera


People also ask

How does Apache CXF work?

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.

How do I generate a class from WSDL using Apache CXF?

You will have to make sure that you create an appropriate directory structure for your project and add the earlier shown hello. wsdl file to the specified folder. The wsdl2java plugin will compile this wsdl and create Apache CXF classes in a pre-defined folder.

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.

What is the use of HTTP conduit?

http-conf:conduit ElementSpecifies the HTTP connection properties such as timeouts, keep-alive requests, content types, etc. Specifies the the parameters for configuring the basic authentication method that the endpoint uses preemptively.


1 Answers

What you specified for your authentication is not enough. You should do something like this:

private YourService proxy;

public YourServiceWrapper() {
    try {
        final String username = "username";
        final String password = "password";
        Authenticator.setDefault(new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(
                        username,
                        password.toCharArray());
            }
        });
        URL url = new URL("http://yourserviceurl/YourService?WSDL");
        QName qname = new QName("http://targetnamespace/of/your/wsdl", "YourServiceNameInWsdl");
        Service service = Service.create(url, qname);
        proxy = service.getPort(YourService.class);
        Map<String, Object> requestContext = ((BindingProvider) proxy).getRequestContext();
        requestContext.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, url.toString());
        requestContext.put(BindingProvider.USERNAME_PROPERTY, username);
        requestContext.put(BindingProvider.PASSWORD_PROPERTY, password);
        Map<String, List<String>> headers = new HashMap<String, List<String>>();
        requestContext.put(MessageContext.HTTP_REQUEST_HEADERS, headers);
    } catch (Exception e) {
        LOGGER.error("Error occurred in web service client initialization", e);
    }
}

Properties:

  1. YourService - your generated web service client interface.
  2. YourServiceWrapper() - wrapper class constructor which initializes your service.
  3. url - URL to your web service with ?WSDL extension.
  4. qname - first constructor argument: target namespace from your WSDL file. Second: your service name from WSDL.

Then you will be able to call your web service methods like this:

proxy.whatEverMethod();
like image 119
Paulius Matulionis Avatar answered Oct 04 '22 07:10

Paulius Matulionis