Class PreemptiveAuthorizer

  • All Implemented Interfaces:
    org.apache.http.HttpRequestInterceptor

    public class PreemptiveAuthorizer
    extends Object
    implements org.apache.http.HttpRequestInterceptor
    An interceptor that initiates authorization without waiting for a challenge from the server. This avoids a response/request exchange (for the challenge), but it doesn't give the server a chance to select the authorization scheme or realm.

    To make this work:

    • In your HttpContext attributes, set a list of preferred auth scheme names.
    • In your DigestHttpClient auth schemes, register an AuthSchemeFactory for at least one of those schemes.
    • In your DigestHttpClient credentials provider, set Credentials for an AuthScope that will match your request.
    • In your DigestHttpClient params, register any parameters the AuthSchemeFactory requires.
    • In your DigestHttpClient, add an instance of this interceptor.
    Here's an example using OAuth:
     context = new BasicHttpContext();
     context.setAttribute(ClientContext.AUTH_SCHEME_PREF,
       Arrays.asList(OAuthSchemeFactory.SCHEME_NAME));
     client = new DefaultHttpClient();
     client.getAuthSchemes().register(OAuthSchemeFactory.SCHEME_NAME,
       new OAuthSchemeFactory());
     client.getCredentialsProvider().setCredentials(new AuthScope("server.com", 80),
       new OAuthCredentials(accessor));
     client.getParams().setParameter(OAuthSchemeFactory.DEFAULT_REALM,
       ProtectedResource.REALM);
     client.addRequestInterceptor(new PreemptiveAuthorizer(), 0);
     
    Author:
    John Kristian
    • Constructor Detail

      • PreemptiveAuthorizer

        public PreemptiveAuthorizer()
    • Method Detail

      • process

        public void process​(org.apache.http.HttpRequest request,
                            org.apache.http.protocol.HttpContext context)
                     throws org.apache.http.HttpException,
                            IOException
        If no auth scheme has been selected for the given context, consider each of the preferred auth schemes and select the first one for which an AuthScheme and matching Credentials are available.
        Specified by:
        process in interface org.apache.http.HttpRequestInterceptor
        Throws:
        org.apache.http.HttpException
        IOException