简体   繁体   中英

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);

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM