Package io.dropwizard.auth
Class CachingAuthenticator<C,P extends Principal>
- java.lang.Object
-
- io.dropwizard.auth.CachingAuthenticator<C,P>
-
- Type Parameters:
C- the type of credentials the authenticator can authenticateP- the type of principals the authenticator returns
- All Implemented Interfaces:
Authenticator<C,P>
public class CachingAuthenticator<C,P extends Principal> extends Object implements Authenticator<C,P>
AnAuthenticatordecorator which uses a Caffeine cache to temporarily cache credentials and their corresponding principals.
-
-
Constructor Summary
Constructors Constructor Description CachingAuthenticator(com.codahale.metrics.MetricRegistry metricRegistry, Authenticator<C,P> authenticator, com.github.benmanes.caffeine.cache.Caffeine<Object,Object> builder)Creates a new cached authenticator.CachingAuthenticator(com.codahale.metrics.MetricRegistry metricRegistry, Authenticator<C,P> authenticator, com.github.benmanes.caffeine.cache.Caffeine<Object,Object> builder, boolean cacheNegativeResult)Creates a new cached authenticator.CachingAuthenticator(com.codahale.metrics.MetricRegistry metricRegistry, Authenticator<C,P> authenticator, com.github.benmanes.caffeine.cache.Caffeine<Object,Object> builder, boolean cacheNegativeResult, Supplier<com.github.benmanes.caffeine.cache.stats.StatsCounter> supplier)Creates a new cached authenticator.CachingAuthenticator(com.codahale.metrics.MetricRegistry metricRegistry, Authenticator<C,P> authenticator, com.github.benmanes.caffeine.cache.CaffeineSpec cacheSpec)Creates a new cached authenticator.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Optional<P>authenticate(C credentials)Given a set of user-provided credentials, return an optional principal.voidinvalidate(C credentials)Discards any cached principal for the given credentials.voidinvalidateAll()Discards all cached principals.voidinvalidateAll(Iterable<C> credentials)Discards any cached principal for the given collection of credentials.voidinvalidateAll(Predicate<? super C> predicate)Discards any cached principal for the collection of credentials satisfying the given predicate.longsize()Returns the number of cached principals.com.github.benmanes.caffeine.cache.stats.CacheStatsstats()Returns a set of statistics about the cache contents and usage.
-
-
-
Constructor Detail
-
CachingAuthenticator
public CachingAuthenticator(com.codahale.metrics.MetricRegistry metricRegistry, Authenticator<C,P> authenticator, com.github.benmanes.caffeine.cache.CaffeineSpec cacheSpec)Creates a new cached authenticator.- Parameters:
metricRegistry- the application's registry of metricsauthenticator- the underlying authenticatorcacheSpec- aCaffeineSpec
-
CachingAuthenticator
public CachingAuthenticator(com.codahale.metrics.MetricRegistry metricRegistry, Authenticator<C,P> authenticator, com.github.benmanes.caffeine.cache.Caffeine<Object,Object> builder)Creates a new cached authenticator.- Parameters:
metricRegistry- the application's registry of metricsauthenticator- the underlying authenticatorbuilder- aCaffeine
-
CachingAuthenticator
public CachingAuthenticator(com.codahale.metrics.MetricRegistry metricRegistry, Authenticator<C,P> authenticator, com.github.benmanes.caffeine.cache.Caffeine<Object,Object> builder, boolean cacheNegativeResult)Creates a new cached authenticator.- Parameters:
metricRegistry- the application's registry of metricsauthenticator- the underlying authenticatorbuilder- aCaffeinecacheNegativeResult- the boolean to enable negative cache
-
CachingAuthenticator
public CachingAuthenticator(com.codahale.metrics.MetricRegistry metricRegistry, Authenticator<C,P> authenticator, com.github.benmanes.caffeine.cache.Caffeine<Object,Object> builder, boolean cacheNegativeResult, Supplier<com.github.benmanes.caffeine.cache.stats.StatsCounter> supplier)Creates a new cached authenticator.- Parameters:
metricRegistry- the application's registry of metricsauthenticator- the underlying authenticatorbuilder- aCaffeinecacheNegativeResult- the boolean to enable negative cachesupplier- aSupplier
-
-
Method Detail
-
authenticate
public Optional<P> authenticate(C credentials) throws AuthenticationException
Description copied from interface:AuthenticatorGiven a set of user-provided credentials, return an optional principal. If the credentials are valid and map to a principal, returns anOptional.of(Object). If the credentials are invalid, returns anOptional.empty().- Specified by:
authenticatein interfaceAuthenticator<C,P extends Principal>- Parameters:
credentials- a set of user-provided credentials- Returns:
- either an authenticated principal or an absent optional
- Throws:
AuthenticationException- if the credentials cannot be authenticated due to an underlying error
-
invalidate
public void invalidate(C credentials)
Discards any cached principal for the given credentials.- Parameters:
credentials- a set of credentials
-
invalidateAll
public void invalidateAll(Iterable<C> credentials)
Discards any cached principal for the given collection of credentials.- Parameters:
credentials- a collection of credentials
-
invalidateAll
public void invalidateAll(Predicate<? super C> predicate)
Discards any cached principal for the collection of credentials satisfying the given predicate.- Parameters:
predicate- a predicate to filter credentials
-
invalidateAll
public void invalidateAll()
Discards all cached principals.
-
size
public long size()
Returns the number of cached principals.- Returns:
- the number of cached principals
-
stats
public com.github.benmanes.caffeine.cache.stats.CacheStats stats()
Returns a set of statistics about the cache contents and usage.- Returns:
- a set of statistics about the cache contents and usage
-
-