Package org.apache.shiro.authc
Class AbstractAuthenticator
- java.lang.Object
-
- org.apache.shiro.authc.AbstractAuthenticator
-
- All Implemented Interfaces:
Authenticator,LogoutAware
- Direct Known Subclasses:
ModularRealmAuthenticator
public abstract class AbstractAuthenticator extends Object implements Authenticator, LogoutAware
Superclass for almost allAuthenticatorimplementations 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 registeredAuthenticationListeners to allow for custom processing logic when these conditions occur. In most cases, the only thing a subclass needs to do (via itsdoAuthenticate(org.apache.shiro.authc.AuthenticationToken)implementation) is perform the actual principal/credential verification process for the submittedAuthenticationToken.- Since:
- 0.1
-
-
Constructor Summary
Constructors Constructor Description AbstractAuthenticator()Default no-argument constructor.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description AuthenticationInfoauthenticate(AuthenticationToken token)Implementation of theAuthenticatorinterface that functions in the following manner: Calls templatedoAuthenticatemethod for subclass execution of the actual authentication behavior. If anAuthenticationExceptionis thrown duringdoAuthenticate,notifyany registeredAuthenticationListeners of the exception and then propagate the exception for the caller to handle. If no exception is thrown (indicating a successful login),notifyany registeredAuthenticationListeners of the successful attempt. Return theAuthenticationInfoprotected abstract AuthenticationInfodoAuthenticate(AuthenticationToken token)Template design pattern hook for subclasses to implement specific authentication behavior.Collection<AuthenticationListener>getAuthenticationListeners()Returns theAuthenticationListeners that should be notified during authentication attempts.protected voidnotifyFailure(AuthenticationToken token, AuthenticationException ae)Notifies any registeredAuthenticationListeners that authentication failed for the specifiedtokenwhich resulted in the specifiedaeexception.protected voidnotifyLogout(PrincipalCollection principals)Notifies any registeredAuthenticationListeners that aSubjecthas logged-out.protected voidnotifySuccess(AuthenticationToken token, AuthenticationInfo info)Notifies any registeredAuthenticationListeners that authentication was successful for the specifiedtokenwhich resulted in the specifiedinfo.voidonLogout(PrincipalCollection principals)This implementation merely callsnotifyLogoutto allow any registered listeners to react to the logout.voidsetAuthenticationListeners(Collection<AuthenticationListener> listeners)Sets theAuthenticationListeners that should be notified during authentication attempts.
-
-
-
Constructor Detail
-
AbstractAuthenticator
public AbstractAuthenticator()
Default no-argument constructor. Ensures the internalAuthenticationListenercollection is a non-nullArrayList.
-
-
Method Detail
-
setAuthenticationListeners
public void setAuthenticationListeners(Collection<AuthenticationListener> listeners)
Sets theAuthenticationListeners that should be notified during authentication attempts.- Parameters:
listeners- one or moreAuthenticationListeners that should be notified due to an authentication attempt.
-
getAuthenticationListeners
public Collection<AuthenticationListener> getAuthenticationListeners()
Returns theAuthenticationListeners that should be notified during authentication attempts.- Returns:
- the
AuthenticationListeners that should be notified during authentication attempts.
-
notifySuccess
protected void notifySuccess(AuthenticationToken token, AuthenticationInfo info)
Notifies any registeredAuthenticationListeners that authentication was successful for the specifiedtokenwhich resulted in the specifiedinfo. This implementation merely iterates over the internallistenerscollection and callsonSuccessfor each.- Parameters:
token- the submittedAuthenticationTokenthat resulted in a successful authentication.info- the returnedAuthenticationInforesulting from the successful authentication.
-
notifyFailure
protected void notifyFailure(AuthenticationToken token, AuthenticationException ae)
Notifies any registeredAuthenticationListeners that authentication failed for the specifiedtokenwhich resulted in the specifiedaeexception. This implementation merely iterates over the internallistenerscollection and callsonFailurefor each.- Parameters:
token- the submittedAuthenticationTokenthat resulted in a failed authentication.ae- the resultingAuthenticationExceptionthat caused the authentication to fail.
-
notifyLogout
protected void notifyLogout(PrincipalCollection principals)
Notifies any registeredAuthenticationListeners that aSubjecthas logged-out. This implementation merely iterates over the internallistenerscollection and callsonLogoutfor each.- Parameters:
principals- the identifying principals of theSubject/account logging out.
-
onLogout
public void onLogout(PrincipalCollection principals)
This implementation merely callsnotifyLogoutto allow any registered listeners to react to the logout.- Specified by:
onLogoutin interfaceLogoutAware- Parameters:
principals- the identifying principals of theSubject/account logging out.
-
authenticate
public final AuthenticationInfo authenticate(AuthenticationToken token) throws AuthenticationException
Implementation of theAuthenticatorinterface that functions in the following manner:- Calls template
doAuthenticatemethod for subclass execution of the actual authentication behavior. - If an
AuthenticationExceptionis thrown duringdoAuthenticate,notifyany registeredAuthenticationListeners of the exception and then propagate the exception for the caller to handle. - If no exception is thrown (indicating a successful login),
notifyany registeredAuthenticationListeners of the successful attempt. - Return the
AuthenticationInfo
- Specified by:
authenticatein interfaceAuthenticator- Parameters:
token- the submitted token representing the subject's (user's) login principals and credentials.- Returns:
- the AuthenticationInfo referencing the authenticated user's account data.
- Throws:
AuthenticationException- if there is any problem during the authentication process - see the interface's JavaDoc for a more detailed explanation.- See Also:
ExpiredCredentialsException,IncorrectCredentialsException,ExcessiveAttemptsException,LockedAccountException,ConcurrentAccessException,UnknownAccountException
- Calls template
-
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 theauthenticate(org.apache.shiro.authc.AuthenticationToken)method and that method invokes this one for custom behavior. N.B. Subclasses should throw some kind ofAuthenticationExceptionif there is a problem during authentication instead of returningnull. Anullreturn value indicates a configuration or programming error, sinceAuthenticationExceptions 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
AuthenticationInfoobject encapsulating the user's account information important to Shiro. - Throws:
AuthenticationException- if there is a problem logging in the user.
-
-