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 authenticate
P - 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>
An Authenticator decorator 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

    Modifier and Type
    Method
    Description
    authenticate(C credentials)
    Given a set of user-provided credentials, return an optional principal.
    void
    invalidate(C credentials)
    Discards any cached principal for the given credentials.
    void
    Discards all cached principals.
    void
    invalidateAll(Iterable<C> credentials)
    Discards any cached principal for the given collection of credentials.
    void
    invalidateAll(Predicate<? super C> predicate)
    Discards any cached principal for the collection of credentials satisfying the given predicate.
    long
    Returns the number of cached principals.
    com.github.benmanes.caffeine.cache.stats.CacheStats
    Returns a set of statistics about the cache contents and usage.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • 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 metrics
      authenticator - the underlying authenticator
      cacheSpec - a CaffeineSpec
    • 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 metrics
      authenticator - the underlying authenticator
      builder - a Caffeine
    • 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 metrics
      authenticator - the underlying authenticator
      builder - a Caffeine
      cacheNegativeResult - 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 metrics
      authenticator - the underlying authenticator
      builder - a Caffeine
      cacheNegativeResult - the boolean to enable negative cache
      supplier - a Supplier<com.github.benmanes.caffeine.cache.stats.StatsCounter>
  • Method Details

    • authenticate

      public Optional<P> authenticate(C credentials) throws AuthenticationException
      Description copied from interface: Authenticator
      Given a set of user-provided credentials, return an optional principal. If the credentials are valid and map to a principal, returns an Optional.of(Object). If the credentials are invalid, returns an Optional.empty().
      Specified by:
      authenticate in interface Authenticator<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