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 allAuthenticator
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 registeredAuthenticationListener
s 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 AuthenticationInfo
authenticate(AuthenticationToken token)
Implementation of theAuthenticator
interface that functions in the following manner: Calls templatedoAuthenticate
method for subclass execution of the actual authentication behavior. If anAuthenticationException
is thrown duringdoAuthenticate
,notify
any registeredAuthenticationListener
s of the exception and then propagate the exception for the caller to handle. If no exception is thrown (indicating a successful login),notify
any registeredAuthenticationListener
s of the successful attempt. Return theAuthenticationInfo
protected abstract AuthenticationInfo
doAuthenticate(AuthenticationToken token)
Template design pattern hook for subclasses to implement specific authentication behavior.Collection<AuthenticationListener>
getAuthenticationListeners()
Returns theAuthenticationListener
s that should be notified during authentication attempts.protected void
notifyFailure(AuthenticationToken token, AuthenticationException ae)
Notifies any registeredAuthenticationListener
s that authentication failed for the specifiedtoken
which resulted in the specifiedae
exception.protected void
notifyLogout(PrincipalCollection principals)
Notifies any registeredAuthenticationListener
s that aSubject
has logged-out.protected void
notifySuccess(AuthenticationToken token, AuthenticationInfo info)
Notifies any registeredAuthenticationListener
s that authentication was successful for the specifiedtoken
which resulted in the specifiedinfo
.void
onLogout(PrincipalCollection principals)
This implementation merely callsnotifyLogout
to allow any registered listeners to react to the logout.void
setAuthenticationListeners(Collection<AuthenticationListener> listeners)
Sets theAuthenticationListener
s that should be notified during authentication attempts.
-
-
-
Constructor Detail
-
AbstractAuthenticator
public AbstractAuthenticator()
Default no-argument constructor. Ensures the internalAuthenticationListener
collection is a non-nullArrayList
.
-
-
Method Detail
-
setAuthenticationListeners
public void setAuthenticationListeners(Collection<AuthenticationListener> listeners)
Sets theAuthenticationListener
s that should be notified during authentication attempts.- Parameters:
listeners
- one or moreAuthenticationListener
s that should be notified due to an authentication attempt.
-
getAuthenticationListeners
public Collection<AuthenticationListener> getAuthenticationListeners()
Returns theAuthenticationListener
s that should be notified during authentication attempts.- Returns:
- the
AuthenticationListener
s that should be notified during authentication attempts.
-
notifySuccess
protected void notifySuccess(AuthenticationToken token, AuthenticationInfo info)
Notifies any registeredAuthenticationListener
s that authentication was successful for the specifiedtoken
which resulted in the specifiedinfo
. This implementation merely iterates over the internallisteners
collection and callsonSuccess
for each.- Parameters:
token
- the submittedAuthenticationToken
that resulted in a successful authentication.info
- the returnedAuthenticationInfo
resulting from the successful authentication.
-
notifyFailure
protected void notifyFailure(AuthenticationToken token, AuthenticationException ae)
Notifies any registeredAuthenticationListener
s that authentication failed for the specifiedtoken
which resulted in the specifiedae
exception. This implementation merely iterates over the internallisteners
collection and callsonFailure
for each.- Parameters:
token
- the submittedAuthenticationToken
that resulted in a failed authentication.ae
- the resultingAuthenticationException
that caused the authentication to fail.
-
notifyLogout
protected void notifyLogout(PrincipalCollection principals)
Notifies any registeredAuthenticationListener
s that aSubject
has logged-out. This implementation merely iterates over the internallisteners
collection and callsonLogout
for each.- Parameters:
principals
- the identifying principals of theSubject
/account logging out.
-
onLogout
public void onLogout(PrincipalCollection principals)
This implementation merely callsnotifyLogout
to allow any registered listeners to react to the logout.- Specified by:
onLogout
in interfaceLogoutAware
- Parameters:
principals
- the identifying principals of theSubject
/account logging out.
-
authenticate
public final AuthenticationInfo authenticate(AuthenticationToken token) throws AuthenticationException
Implementation of theAuthenticator
interface that functions in the following manner:- Calls template
doAuthenticate
method for subclass execution of the actual authentication behavior. - If an
AuthenticationException
is thrown duringdoAuthenticate
,notify
any registeredAuthenticationListener
s of the exception and then propagate the exception for the caller to handle. - If no exception is thrown (indicating a successful login),
notify
any registeredAuthenticationListener
s of the successful attempt. - Return the
AuthenticationInfo
- Specified by:
authenticate
in 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 ofAuthenticationException
if there is a problem during authentication instead of returningnull
. Anull
return value indicates a configuration or programming error, sinceAuthenticationException
s 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.
-
-