Search code examples
javaweb-servicesapache-axis

Axis2 working with even BASIC authentication


Is it even possible? The documentation does not make it clear. I've tried all sorts of different mechanisms to get authentication working, but I continually get a "401: Unauthorized" error. Here is where I am at:

    stub = new EngineStub();

    HttpClient client = new HttpClient();
    client.getParams().setAuthenticationPreemptive(true);
    stub._getServiceClient().getServiceContext().getConfigurationContext().setProperty(HTTPConstants.CACHED_HTTP_CLIENT, client);

    HttpTransportProperties.Authenticator auth = new HttpTransportProperties.Authenticator();
    List<String> authpref = new ArrayList<String>();
    authpref.add(AuthPolicy.BASIC);
    auth.setAuthSchemes(authpref);
    auth.setUsername(username); 
    auth.setPassword(password);
    auth.setPreemptiveAuthentication(true);
    stub._getServiceClient().getOptions().setProperty(HTTPConstants.AUTHENTICATE, auth);

Solution

  • What do you mean axis2 work with BASIC AUTH? If your backend service is secured using basic auth, all you have to do is set the basic auth header from client and send requests. Your client code looks right; not sure about auth policy;try without like this;

     auth.setUsername(USER_NAME);
        auth.setPassword(PASSWORD);
        auth.setPreemptiveAuthentication(true);
        option.setProperty(org.apache.axis2.transport.http.HTTPConstants.AUTHENTICATE, auth);
        option.setManageSession(true);