Class AbstractAuthenticator
java.lang.Object
org.apache.shiro.authc.AbstractAuthenticator
- All Implemented Interfaces:
Authenticator,LogoutAware
- Direct Known Subclasses:
ModularRealmAuthenticator
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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionfinal 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 AuthenticationInfoTemplate design pattern hook for subclasses to implement specific authentication behavior.Returns theAuthenticationListeners that should be notified during authentication attempts.protected voidNotifies 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.voidSets theAuthenticationListeners that should be notified during authentication attempts.
-
Constructor Details
-
AbstractAuthenticator
public AbstractAuthenticator()Default no-argument constructor. Ensures the internalAuthenticationListenercollection is a non-nullArrayList.
-
-
Method Details
-
setAuthenticationListeners
Sets theAuthenticationListeners that should be notified during authentication attempts.- Parameters:
listeners- one or moreAuthenticationListeners that should be notified due to an authentication attempt.
-
getAuthenticationListeners
Returns theAuthenticationListeners that should be notified during authentication attempts.- Returns:
- the
AuthenticationListeners that should be notified during authentication attempts.
-
notifySuccess
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
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
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
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:
- 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.
-