Class AbstractAuthenticator

  • All Implemented Interfaces:
    Authenticator, LogoutAware
    Direct Known Subclasses:
    ModularRealmAuthenticator

    public abstract class AbstractAuthenticator
    extends 
    implements Authenticator, LogoutAware
    Superclass for almost all Authenticator implementations that performs the common work around authentication attempts.

    This class delegates the actual authentication attempt to subclasses but supports notification for successful and failed logins as well as logouts. Notification is sent to one or more registered AuthenticationListeners to allow for custom processing logic when these conditions occur.

    In most cases, the only thing a subclass needs to do (via its doAuthenticate(org.apache.shiro.authc.AuthenticationToken) implementation) is perform the actual principal/credential verification process for the submitted AuthenticationToken.

    Since:
    0.1
    • Constructor Detail

      • AbstractAuthenticator

        public AbstractAuthenticator()
        Default no-argument constructor. Ensures the internal AuthenticationListener collection is a non-null ArrayList.
    • Method Detail

      • setAuthenticationListeners

        public void setAuthenticationListeners​(<AuthenticationListener> listeners)
        Sets the AuthenticationListeners that should be notified during authentication attempts.
        Parameters:
        listeners - one or more AuthenticationListeners that should be notified due to an authentication attempt.
      • notifySuccess

        protected void notifySuccess​(AuthenticationToken token,
                                     AuthenticationInfo info)
        Notifies any registered AuthenticationListeners that authentication was successful for the specified token which resulted in the specified info. This implementation merely iterates over the internal listeners collection and calls onSuccess for each.
        Parameters:
        token - the submitted AuthenticationToken that resulted in a successful authentication.
        info - the returned AuthenticationInfo resulting from the successful authentication.
      • notifyFailure

        protected void notifyFailure​(AuthenticationToken token,
                                     AuthenticationException ae)
        Notifies any registered AuthenticationListeners that authentication failed for the specified token which resulted in the specified ae exception. This implementation merely iterates over the internal listeners collection and calls onFailure for each.
        Parameters:
        token - the submitted AuthenticationToken that resulted in a failed authentication.
        ae - the resulting AuthenticationException that caused the authentication to fail.
      • notifyLogout

        protected void notifyLogout​(PrincipalCollection principals)
        Notifies any registered AuthenticationListeners that a Subject has logged-out. This implementation merely iterates over the internal listeners collection and calls onLogout for each.
        Parameters:
        principals - the identifying principals of the Subject/account logging out.
      • onLogout

        public void onLogout​(PrincipalCollection principals)
        This implementation merely calls notifyLogout to allow any registered listeners to react to the logout.
        Specified by:
        onLogout in interface LogoutAware
        Parameters:
        principals - the identifying principals of the Subject/account logging out.
      • doAuthenticate

        protected abstract AuthenticationInfo doAuthenticate​(AuthenticationToken token)
                                                      throws AuthenticationException
        Template design pattern hook for subclasses to implement specific authentication behavior.

        Common behavior for most authentication attempts is encapsulated in the authenticate(org.apache.shiro.authc.AuthenticationToken) method and that method invokes this one for custom behavior.

        N.B. Subclasses should throw some kind of AuthenticationException if there is a problem during authentication instead of returning null. A null return value indicates a configuration or programming error, since AuthenticationExceptions should indicate any expected problem (such as an unknown account or username, or invalid password, etc).

        Parameters:
        token - the authentication token encapsulating the user's login information.
        Returns:
        an AuthenticationInfo object encapsulating the user's account information important to Shiro.
        Throws:
        AuthenticationException - if there is a problem logging in the user.