Use of CachingAuthenticator in Dropwizard

Solution 1:

I don't know about about "2)" but for "1)", you simply need to wire the authenticator up to the Basic auth provider

CachingAuthenticator<BasicCredentials, AuthUser> cachingAuthenticator =
        new CachingAuthenticator<>(environment.metrics(), new WebAuthenticator(), 
                configuration.getAuthenticationCachePolicy());

environment.jersey().register(AuthFactory.binder(
            new BasicAuthFactory<>(cachingAuthenticator,
                     "Example Realm", AuthUser.class)));

Solution 2:

Working solution

/** authentication config */
CachingAuthenticator<BasicCredentials, AuthUser> cachingAuthenticator = new CachingAuthenticator<>(
    environment.metrics(), new MyAuthenticator(), configuration.getAuthenticationCachePolicy());

environment.jersey().register(new AuthDynamicFeature(new BasicCredentialAuthFilter.Builder<AuthUser>()
    .setAuthenticator(cachingAuthenticator).setRealm("kedar.javalkar.realm").buildAuthFilter()));

environment.jersey().register(new AuthValueFactoryProvider.Binder<>(AuthUser.class));

The yml can either have

# Caching authenticator.
authenticationCachePolicy: maximumSize=10000, expireAfterAccess=10m

or

# Caching authenticator.
authenticationCachePolicy: maximumSize=10000, expireAfterAccess=1s