Search code examples
web-servicestomcatservletsapache-axis

Axis2 multiple connection authentication issue


I have two servlets that access two corresponding Axis2 web services on the same host. One of the servlets is read-only, while the other writes to a database.

Each of the Axis2 web services uses BASIC authentication. The read-only web service uses a system account, while the write web service uses the user's credentials (which are submitted as part of a web form).

The problem I'm running into is that the servlet called second always fails authentication to its web service. For example, I can query the read-only service through it's servlet all I want, but I get a "401: Authorization Required" when I try to use the write service. If I call the write service first, I get the same error when I try to use the read-only service.

Here is how I am setting the credentials for the connections in the servlets:

Stub service = new Stub(serviceUrl);
HttpTransportProperties.Authenticator auth = new HttpTransportProperties.Authenticator();
auth.setUsername(username);
auth.setPassword(password);
auth.setPreemptiveAuthentication(true);

service._getServiceClient().getOptions().setProperty(HTTPConstants.AUTHENTICATE, auth);

The servlet that accesses the read-only service has this code in it's constructor. The servlet that accesses the write service has this code in it's doGet/doPost method.

It seems that the credentials for the first service called are getting cached somewhere, but I can't find where that could be. I saw a possible solution here, but I can't find where WSClientConstants.CACHED_HTTP_STATE is defined. The comments in this JIRA issue seems to imply that it's part of org.apache.axis2.transport.http.HTTPConstants but it's not there.

Specifics:

  • Axis version: 1.5.1
  • Tomcat Version: 6.0.26
  • Java version: 1.6.0_23

Solution

  • It turns out the connections to the two different services were using the same JSESSIONID. Thus, the connection to the second web service was trying to use a session authenticated for the first web service, causing the error.

    My solution for this was to define an HttpClient for each service, done by the following

    MultiThreadedHttpConnectionManager manager = new MuliThreadedHttpConnectionManager();
    HttpClient client = new HttpClient(manager);
    
    ConfigurationContext context = ConfigurationContextFactory.createDefaultConfigurationContext();
    context.setProperty(HTTPConstants.CACHED_HTTP_CLIENT, client);
    context.setProperty(HTTPConstants.REUSE_HTTP_CLIENT, true);
    
    Stub service = new Stub(context, serviceUrl);
    

    This allows both servlets to have a separate session for their corresponding services.